【问题标题】:How to deal with automated duplicate user removal如何处理自动重复用户删除
【发布时间】:2016-03-09 10:17:45
【问题描述】:

我有以下几点:

@(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'  
}    

我可以做些什么来确保所有用户都在那里,而不会删除任何原始文件,而只是删除重复文件?

【问题讨论】:

  • 它在哪里明确声明它创建了一个用户?它明确指出尝试创建用户时出错,因为用户已经存在,所以不,它没有创建用户。因此,$newusers1 为空,将其传递给其他命令是徒劳的。对于第二部分,将其更改为 Get-ADUser $username,我敢打赌,您在这部分会更幸运。
  • 原来的错误信息是不言自明的。您的 AD 中已经有一个对象 CN=Miss Dunning,dc=csi,dc=lab。仔细检查。此外,如果您不使用参数-PassthruNew-ADUser 不会返回用户对象,因此$newusers1 将始终为空。如需进一步帮助,您需要显示当前代码、完整的错误消息和示例输入数据。不要在 cmets 中发布该信息。改为编辑您的问题。
  • @AnsgarWiechers 对不起,总菜鸟。这次编辑了。 :)
  • 你的第一个代码 sn-p 完全坏了,不可能工作。而且我仍然没有看到连贯的错误消息或示例输入。如果您希望任何人解决您的问题:停止显示零碎的内容。
  • @AnsgarWiechers 尝试修复它和格式化,希望对您有所帮助:)

标签: csv powershell error-handling automation


【解决方案1】:

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

【讨论】:

  • 非常感谢 Ansgar,您的帮助很大,效果很好!还要感谢整个过程中的所有其他贡献者。 :D
【解决方案2】:

您可能想要使用 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 变量将为空。它不会自动与其他用户一起加载,如果这样做会很糟糕。您需要决定如果该帐户已经存在该怎么办,这可能只是使用另一个帐户加载变量,或者执行诸如将“1”附加到 $name 并重试命令之类的操作。

【讨论】:

  • 谢谢,我之前尝试过使用 try/catch,无论出于何种原因,PS 都会完全忽略我的 Try 块。但是再试一次也无妨。如果再次失败,我还能做什么?
  • 哦,还有,肯定没有连接到不同的 DC,因为只有 1 个,本地的。
  • 啊,尝试在命令末尾添加“-ErrorAction:Stop”以确保触发 Catch。
  • 谢谢,我会记住的。问题本身已解决,脚本功能齐全,但我保留这些注释以供参考。 :) 如果应该删除线程,请告诉我,或者在管理员离题时随意这样做......不确定它是如何工作的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 2010-11-24
  • 2014-09-30
  • 2015-06-04
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多