0

I was hoping someone could help me out here. I have a csv file I'm trying to use to make new AD User accounts. The csv looks like this.

"ID","DEPT","FN","LN","BDATE","GRD"
"111111","1480","Test","HS","19980601","2018"
"222222","1479","Test","Elem","19980522","2025"

When running the below code I'm getting an error about 'Path' is null or empty. Provide an argument that is not null or empty.

Any help would be appreciated.

# Import active directory module for running AD cmdlets
 Import-Module activedirectory

 #Store the data from ADUsers.csv in the $ADUsers variable
 $ADUsers = Import-csv adusers.csv

 #Store report in log file in the $log variable
 $log = "log.txt"

 #Set Additional Variables
 $Password = (ConvertTo-SecureString -AsPlainText "$User.BDATE" -Force)
 $DisplayName = "$User.FN+ ' ' + $user.LN"
 $SCHID = $User.DEPT

 # Choose OU

Switch ($SCHID)
{
    "1480" {$OU = 'OU=students,OU=users,ou=hs,dc=clasd,dc=net'}
    "1479" {$OU = 'OU=students,OU=users,ou=elem,dc=clasd,dc=net'}
    "1480" {$Folder = '\\hs-ss\students\hs'}
    "1479" {$Folder = '\\hs-ss\students\elem'}
}


#Create Hash Table for New User Creation

 $ADUsers = @{

'SamAccountName' = "$User.ID"
'UserPrincipalName' = "$User.ID + '@clasd.net'"
'GivenName' = "$User.FNAME"
'SurName' = "$User.LNAME"
'EmailAddress' = "$User.ID = '@clasd.net'"
'Path' = "$OU"
'Department' = "$User.GRD"
'Company' = "$User.DEPT"
'AccountPassword' = $Password
'ChangePasswordAtLogon' = $true
'Enabled' = $true
'DisplayName' = "$DisplayName"
'Name' = $Displayname
'Homedrive' = "Z"
'Homedirectory' = "$Folder\'$User.ID'"
}

#Call New-ADUser with the parameters Above
Foreach ($User in $ADUsers) {
New-ADUser @ADUsers}

Error code is below

New-ADUser : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At C:\Users\jmerwin\Desktop\testUser.ps1:49 char:12 + New-ADUser @ADUsers} + ~~~~~~~~ + CategoryInfo : InvalidData: (:) [New-ADUser], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser

1
  • 1
    Please paste the exact text of the error message you are getting into your question. It identifies which line is throwing the error, and we need that information to be able to assist you further. Commented May 25, 2017 at 16:57

1 Answer 1

1

First, as the @TheMadTechnician noted, your loop and your CSV were being saved in the same variable. Even further you are going to want to loop for each user in the CSV. This makes it so that $user is defined when you are looking for properties.

Rather than define a couple variables like $password early, I moved them into where they were being called like the parameter hashtable for clarity.

Next your Switch block I combined the duplicated statements.

Lastly, in your parameter block as @Swonkie noted when you put a $variable.property into double quotes, when the variable will be expanded .property appended rather getting just the value of the property. To evaluate inside of double quote you can use a subexpression $() This will evaluate all of what inside then build the string. But if a property is already a string, then you can just leave the "" off.

$ImportedCSV = Import-csv adusers.csv
$log = "log.txt"

ForEach ($User in $ImportedCSV) {
    Switch ($User.DEPT) {
        "1480" {
            $OU = 'OU=students,OU=users,ou=hs,dc=clasd,dc=net'
            $Folder = '\\hs-ss\students\hs'
        }
        "1479" {
            $OU = 'OU=students,OU=users,ou=elem,dc=clasd,dc=net'
            $Folder = '\\hs-ss\students\elem'
        }
    }

    $ADUserParams = @{
        'SamAccountName' = $User.ID
        'UserPrincipalName' = "$($User.ID)@clasd.net"
        'GivenName' = $User.FNAME
        'SurName' = $User.LNAME
        'EmailAddress' = "$($User.ID)@clasd.net"
        'Path' = $OU
        'Department' = $User.GRD
        'Company' = $User.DEPT
        'AccountPassword' = (ConvertTo-SecureString -AsPlainText $User.BDATE -Force)
        'ChangePasswordAtLogon' = $true
        'Enabled' = $true
        'DisplayName' = "$($User.FN) $($User.LN)"
        'Name' = "$($User.FN) $($User.LN)"
        'Homedrive' = "Z"
        'Homedirectory' = "$Folder\$($User.ID)"
    }
    New-ADUser @ADUserParams
}

One other note, I question the wisdom of using birthdays as a password.

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

4 Comments

I'm using bdate as a password for students. The students are then forced to change it the first time they login.
@JustinMerwin Yes, just realize that this means that every student will know all the other students passwords before they are changed.
@BeH I do realize that. What would you recommend? I'm up for suggestions :-)
@JustinMerwin Providing whoever is assisting with onboarding the users with the list of randomized passwords to provide individually. They can then provide you with a list of accounts that did not get set up and if/when they will be used so you can disable accounts.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.