Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Csv 如何处理自动删除重复用户_Csv_Powershell_Error Handling_Automation - Fatal编程技术网

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

Csv 如何处理自动删除重复用户,csv,powershell,error-handling,automation,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\Desk

我有以下资料:

@(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:无法将输入对象绑定到命令的任何参数
要么是因为命令不接受管道输入,要么是因为输入及其
属性与接受管道输入的任何参数都不匹配。
在C:\Users\Administrator\Desktop\Team2.ps1:40 char:14
+$newusers1 | Get ADUser-Filter*-SearchBase“OU=活动用户,DC=csilab,DC=loca。。。
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:InvalidArgument:(CN=Bethanie Cut…csilab,dc=local:PSObject)[Get ADUser],ParameterBindingException
+FullyQualifiedErrorId:InputObject NotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
我还担心,即使它真的起作用,它也会删除唯一的用户,而不是重复的用户

get-AdUser $username |  Move-ADObject -TargetPath 'OU=Active Users,dc=csilab,dc=local'  
}    

如何确保所有用户都在那里,而不删除任何原件,只删除副本?

您可能需要使用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。它不会自动加载到其他用户,如果它加载了,那将非常糟糕。如果帐户已经存在,您需要决定怎么做,这可能只是将变量加载到其他帐户,或者执行类似于附加“1”的操作“转到$name并重试该命令。

您的
新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对象处理文件列表

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

它在哪里清楚地说明它创建了一个用户?它清楚地表明,尝试创建用户时出错,因为该用户已经存在,所以它没有创建用户。因此,
$newusers1
为空,将其传输到其他命令是徒劳的。对于第二部分,将其更改为
Get ADUser$username
,我敢打赌您在这一部分会更幸运。最初的错误消息是不言自明的。你的广告中已经有一个对象了。仔细检查一下。另外,如果不使用参数
-Passthru
,则
New ADUser
不会返回用户对象,因此
$newusers1
将始终为空。要获得进一步帮助,您需要显示当前代码、完整的错误消息和示例输入数据。不要在评论中发布这些信息。改为编辑您的问题。@AnsgarWiechers抱歉,完全没有问题。这次编辑了它。:)您的第一个代码片段已完全损坏,不可能工作。我仍然没有看到一致的错误信息或样本输入。如果您希望有人对您的问题进行故障排除:停止显示零碎内容。@AnsgarWiechers试图修复它并设置格式,希望有帮助:)谢谢,我以前尝试过使用try/catch,无论出于什么原因,PS都会完全忽略我的try块。不过,再试一次也无妨。如果再次失败,我还能做什么?哦,当然没有连接到不同的DC,因为只有1个本地DC。啊,试着在命令末尾加一个“-ErrorAction:Stop”,以确保捕获被触发。谢谢,我会记住这一点。问题本身已经解决,脚本功能齐全,但我保留这些注释以供参考。:)如果线程应该删除让我知道,或随时这样做,在管理离题。。。不知道它是怎么工作的非常感谢Ansgar,你帮了我很大的忙,它工作得很好!感谢整个过程中的所有其他贡献者:D