I'm working on a script to import users into Active Directory and it is working with no issue, all users are dropped into the respective OU that was written in the original script. I modified the script to add an array with job titles and added an if statement to place the new users into different OU's based on their job titles but receive an error that it cannot validate the argument on parameter 'Path'. I an not sure if the code it correct or how to go about referencing PATH. I don't really know the best method for this and i may be approaching it completely wrong but below is my current script. Its pretty basic to get the users into AD but this addition is making me scratch my head. Any help would be much appreciated.
Import-Module ActiveDirectory
#Enter a path to your import CSV file
$ADUser = Import-csv C:\ADS\NewHiresRev1.csv
$OU = "JOB TITLE 1","JOB Title 2","JOB Title 3"
$Domain = "mydomain.com"
$Username = $user.FirstName + "." + $user.LastName
$Password = "Welcome"+$user.employeenumber+"!"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$GivenName = $user.firstname
$Surname = $user.lastname
$Name = $user.firstname + " " + $user.lastname
$DisplayName = $user.firstname + " " + $user.lastname
$Description = $user.job
$UserPrincipalName = $user.firstname + "." + $user.lastname + "@" + $Domain
$Enable = $true
$ChangePasswordAtLogon = $false
$Organization = "ScriptedUsers"
$Department = "Operations"
$Office = $user.job
$Company = "Company Inc."
$Manager = "Manager Name"
$StreetAddress = ""
$City = ""
$State = ""
$PostalCode = ""
$Country = "US"
#Check if the user account already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, output a warning message
Write-Warning "A user account $SamAccountName has already exist in Active Directory."
}
else
{
function identifyOU() {
foreach ($User in $ADUser){
if($user.job -contains $OU) {
$UsersOU = "OU=MDaemon Users,DC=ad,DC=cg-idd,DC=com"
}
else {"OU=Company Users Users,DC=ad,DC=cg-idd,DC=com"}
}
}
#If a user does not exist then create a new user account
#Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
New-ADUser `
-Path $UsersOU `
-SamAccountName $Username `
-AccountPassword $secpasswd `
-GivenName $GivenName `
-Surname $Surname `
-Name $Name `
-DisplayName $DisplayName `
-Description $Description `
-UserPrincipalName $UserPrincipalName `
-Enabled $True `
-ChangePasswordAtLogon $False `
-Organization $Organization `
-Department $Department `
-Office $Office `
-Title $Title `
-Company $Company `
-StreetAddress $StreetAddress `
-City $City `
-State $State `
-PostalCode $PostalCode `
-Country $Country `
}