简体   繁体   English

使用Powershell在AD中添加多个用户不起作用

[英]Adding multiple users in AD with powershell not working

I am trying to import multiple user accounts from a csv file into powershell script. 我正在尝试从csv文件将多个用户帐户导入到powershell脚本中。 The script is running, but i do not see any added useres under the OU i created in AD. 该脚本正在运行,但是我在AD中创建的OU下看不到任何添加的用户。 I do get an error message when running the script: "The seach filter cannot be recognized". 运行脚本时,确实收到错误消息:“无法识别seach过滤器”。 What can be the error here? 这可能是什么错误?

Update: The script is now updated after the comment tips. 更新:注释提示后,脚本现已更新。

Import-Module activeDirectory
$csvpath = $PSScriptRoot + "\produksjonsbrukereImport.csv"

if (Test-Path $csvpath) {

    $csvpath = "C:\script\produksjonsbrukereImport.csv"
    $csv = Import-Csv -Delimiter "," -Path $csvpath -Encoding UTF7
    $OUBasePath =  ",OU=Produksjon,OU=OpMeis,DC=OpMeis,DC=local"
    $logpath = "$PSScriptRoot\import-brukere-loggfil.txt"

    }


    foreach ($line in $csv) {

        #Lagrer variabler
        $fornavn = $line.fornavn
        $etternavn = $line.etternavn
        #$navn = ($fornavn + " " + $etternavn)
        $beskrivelse = $line.beskrivelse

        $passord = $line.Passord
        $avdeling = $line.avdeling
        #$brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring(0,3)).tolower()


        $brukernavn = $brukernavn -replace "æ", "a"
        $brukernavn = $brukernavn -replace "å", "a"
        $brukernavn = $brukernavn -replace "ø", "o"

        $principal = $brukernavn + "@OpMeis.local"
        $profPath = ($profBasePath + $brukernavn)
        $profPathTrue = $profPath + ".V2"

        #Genererer OU-path basert på avdelingsnavn
        $OU = ("OU=" + $avdeling + $OUBasePath)


   }

        if (!(get-aduser -Filter {sAMAccountName -eq $brukernavn})) {

            $logg = "Forsøker å legge til bruker for $navn - brukernavn: $brukernavn, passord: $passord, avdeling: $avdeling"

            #Skriver til skjerm og lagrer i loggfil
            Write-Host $logg



            try {

                New-ADUser -Name $navn -GivenName $fornavn `
                -Surname $etternavn –DisplayName $navn `
                -sAMAccountName $brukernavn `
                -Description $beskrivelse -Path $OU `
                -AccountPassword (ConvertTo-SecureString $passord -AsPlainText -Force) -Enabled $true `
                -ChangePasswordAtLogon $true `
                -UserPrincipalName $principal `
               # -ProfilePath $profPath `
                -Passthru | Out-file -FilePath $logpath -Append


                Start-Sleep -Milliseconds 500


                ADD-ADGroupMember (“G_” + $line.avdeling) –members $brukernavn

                Start-Sleep -Milliseconds 100


                $logg | Out-File -FilePath $logpath -Append
                Write-Host "Bruker: $brukernavn lagt til i AD"

                $logg | Out-File -FilePath $logpath -Append
                Write-Host "Bruker: $brukernavn lagt til i AD"

            } #END TRY

            #Ved feil, skriv til loggfil
            catch {

                    $ErrorMessage = $_.Exception.Message | Out-File -FilePath $logpath -Append
                    $FailedItem = $_.Exception.ItemName | Out-File -FilePath $logpath -Append

                    }    
        } 

You should not be using the -eq on the Get-Aduser line. 您不应在Get-Aduser行上使用-eq You can simply use 您可以简单地使用

if (!(Get-ADuser -identity $brukernavn)) {
    # Create User command block
}

Also, you should do that inside the for loop rather than outside. 另外,您应该在for循环内而不是在外部进行。 Doing it outside will only create the last user. 在外面做只会创建最后一个用户。

With your current code, your variable $brukernavn that you use at line 41 (Get-ADUser) is an empty string. 使用当前代码,在第41行(Get-ADUser)使用的变量$ brukernavn是一个空字符串。 Fill it with data beforehand, like in the out commented line 24. 预先在其中填充数据,例如在注释行24中。

As an additional "error" that I see is the New-ADUser part. 我看到的另一个“错误”是New-ADUser部分。 The backtick is not a line continuation character but an escape character with this the next used character is escaped. 反引号不是换行符,而是转义字符,下一个使用的字符将被转义。 In your usage, this would the line break, as you have commented line 59 out your New-ADUser command will end at this point. 在您的用法中,这将导致换行,因为您注释了第59行,New-ADUser命令将在此处结束。 The next line will throw a CommandNotFoundException. 下一行将抛出CommandNotFoundException。 I'm personally not a huge friend of the backtick to shorten lines, a better way would be the usage of splatting . 我个人不是缩短行距的绝妙朋友,更好的方法是使用splatting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM