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

时间:2015-12-04 13:04:15

标签: 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'  
}    

我该怎么做才能确保所有用户都没有删除任何原件,只有重复?

2 个答案:

答案 0 :(得分:0)

您可能想要使用Try / Catch:

Try {$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"dc=csi,dc=lab"-Enabled$true)}
Catch {
    If ($_.Exception.Message -match "account already exists")
    {
        #do whatever here, eg $NewUsers1 = Get-ADUser $Name
    }
}

此外,如果您在通过ADUC浏览时看不到用户,则可能是您已连接到其他DC。

如上所述,如果命令失败,newuser1变量将为null。它不会自动加载其他用户,如果确实如此,那就太可怕了。如果帐户已经存在,您可能需要决定该怎么做,可能只是将变量加载到另一个帐户,或者做一些事情,例如在$ name上附加“1”并重试命令。

答案 1 :(得分:0)

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
    ...
  }