提问人:SuperZee 提问时间:7/27/2023 更新时间:7/27/2023 访问量:21
比较大量 csv 文件,特别是每次调用两个 CSV 文件
Comparing a host of csv files - in particular two CSV files per call
问:
我已经构建了一个 Powershell 脚本,该脚本已找到并扩展为包含两个 CSV 文件的多个比较。
我是 PowerShell 的新手,我知道您可以执行 If 语句,但我还不知道如何使用它,所以我不得不逐行构建 PS 脚本,因此任何帮助将不胜感激;
# Set default vCenters
$VCServer1 = "Server1"
$VCServer2 = "Server2"
$VCServer3 = "Server3"
$VCServer4 = "Server4"
# Set the default export file path
$XlsxDir1 = "E:\$VCServer1\"
$XlsxDir2 = "E:\$VCServer2\"
$XlsxDir3 = "E:\$VCServer3\"
$XlsxDir4 = "E:\$VCServer4\"
# Set the default vSwitch file path
$vSwitchDir1 = "E:\$VCServer1\vSwitch"
$vSwitchDir2 = "E:\$VCServer2\vSwitch"
$vSwitchDir3 = "E:\$VCServer3\vSwitch"
$vSwitchDir4 = "E:\$VCServer4\vSwitch"
# Set the default dvSwitch file path
$dvSwitchDir1 = "E:\$VCServer1\dvSwitch"
$dvSwitchDir2 = "E:\$VCServer2\dvSwitch"
$dvSwitchDir3 = "E:\$VCServer3\dvSwitch"
$dvSwitchDir4 = "E:\$VCServer4\dvSwitch"
# Set the default vSwitch file
$vSwitch1Path = Get-Item -Path $vSwitchDir1\vSwitch_*.csv
$vSwitch2Path = Get-Item -Path $vSwitchDir2\vSwitch_*.csv
$vSwitch3Path = Get-Item -Path $vSwitchDir3\vSwitch_*.csv
$vSwitch4Path = Get-Item -Path $vSwitchDir4\vSwitch_*.csv
# Set the default dvSwitch file
$dvSwitch1Path = Get-Item -Path $dvSwitchDir1\dvSwitch_*.csv
$dvSwitch2Path = Get-Item -Path $dvSwitchDir2\dvSwitch_*.csv
$dvSwitch3Path = Get-Item -Path $dvSwitchDir3\dvSwitch_*.csv
$dvSwitch4Path = Get-Item -Path $dvSwitchDir4\dvSwitch_*.csv
# find the latest vSwitch file
$vSwitch1Lat = @(Get-ChildItem -Path $vSwitchDir1 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
$vSwitch2Lat = @(Get-ChildItem -Path $vSwitchDir2 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
$vSwitch3Lat = @(Get-ChildItem -Path $vSwitchDir3 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
$vSwitch4Lat = @(Get-ChildItem -Path $vSwitchDir4 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
# find the latest dvSwitch file
$dvSwitch1Lat = @(Get-ChildItem -Path $dvSwitchDir1 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
$dvSwitch2Lat = @(Get-ChildItem -Path $dvSwitchDir2 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
$dvSwitch3Lat = @(Get-ChildItem -Path $dvSwitchDir3 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
$dvSwitch4Lat = @(Get-ChildItem -Path $dvSwitchDir4 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1)
# find the older vSwitch file
$vSwitch1Old = @(Get-ChildItem -Path $vSwitchDir1 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
$vSwitch2Old = @(Get-ChildItem -Path $vSwitchDir2 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
$vSwitch3Old = @(Get-ChildItem -Path $vSwitchDir3 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
$vSwitch4Old = @(Get-ChildItem -Path $vSwitchDir4 -Filter 'vSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
# find the older dvSwitch file
$dvSwitch1Old = @(Get-ChildItem -Path $dvSwitchDir1 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
$dvSwitch2Old = @(Get-ChildItem -Path $dvSwitchDir2 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
$dvSwitch3Old = @(Get-ChildItem -Path $dvSwitchDir3 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
$dvSwitch4Old = @(Get-ChildItem -Path $dvSwitchDir4 -Filter 'dvSwitch_*.csv' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 -First 1)
# define output destinations
$outFilePaths = @{
vSwitch1 = "$vSwitchDir1\Changes.csv"
vSwitch2 = "$vSwitchDir2\Changes.csv"
vSwitch3 = "$vSwitchDir3\Changes.csv"
vSwitch4 = "$vSwitchDir4\Changes.csv"
dvSwitch1 = "$dvSwitchDir1\Changes.csv"
dvSwitch2 = "$dvSwitchDir2\Changes.csv"
dvSwitch3 = "$dvSwitchDir3\Changes.csv"
dvSwitch4 = "$dvSwitchDir4\Changes.csv"
# Latest = 'E:\TEMP\LatestChanges.csv'
}
# find all the subsequent versions of the file, make sure
# they're sorted by name, then store them in an array
$FileCount = $dvSwitch1Old
# make sure we've located at least one subsequent version - otherwise there's nothing to compare against!
if ($FileCount.Count -gt 0) {
# let's start by comparing original with the latest version
#
# $files[-1] is going to evaluate to the _last_ item in the $files array
$vSwitch1LatChanges = Compare-Object -ReferenceObject ($vSwitch1Lat |Get-Content) -DifferenceObject ($vSwitch1Old |Get-Content)
$vSwitch2LatChanges = Compare-Object -ReferenceObject ($vSwitch2Lat |Get-Content) -DifferenceObject ($vSwitch2Old |Get-Content)
$vSwitch3LatChanges = Compare-Object -ReferenceObject ($vSwitch3Lat |Get-Content) -DifferenceObject ($vSwitch3Old |Get-Content)
$vSwitch4LatChanges = Compare-Object -ReferenceObject ($vSwitch4Lat |Get-Content) -DifferenceObject ($vSwitch4Old |Get-Content)
$dvSwitch1LatChanges = Compare-Object -ReferenceObject ($dvSwitch1Lat |Get-Content) -DifferenceObject ($dvSwitch1Old |Get-Content)
$dvSwitch2LatChanges = Compare-Object -ReferenceObject ($dvSwitch2Lat |Get-Content) -DifferenceObject ($dvSwitch2Old |Get-Content)
$dvSwitch3LatChanges = Compare-Object -ReferenceObject ($dvSwitch3Lat |Get-Content) -DifferenceObject ($dvSwitch3Old |Get-Content)
$dvSwitch4LatChanges = Compare-Object -ReferenceObject ($dvSwitch4Lat |Get-Content) -DifferenceObject ($dvSwitch4Old |Get-Content)
# output to a file
$vSwitch1LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['vSwitch1'] -Width 200
$vSwitch2LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['vSwitch2'] -Width 200
$vSwitch3LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['vSwitch3'] -Width 200
$vSwitch4LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['vSwitch4'] -Width 200
$dvSwitch1LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['dvSwitch1'] -Width 200
$dvSwitch2LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['dvSwitch2'] -Width 200
$dvSwitch3LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['dvSwitch3'] -Width 200
$dvSwitch4LatChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $outFilePaths['dvSwitch4'] -Width 200
}
else {
Write-Host "Updated files are missing" -ForegroundColor White -BackgroundColor Red
}
我不太确定验证检查 $FileCount = $dvSwitch 1Old,但我知道这个确实有效,所以我知道脚本会继续,但理想情况下,我希望它检查每个旧 csv 文件为 vSwitch 和 dvSwitch。
另一件事是,一旦生成了比较文件,它就不包含任何标头,因此理想情况下,如果可能的话,我希望在 Changes.csv 文件中包含这些标头。
提前干杯
该代码确实有效,但是我相信它可以从经验丰富的 PS 脚本编写者那里完成更多的工作,并且当它尝试在开始下一个代码之前进行验证/预检查时。
答: 暂无答案
下一个:如何输入分母为两位数的分数
评论