2017-05-03 39 views
-1

所以我有一個問題與下面提供的代碼。 我有一個名爲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" 
+1

你有什麼試過了,你試過的怎麼都失敗了?理想情況下,您應該提供您嘗試過的[最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve),並且包含錯​​誤消息的具體信息以及錯誤消息和/或錯誤的輸出。 SO不是代碼寫入服務;最好的問題是提供有用信息的問題,以便那些回答問題的人可以指導你設計自己的正確答案。參見[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)。 –

+0

我已經創建了上述代碼,但我不知道將用戶分配給銷售,管理或帳戶的確切代碼。 確切的錯誤是:錯誤:未指定:(:) [Get-ADUser],ADException 消息:無法識別搜索過濾器 –

+0

'Get-ADUser'不會將用戶分配給任何東西;它只是從Active Directory中檢索用戶信息。您收到的錯誤表明您的LDAPFilter存在問題;您需要更正此問題才能讓您的電話正常工作。我會注意到,我很少 - 如果有的話 - 請參閱實際使用的'LDAPFilter';我發現以其他方式過濾更容易(顯然,大多數人也是如此)。['Get-Help Get-ADUser'](https://technet.microsoft.com/zh-cn/library/ee617241.aspx)在這裏會有所幫助。 –

回答

0

尋找你的劇本,我看不出你曾經設置的OU上的命令。

你需要一個-path添加到您的命令來創建新用戶,並從您的CSV提供OU的名字..

例如:

New-ADUser -Name "GlenJohn" -Path "OU=Sales" 
+0

將新用戶創建到「Try/Catch」塊中可能也更容易,更快速。嘗試創建帳戶,並只是捕獲錯誤。這樣,它是1 AD而不是2,但仍然有相同的結果。 (我沒有測試過,只是在這裏大聲思考) –

相關問題