Powershell脚本文件从.csv到3个不同的ous

时间:2017-05-03 16:25:25

标签: powershell csv organizational-unit

所以我对下面提供的代码有疑问。 我有一个名为0010798M的.csv文件,其中有3个文件,名称为姓氏,ou。

在.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 个答案:

答案 0 :(得分:0)

查看您的脚本,我看不到您在命令上设置OU的位置。

您需要在命令中添加 -path 以创建新用户,并从CSV中提供OU的名称。

例如:

Height