2015-12-04 15 views
-2

我有以下幾點:如何處理與自動重複的用戶去除

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

我該怎麼做才能確保所有用戶在那裏沒有任何原件被刪除,只是重複?

+1

它在哪裏明確說明它創建了一個用戶?它清楚地表明,嘗試創建用戶時出現錯誤,因爲用戶已經存在,所以不會創建用戶。因此,'$ newusers1'爲空,並將其管道傳送給其他命令是徒勞的。對於第二部分,將其更改爲「Get-ADUser $ username」,我敢打賭,你有更多的運氣與該部分。 – TheMadTechnician

+0

最初的錯誤信息是不言自明的。您的AD中已經有一個對象CN = Miss Dunning,dc = csi,dc = lab'。仔細檢查一下。另外,如果你不使用參數'-Passthru','New-ADUser'不返回用戶對象,所以'$ newusers1'將一直爲空。爲獲得進一步的幫助,您需要顯示當前的代碼,完整的錯誤消息和樣本輸入數據。不要在評論中發佈該信息。改爲編輯你的問題。 –

+0

@AnsgarWiechers對不起,總noob。這次編輯它。 :) – noob

回答

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

非常感謝Ansgar,你一直是一個主要幫助,這工作完美!感謝整個過程中的所有其他貢獻者。 :d – noob

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將爲空,如果命令失敗。它不會自動加載其他用戶,如果它真的會很糟糕。如果賬戶已經存在,您需要決定該做什麼,可能只需將該變量加載到其他賬戶,或者執行諸如向$ name追加「1」並重試該命令之類的操作。

+0

謝謝,我已經使用try/catch語句之前以任何理由PS完全忽略了我的Try塊嘗試。然而,再試一次也無妨。如果再次失敗,我還能做什麼? – noob

+0

哦,絕對沒有連接到不同的DC,因爲只有1,本地的。 – noob

+0

啊,試着在命令末尾加上「-ErrorAction:Stop」來確保Catch被觸發。 – Guppy