csv - 如何处理自动重复用户删除

标签 csv powershell error-handling automation

我有以下内容:

@(Import-Csv C:\Users\Administrator\Desktop\dbs\Monday.csv) +
    @(Import-Csv C:\Users\Administrator\Desktop\dbs\Tuesday.csv) +
    @(Import-Csv C:\Users\Administrator\Desktop\dbs\Wednesday.csv) +
    @(Import-Csv C:\Users\Administrator\Desktop\dbs\Thursday.csv) +
    @(Import-Csv C:\Users\Administrator\Desktop\dbs\Friday.csv) |
  sort first_name,last_name,phone1 -Unique | 
  Export-Csv C:\Users\Administrator\Desktop\dbs\joined.csv

Import-Module ActiveDirectory

#EDIT PATH SO IT POINTS TO DB FILE \/  
$newUserList = Import-Csv C:\Users\Administrator\Desktop\dbs\joined.csv

ForEach ($item in $newUserList){ 
  $fname = $($item.first_name)
  $lname = $($item.last_name)
  $phone = $($item.phone1)

  $username=$fname+$lname.substring(0,1)

  # Puts Domain name into a Placeholder.
  $domain='@csilab.local'

  # Build the User Principal Name Username with Domain added to it
  $UPN=$username+$domain

  # Create the Displayname
  $Name=$fname+" "+$lname

  $newusers1 = (New-ADUser -GivenName $fname -Surname $lname -HomePhone $phone -Name $Name -DisplayName  $Name  -SamAccountName $username -AccountPassword (ConvertTo-SecureString "1NewPassword" -asplaintext -force) -ChangePasswordAtLogon $true -UserPrincipalName $UPN -Path "ou=test,dc=csi,dc=lab" -Enabled $true -PassThru) | 

  # I need this block to check for duplicates missed by the csv sort & merge
  # as well as any in the destination OU itself as the script will run daily
  # with inevitable possibility that user is unique to the csv but not the AD.
  $newusers1 | Get-ADUser -Filter * -SearchBase "OU=Active Users,DC=csilab,DC=local" |
    Sort-Object -Unique |
    Remove-ADUser -confirm:$false 

但是我运行它并得到:

Get-ADUser : The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At C:\Users\Administrator\Desktop\Team2.ps1:40 char:14
+ $newusers1 | Get-ADUser -Filter * -SearchBase "OU=Active Users,DC=csilab,DC=loca ...
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (CN=Bethanie Cut...csilab,dc=local:PSObject) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

我也担心即使它确实可以删除唯一用户而不是重复用户。
get-AdUser $username |  Move-ADObject -TargetPath 'OU=Active Users,dc=csilab,dc=local'  
}    

我该怎么做才能确保所有用户都在那里,而不会删除任何原件,只删除重复项?

最佳答案

New-ADUser语句的末尾,您仍然有一个空管道,这将导致脚本失败,并显示“不允许使用空管道元素”错误,但是好吧...

为避免冲突,只需在尝试创建帐户之前检查该帐户是否已经存在,并仅在不存在的情况下创建它:

$username = $fname + $lname.substring(0,1)
...
if (Get-ADUser -Filter "SamAccountName -eq '$username'") {
  Write-Host "Account $username already exists."
} else {
  New-ADUser -SamAccountName $username -Name $Name ... -PassThru
}

另外,您也使CSV处理过于复杂。只需通过ForEach-Object处理文件列表:
$domain = '@csilab.local'

Set-Location 'C:\Users\Administrator\Desktop\dbs'

'Monday.csv', 'Tuesday.csv', 'Wednesday.csv', 'Thursday.csv', 'Friday.csv' |
  ForEach-Object { Import-Csv $_ } |
  Sort-Object first_name, last_name, phone1 -Unique |
  ForEach-Object {
    $fname = $_.first_name
    $lname = $_.last_name
    $phone = $_.phone1
    ...
  }

关于csv - 如何处理自动重复用户删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34089070/

相关文章:

list - 如何检索计算机的主要用户列表

html - HTTP 错误,我可以返回 HTML 以外的数据吗?

regex - 如何使用 pprof 优化 CSV 加载器?

c# - 有没有办法从 StreamReader 中删除第一行

postgresql - 将 csv 文件导入 psql 数据库

regex - Powershell正则表达式组替换

csv - 将 CSV 文件读取到 Spark 时出现问题

powershell - 按州和姓名找工作?

python - 测量 `UnboundLocalError`的时间时出现 `torch.where`错误

php - 我该如何处理错误消息