获取路径ACLS并将处理结果按每1000行拆分输出为多个CSV文件的方案咨询
千万级路径ACL查询+拆分CSV实现方案(PowerShell 环境适配NTFS场景)
前置优化说明
- 采用流式逐行读取输入文件,避免一次性加载1950万行数据导致内存溢出
- 内置错误捕获逻辑,路径不存在/无权限访问时不会中断整体遍历流程
- 批次拆分逻辑自动对齐1000条/文件的要求,不足1000条的尾部数据会单独生成一个文件
可直接运行的实现代码
# 配置项:按需修改 $inputTxtPath = "C:\path\to\your\all_paths.txt" $outputDir = "C:\path\to\output\csv_dir" $batchSize = 1000 # 初始化变量 $batchCount = 1 $currentBatch = @() $counter = 0 # 创建输出目录 if (-not (Test-Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir | Out-Null } # 流式逐行读取大文件,性能远高于Get-Content foreach ($fullPath in [System.IO.File]::ReadLines($inputTxtPath)) { $counter++ $aclResult = [PSCustomObject]@{ 完整路径 = $fullPath 所有者 = $null 访问规则 = $null 查询状态 = "成功" 错误信息 = $null } try { $acl = Get-Acl -Path $fullPath -ErrorAction Stop $aclResult.所有者 = $acl.Owner # 访问规则转为字符串存储,也可自行拆分单个规则为多行 $aclResult.访问规则 = $acl.AccessToString } catch { $aclResult.查询状态 = "失败" $aclResult.错误信息 = $_.Exception.Message } $currentBatch += $aclResult # 达到批次大小则导出CSV if ($counter % $batchSize -eq 0) { $outputPath = Join-Path $outputDir "acl_result_$batchCount.csv" $currentBatch | Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8 $batchCount++ $currentBatch = @() # 可选:打印进度 Write-Host "已处理 $counter 条,生成第 $($batchCount-1) 个批次文件" } } # 导出最后不足1000条的剩余数据 if ($currentBatch.Count -gt 0) { $outputPath = Join-Path $outputDir "acl_result_$batchCount.csv" $currentBatch | Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8 Write-Host "处理完成,共生成 $batchCount 个CSV文件,总处理条数 $counter" }
性能优化建议
- PowerShell 7+版本可将foreach循环替换为
ForEach-Object -Parallel,设置-ThrottleLimit 8(根据磁盘IO性能调整阈值),处理速度可提升3~8倍 - 不需要的字段可直接从
$aclResult中删除,减少单条数据体积,提升整体处理效率 - 若输入txt为非UTF8编码,可在
[System.IO.File]::ReadLines的第二个参数指定编码,例如[System.Text.Encoding]::GetEncoding("GB2312")
内容的提问来源于stack exchange,提问作者Jimcesseg
相关产品推荐
相关产品推荐

