在PowerShell中从csv导入数据时,可以忽略具有相同名称和姓氏的行。是否可以忽略它们而不在AD中对其进行更新?
我宁愿收到一条消息
答案 0 :(得分:2)
这绝对是可行的!
首先,假设您有一个像这样的用户的起始列表
$csv = 'Name,Surname,ID
Stephen,Owen,1234
Lindey,Owen,2345
Stephen,Owen,3456
Pippin,Owen,4567
John,Elway,5678'
$users = $csv | ConvertFrom-Csv
为了知道我们是否已经处理过一个用户,我们需要一种在处理每个用户时对其进行跟踪的方法,因此,我们将在处理每个用户时将其添加到ArrayList中,就像这样。
$processed = New-Object System.Collections.ArrayList
ForEach ($user in $users){
#Do your processing here
$null = $processed.Add($user)
}
但是我们仍然需要一些逻辑来检查并查看我们之前是否曾在特定用户上工作过。为此,我们可以检查$processed
是否包含与我们目前正在使用的$user
相匹配的Name和SurName。
ForEach ($record in $processed){
if ($record.Name -eq $User.Name){
If ($record.SurName -eq $User.SurName){
Write-Warning "Skipping user $($user.Name) $($user.SurName), ID $($user.ID), already processed"
continue #This Keyword skips this item and continues to the next in the list
}
}
}
将它们放在一起,就可以了。
$users = $csv | ConvertFrom-Csv
$processed = New-Object System.Collections.ArrayList
ForEach ($user in $users){
ForEach ($record in $processed){
if ($record.Name -eq $User.Name){
If ($record.SurName -eq $User.SurName){
Write-Warning "Skipping user $($user.Name) $($user.SurName), ID $($user.ID), already processed"
continue
}
}
}
"importing $($user.Name) $($User.SurName)"
$null = $processed.Add($user)
}
将输出以下内容(请注意,列表中有“斯蒂芬·欧文”的两个条目)
importing Stephen Owen
importing Lindey Owen
WARNING: Skipping user Stephen Owen, ID 3456, already processed
importing Pippin Owen
importing John Elway
答案 1 :(得分:1)
我建议在导入的csv中添加一个新属性,使每个用户都能获得First和Surname的组合。从那时起,跳过具有相同全名的名称就足够了。 (请注意,我已经在那里接受了LotPings示例用户,以确保它能按预期工作)
$csv = 'Name,Surname,ID
Stephen,Owen,1234
Lindey,Owen,2345
Stephen,Owen,3456
Pippin,Owen,4567
John,Elway,5678
Lindey,Elway,9876
John,Elway,5555'
$users = $csv | ConvertFrom-Csv
# add a column with the first and lastnames combined
foreach ($user in $users) {
$user | Add-Member -MemberType NoteProperty -Name 'FullName' -Value ("{0} {1}" -f $user.Name, $user.Surname)
}
# now create an array with all users except those that have the same 'FullName'
$noDupes = @()
foreach ($user in $users) {
If ($user.FullName -in $noDupes.FullName){
Write-Warning "Skipping user $($user.FullName), ID $($user.ID), already processed"
continue
}
"importing $($user.Name) $($User.SurName)"
$noDupes += ($user)
}
$noDupes
将产生产量:
importing Stephen Owen
importing Lindey Owen
WARNING: Skipping user Stephen Owen, ID 3456, already processed
importing Pippin Owen
importing John Elway
importing Lindey Elway
WARNING: Skipping user John Elway, ID 5555, already processed
Name Surname ID FullName
---- ------- -- --------
Stephen Owen 1234 Stephen Owen
Lindey Owen 2345 Lindey Owen
Pippin Owen 4567 Pippin Owen
John Elway 5678 John Elway
Lindey Elway 9876 Lindey Elway
修改
如果您要跳过所有具有重复项(而不仅仅是重复项)的用户,则可以使用以下方式:
$csv = 'Name,Surname,ID
Stephen,Owen,1234
Lindey,Owen,2345
Stephen,Owen,3456
Pippin,Owen,4567
John,Elway,5678
Lindey,Elway,9876
John,Elway,5555'
$users = $csv | ConvertFrom-Csv
# add a column with the first and lastnames combined
foreach ($user in $users) {
$user | Add-Member -MemberType NoteProperty -Name 'FullName' -Value ("{0} {1}" -f $user.Name, $user.Surname)
}
# find all duplicate objects using the 'FullName'
$names = @()
$dupes = @()
foreach ($user in $users) {
If (-not ($user.FullName -in $names)) {
$names += ($user.FullName)
}
else {
$dupes += $user.FullName
Write-Warning "Duplicate user skipped: $($user.FullName)"
}
}
$users = ($users | Where-Object { $_.FullName -notin $dupes })
$users
将产生:
Name Surname ID FullName
---- ------- -- --------
Lindey Owen 2345 Lindey Owen
Pippin Owen 4567 Pippin Owen
Lindey Elway 9876 Lindey Elway
答案 2 :(得分:0)
或者您可以使用Sort -Unique:
$users | Select-Object -Property @{ Name = "FullName"; Expression = { $_.Name + $_.Surname }}, Name, Surname, ID | Sort-Object FullName -Unique
尽管这不会让您知道用户已被删除。