0
votes

I am trying to bulk-add users into my Active Directory, but I am getting expression errors right at the start of the script. I am not a script buff at all, so I am really out of ideas from the get go.

$Users = Import-Csv ".\UsersFile.csv"
foreach ($User in $Users)
{
        -OrganizationalUnit $User.OU 
        -SamAccountName $User.UserName 
        -userPassword $User.Password 
        -GivenName $User.First 
        -Initials $USer.Initial 
        -sn $User.Last 
        -Displayname $User.DisplayName
        -Description $User.Description
        -Physicaldeliveryofficename $User.Office 
        -TelephoneNumber $User.Tel
        -Mail $User.mail 
        -streetaddress $User.Street 
        -postOfficeBox $User.Postbus
        -l $User.Location 
        -st $User.Provincie 
        -postalCode $User.Postcode 
        -c $User.Land 
        -deparment $User.Department
        -Company $User.Organisatie
        -Manager $User.Manager
        -Password $User.Password -ResetPasswordOnNextLogon $false 
}

The error log.

Missing expression after unary operator '-'. At C:\Users\Administrator\Desktop\CreateUserBulk.ps1:4 char:10 + - <<<< OrganizationalUnit $User.OU ` + CategoryInfo : ParserError: (-:String) [], Parseexception + FullyQualifiedErrorID : MissingExpressionAfterOperator


After trying the link (From serv) and editing the CSV and script accordingly, getting a lot more errors now with this.

Import-Csv : Cannot open file "C:\Users\administrator\UsersFile.csv". At C:\Users\administrator\Desktop\Untitled3.ps1:2 char:20 + $Users = Import-Csv <<<< -Delimiter ";" -Path ".\UsersFile.csv" + CategoryInfo : OpenError: (:) [Import-Csv], FileNotFoundException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand You cannot call a method on a null-valued expression. At C:\Users\administrator\Desktop\Untitled3.ps1:9 char:53 + $FirstLetterFirstname = $UserFirstname.substring <<<< (0,1) + CategoryInfo : InvalidOperation: (substring:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null. At C:\Users\administrator\Desktop\Untitled3.ps1:11 char:195 + New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $Detailedname -GivenName $user.firstname -Surname $user.name -AccountPassword (ConvertTo-SecureString <<<< $Password -AsPlainText -Force) -Ena bled $true -Path $OU + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSe cureStringCommand


EDIT2: Fixed, I changed the source link to it's fullness and now it works!

1
and what is the delimiter in your csv source file?Marco
Added, the delimiter is a semicolondeknegt
The first error is easy: The path to the csv is incorrect. The second error says: the variable $password is null.Marco
Glad I could help ;) It's always the little things.Marco

1 Answers

0
votes

You are not creating a new AD user in your script. So -OrganizationalUnit should be undefined and throw an error, excpet you declare them as a variable first.

Import-Module ActiveDirectory 
$Users = Import-Csv -Delimiter ";" -Path ".\UsersFile.csv"  
foreach ($User in $Users)  
{  
    $FirstLetterFirstname = $User.firstname.substring(0,1)     

    New-ADUser -Name $User.firstname + " " + $User.name 
    -SamAccountName $FirstLetterFirstName + $User.name 
    //... and so on
} 

You can declare all variables first in the manner of $FirstletterFirstName and execute the New-ADUser command at the end of each loop, which makes it easier to read and modify later on.

The important part stays: Adding a new user is executed through the New-ADUser command which you are missing. You can also add the New-ADUser to the top of your loop, which should make your query work if there are no other syntax errors / Spelling errors in your code

//EDIT: You can find a working example at http://gallery.technet.microsoft.com/scriptcenter/ed20b349-9758-4c70-adc0-19c5acfcae45

and the TechNet article for New-ADUser: http://technet.microsoft.com/en-us/library/ee617253.aspx