0

I am trying to import multiple user accounts from a csv file into powershell script. The script is running, but i do not see any added useres under the OU i created in AD. I do get an error message when running the script: "The seach filter cannot be recognized". 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

                    }    
        } 
5
  • You don't appear to be actually creating any ADUsers in this script. Commented Oct 12, 2017 at 13:30
  • 1
    I don't see a New-AdUser or anything in here? it also sounds like you're running the script wrong if you get the name printed back. Commented Oct 12, 2017 at 13:32
  • For creating users check this site: technet.microsoft.com/en-us/library/ee617253.aspx. For your apparent diacritic characters conversion issue, I would take a more common approach: stackoverflow.com/a/46660695/1701026 Commented Oct 12, 2017 at 14:03
  • Please dear OP, why do you share blank lines with us? Is this how you interpret a Minimal, Complete, and Verifiable example (sorry for my rudeness) Commented Oct 12, 2017 at 14:08
  • The script is now updated. I get the error: "The seach filter cannot be recognized" when running the script Commented Oct 12, 2017 at 14:19

2 Answers 2

0

You should not be using the -eq on the Get-Aduser line. 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. Doing it outside will only create the last user.

Sign up to request clarification or add additional context in comments.

Comments

0

With your current code, your variable $brukernavn that you use at line 41 (Get-ADUser) is an empty string. Fill it with data beforehand, like in the out commented line 24.

As an additional "error" that I see is the New-ADUser part. 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. The next line will throw a CommandNotFoundException. I'm personally not a huge friend of the backtick to shorten lines, a better way would be the usage of splatting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.