需要使用 Azure Powershell 脚本比较两个 csv 文件并获取不匹配的值。 请帮忙。 1.csv文件示例
"网络" "10.4.0.0/16" "10.10.0.0/16" "10.37.0.0/16" "10.80.0.0/16">
- csv 文件示例
"地址前缀文本" ""10.81.0.0/16"", ""10.82.0.0/16"", ""10.10.0.0/16"", "10.4.0.0/16">
,谢谢,
我想下面这样的事情应该可以做到:
$fileA = 'E:ValidationER1.csv'
$fileB = 'E:ValidationLocalGateway1.csv'
# FileA can be imported as-is
$csvA = Import-Csv -Path $fileA
# FileB needs treatment after importing
$csvB = Import-Csv -Path $fileB | Select-Object @{Name = 'Network'; Expression = {$_.AddressPrefixesText -replace '[s"]'}}
# now that we have equal looking objects, you can compare them
$result = Compare-Object -ReferenceObject $csvA.Network -DifferenceObject $csvB.Network -PassThru -IncludeEqual | ForEach-Object {
$where = switch ($_.SideIndicator) {
'==' { 'Both files' }
'<=' { "File $fileA" }
'=>' { "File $fileB" }
}
[PsCustomObject]@{
'Network' = $_
'WhereFound' = $where
}
}
# output on screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path 'E:Validationoutput.csv' -NoTypeInformation
屏幕上的输出:
网络在哪里找到 ------- ---------- 10.10.0.0/16 两个文件 10.4.0.0/16 两个文件 10.81.0.0/16 文件 E:\Validation\LocalGateway1.csv 10.82.0.0/16 文件 E:\Validation\LocalGateway1.csv 10.37.0.0/16 文件 E:\验证\ER1.csv 10.80.0.0/16 文件 E:\验证\ER1.csv