【发布时间】:2016-07-13 06:13:16
【问题描述】:
我有 3 个包含用户信息的 CSV 文件。 CSV1 是所有非活动用户的“主”列表。 CSV2 是当前需要停用的用户列表,CSV3 是需要激活的用户列表。
我想要的是有一个 PowerShell 脚本,可以从另一个脚本(创建 CSV2/3 的那个)调用它来比较 CSV1/2 并将所有唯一记录写回 CSV1。然后我希望它比较 CSV1/3 并删除 CSV1 中存在于 CSV3 中的所有记录。 CSV2/3 可以每天更改,其中可能没有数据,除了标题。
有几个独特的字段,但我想比较“EmployeeID”。 所有 3 个 CSV 文件都有标头(所有的标头都相同,因此数据是一致的)。
到目前为止,我最终将记录从 CSV2 添加到 CSV1,但它添加了两个标题。
$ICM= Import-Csv inactiveicmaster.csv -Header 'StudentDistrictID', 'StudentSiteCode', 'StudentLastName', 'StudentFirstName', 'StudentGradeLevel', 'GraduationYr', 'Masterck', 'Homeroom', 'MiddleName', 'Birthday', 'Gender', 'Email'
$IC = Import-Csv csv\inactiveic.csv -Header 'StudentDistrictID', 'StudentSiteCode', 'StudentLastName', 'StudentFirstName', 'StudentGradeLevel', 'GraduationYr', 'Masterck', 'Homeroom', 'MiddleName', 'Birthday', 'Gender', 'Email'
$DIS = Import-Csv csv\disinad.csv -Header 'StudentDistrictID', 'StudentSiteCode', 'StudentLastName', 'StudentFirstName', 'StudentGradeLevel', 'GraduationYr', 'Masterck', 'Homeroom', 'MiddleName', 'Birthday', 'Gender', 'Email'
foreach ($f in $ic) {
$found = $false
foreach ($g in $icm) {
if ($g.StudentDistrictID -eq $f.StudentDistrictID) {
$found = $true
}
}
if ($found -eq $false) {
$icm += $f
if ($f.masterck -eq "") {
$f.masterck = "IM"
}
}
}
<#
foreach ($h in $dis) {
$found = $false
foreach ($g in $icm) {
if ($g.studentdistrictid -eq $h.studentdistrictid) {
$found = $true
}
if ($found -ne $false) {
#don't know what to do here to remove the duplicate
}
}
}
#>
$icm | select * | Export-Csv master.csv -NoTypeInformation
【问题讨论】:
-
要开始使用,请查看 Compare-Object cmdlet。
-
如果我从导入中删除 -Header,它只会在导出中添加一个。
标签: csv powershell