Reputation: 103
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
Upvotes: 0
Views: 5126
Reputation: 10044
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.
Upvotes: 1