【发布时间】:2021-12-15 18:45:43
【问题描述】:
一个月以来,我一直坚持尝试以该脚本所需的格式导出。我不知道如何让它在两个单独的循环中将这些变量导出到一个 .csv 文件中。我们得到一个 .csv,其中包含 staging_input 和 staging_location 字段,这两个字段都包含网络上的文件位置。我们需要比较这两者以确保它们的文件数和大小相同。我为这些字段中的每一个创建了一个ForEach-Object 循环,这给了我所需的输出,但我无法在每个循环结束时将export-csv 转换为单个.csv。
#User input CSV File and Workorder
$workorder = Read-Host -Prompt 'Please enter the Fq job code'
$pos = $workorder.IndexOf("_")
$Client = $workorder.Substring(0, $pos)
$Matter = $workorder.Substring(6, $pos)
$job = $workorder.Substring($pos+7)
$csvoutputpath = "\\ldthost.pvt\client\" + $Client + "\" + $Matter + "\Proc\WKP\" + $job
$outputfilename = $workorder + ".csv"
$csvinputpath = Read-Host -Prompt 'Please Input the directory of the CSV file containing Staging input and Staging location'
$staginginput = Import-Csv $csvinputpath | select -ExpandProperty staging_input
$staginginputpath = $staginginput.TrimStart("\")
#Get Child Item for each line
$staginginputpath | ForEach-Object{
# In here, we do whatever want to the 'cell' that's currently in the pipeline
# Get File Counts
$staginginputcount = (Get-ChildItem -Recurse -File -Force -LiteralPath \\?\UNC\$_ | Measure-Object).Count
#Get size
$staginginputsize = Get-ChildItem -Recurse -File -Force -LiteralPath \\?\UNC\$_ | Measure-Object -property length -sum
$staginginputsize = $staginginputsize.sum / 1KB
}
$staginglocation = Import-Csv $csvinputpath | select -ExpandProperty staging_location
$staginglocationpath = $staginglocation.TrimStart("\")
#Get Child Item for each line
$staginglocationpath | ForEach-Object{
# In here, we do whatever want to the 'cell' that's currently in the pipeline
# Get File Counts
$staginglocationcount = (Get-ChildItem -Recurse -File -Force -LiteralPath \\?\UNC\$_ | Measure-Object).Count
#Get size
$staginglocationsize = Get-ChildItem -Recurse -File -Force -LiteralPath \\?\UNC\$_ | Measure-Object -property length -sum
$staginglocationsize = $staginglocationsize.sum / 1KB
}
##Export Final Output
$data = @()
$row = New-Object PSObject
$row | Add-Member -MemberType NoteProperty -Name "staging_input" -Value $staginginput
$row | Add-Member -MemberType NoteProperty -Name "staging_location" -Value $staginglocation
$row | Add-Member -MemberType NoteProperty -Name "staging_input_File_Count" -Value $staginginputcount
$row | Add-Member -MemberType NoteProperty -Name "staging_location_File_Count" -Value $staginglocationcount
$row | Add-Member -MemberType NoteProperty -Name "staging_input_File_Size" -Value $staginginputsize
$row | Add-Member -MemberType NoteProperty -Name "staging_location_File_Size" -Value $staginglocationsize
$data += $row
$Finaloutput = $csvoutputpath + "\" + $outputfilename
$data | Export-Csv $Finaloutput -NoTypeInformation -Append
【问题讨论】:
-
在深入研究之前。 . .你只是想比较2个csv?
Compare-Object没有给你预期的结果吗? -
$staginginput和$staginglocation都是对象的数组。不是单弦。按照@AbrahamZinala 的建议尝试Compare-Object。 -
@AbrahamZinala 非常感谢您的回复。更重要的是比较 csv 中目录的大小。我们得到的只有一个 csv,其中包含需要比较的目录。我们所做的是从暂存输入复制到暂存位置,因此它们应该相同,但我们需要检查以确保质量。即使导入 csv 有很多需要忽略的字段,Compare-Object 是否会给出所需的最终输出?
-
如果结果符合您的需要,这取决于您。为什么不直接运行它?
标签: powershell csv export-to-csv