我有以下幾點:如何處理與自動重複的用戶去除
@(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」,我敢打賭,你有更多的運氣與該部分。 – TheMadTechnician
最初的錯誤信息是不言自明的。您的AD中已經有一個對象CN = Miss Dunning,dc = csi,dc = lab'。仔細檢查一下。另外,如果你不使用參數'-Passthru','New-ADUser'不返回用戶對象,所以'$ newusers1'將一直爲空。爲獲得進一步的幫助,您需要顯示當前的代碼,完整的錯誤消息和樣本輸入數據。不要在評論中發佈該信息。改爲編輯你的問題。 –
@AnsgarWiechers對不起,總noob。這次編輯它。 :) – noob