所以我有一个问题与下面提供的代码。 我有一个名为0010798M的.csv文件,其中我有3个文件,名称,姓氏,ou。从.csv到3个不同的Powershell脚本文件
在.csv文件的ou部分中,我有3个不同的名称为Account,Sales,Management。
我的代码是在这个环节上提供:https://pastebin.com/D0Naiccr
#Import the PowerShell module containing AD cmdlets
Import-Module ActiveDirectory
write-host "Start Process"
write-host "-------------------------------------"
try {
#Read the CSV file
$csvPath = "C:\0010798M.csv"
$csvData = import-csv $csvPath
write-host "Reading the CSV file......"
#Loop through all items in the CSV items
ForEach ($user In $csvData) {
$saMAccountName = $user.sAMAccountName
#Check if the User exists
$ADuser = Get-ADUser -LDAPFilter "(sAMAccountName=$saMAccountName)"
If ($ADuser -eq $Null) {
#Create user using New-ADUser cmdlet
$userPrincipalName = $user.sAMAccountName + "@adatum.com"
New-ADUser -Name $user.displayName `
-SamAccountName $sAMAccountName `
-UserPrincipalName $userPrincipalName `
-GivenName $user.givenname `
-Surname $user.sn `
-DisplayName $user.displayName `
-AccountPassword (ConvertTo-SecureString "Pa`$`$w0rd" -AsPlainText -Force) `
-PasswordNeverExpires $true `
-ChangePasswordAtLogon $false `
-Enabled $true
write-host "- " $user.sAMAccountName "| Account Created" -ForegroundColor green
} else {
write-host "- " $user.sAMAccountName "|Account Exists" -ForegroundColor yellow
}
}
} catch {
write-host "Error: " $($_.CategoryInfo) -ForegroundColor red
write-host "Message: " $($_.Exception.Message) -ForegroundColor red
}
write-host "-----------------------------------------------------------------"
write-host "End Process"
你有什么试过了,你试过的怎么都失败了?理想情况下,您应该提供您尝试过的[最小,完整和可验证的示例](http://stackoverflow.com/help/mcve),并且包含错误消息的具体信息以及错误消息和/或错误的输出。 SO不是代码写入服务;最好的问题是提供有用信息的问题,以便那些回答问题的人可以指导你设计自己的正确答案。参见[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)。 –
我已经创建了上述代码,但我不知道将用户分配给销售,管理或帐户的确切代码。 确切的错误是:错误:未指定:(:) [Get-ADUser],ADException 消息:无法识别搜索过滤器 –
'Get-ADUser'不会将用户分配给任何东西;它只是从Active Directory中检索用户信息。您收到的错误表明您的LDAPFilter存在问题;您需要更正此问题才能让您的电话正常工作。我会注意到,我很少 - 如果有的话 - 请参阅实际使用的'LDAPFilter';我发现以其他方式过滤更容易(显然,大多数人也是如此)。['Get-Help Get-ADUser'](https://technet.microsoft.com/zh-cn/library/ee617241.aspx)在这里会有所帮助。 –