Search code examples
windowspowershellcsvwindows-server-2016

Powershell New-ADUser Variable isnt working


Script

$ADUser = Get-ADUser
if($ADUser -ne "") {echo "there are existing ADUsers!"} #

$Zeilen = (Get-Content C:\Users\Administrator\Documents\users.csv  | Measure-Object –Line).Lines              #import file, count lines with Measure-Object(mo)(-> output object), ".Lines" takes just number from Measure-Object and convert it to a number
$Zeilen -= 1    # "-1" because 1. line = header without relevant information

$Ausgabe = Import-Csv C:\Users\Administrator\Documents\users.csv 

$password = "Ausbildung2020" | ConvertTo-SecureString -AsPlainText -Force      #is disabled without password

###################
#creates list with existing users

$Userlist = ""
$CsvData = (Get-ADUser).DistinguishedName | Select-Object -Skip 3 #removes first 3 Zeilen, which contains Admin, Guest und kgrtgt because the are irrelevant

$ZeilenCsvData = $CsvData.count   #line amount from (Get-ADUser).DistinguishedName

for($line = 0;$line -lt $ZeilenCsvData; $line++ ) #-lt oder -le ?
{ 
    $UserLine = $CsvData[$line]

    $user1 = $UserLine.Split(",")
    $user2 = $user1[0].Split("=")             
    $user3 = $user2[1]           #extract Username from String
    $UserList += $user3 + "`r`n" #paste Username from current Iteration to $UserList
}

###################
#check if ADUser exists

for ($loop = 0; $loop -lt $Zeilen; $loop++)  #-lt oder -le ?  #execute loop as often as lines in (Csv-file with (new) Users) exists
{
    for($i = 0; $i -lt $ZeilenCsvData ; $i++) #$ZeilenCsvData: number of lines from Get-ADUser without Admin, Guest und kgrtgt
    {   
        if($Ausgabe[$loop].Name -eq $UserList.[$i])       #!!line with problem!!
        { 
            $match = $true 
            break
        }
    }

    if($match -eq $true){ continue }    #ends Iteration and continue with new one if User exists
                                        
###################
#create User if it doesn't exist
    New-ADUser -Name $Ausgabe[$loop].Name `
               -GivenName $Ausgabe[$loop].GivenName `
               -Surname $Ausgabe[$loop].Surname `
               -City $Ausgabe[$loop].City `
               -AccountPassword $password `
               -path "OU=Benutzer,DC=dmamgt,DC=local" `

    Enable-ADAccount -Identity $Ausgabe[$loop].Name        #requirement: password matches standard
}

Problem

In the problem line the „ [$ “ marked red and I get those errors which make the script unexecutable

Task

I got the task to create a organizational unit called "Benutzer" in which I should create 20 Users with some properties like name, city,password, enabled,... which I imported from a csv file. But I have to check whether the user already exists if so the loop should go to the next user to create it.

Errors:

At C:\Users\Administrator\Documents\extended New-ADUser mit csv.ps1:36 char:48
+         if($Ausgabe[$loop].Name -eq $UserList.[$i])
+                                                ~
Missing type name after '['.
At C:\Users\Administrator\Documents\extended New-ADUser mit csv.ps1:36 char:47
+         if($Ausgabe[$loop].Name -eq $UserList.[$i])
+                                               ~
Missing property name after reference operator.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingTypename 

Method invocation failed because [System.Char] does not contain a method named 'Split'.
At C:\Users\Administrator\Documents\extended New-ADUser mit csv.ps1:23 char:5
+     $user1 = $UserLine.Split(",")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Solution

  • Make $UserList an array:

    $UserList = @()
    

    Add items like this:

    $UserList += $user
    

    And this is how you index an array (without the dot)

    $UserList[$i]
    

    There might be some other issues with your script too, I didn't read it fully, but I hope this will get you started.