【发布时间】: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。仔细检查。此外,如果您不使用参数-Passthru,New-ADUser不会返回用户对象,因此$newusers1将始终为空。如需进一步帮助,您需要显示当前代码、完整的错误消息和示例输入数据。不要在 cmets 中发布该信息。改为编辑您的问题。 -
@AnsgarWiechers 对不起,总菜鸟。这次编辑了。 :)
-
你的第一个代码 sn-p 完全坏了,不可能工作。而且我仍然没有看到连贯的错误消息或示例输入。如果您希望任何人解决您的问题:停止显示零碎的内容。
-
@AnsgarWiechers 尝试修复它和格式化,希望对您有所帮助:)
标签: csv powershell error-handling automation