I am tying to create a Powershell script to create user accounts, not a difficult task normally. However, I have been asked to create a GUI (already done) that allows the person creating the account to enter the users information and hit a button to create the account.
If I hard code the value for the -path attribute it works fine, but we have offices in multiple locations and each location has its own OU so I need to be able to use location as one of the items input by the service desk person creating the account and translate this to the path for the correct OU.
I have set it up using the entry typed in by the service desk user as a variable, the remainder of the path is set as another variable. I then use + to put the 2 variables together to create a third variable with the full path as the value
Using the get-variable cmdlet shows this has worked as intended, the full path is shown but when I put this third variable into my script as the -path value it fails, the script looks like it executes but then it displays the code executed and does not create the account. No errors are displayed.
What am I missing here?
$OU= ",DC=domain,DC=com"
$Path= "$locationentry"+"$OU"
{
New-ADUser -Name "Test User6" -GivenName "Test" -Surname "User6" -SamAccountName "testuser6" -UserPrincipalName "testuser6@domain.com" -Path $Path -AccountPassword(ConvertTo-SecureString "P@ssw0rd123" -AsPlainText -force) -Enabled $true -ChangePasswordAtLogon $true -title "test user"
}
The $locationentry is the variable coming from the GUI, if I replace this with a hard coded entry it does exactly the same as it does as listed here. If I remove the variables and just hard code the path it works exactly as intended but I can't use this in production. I just don't understand why the above code is not working, even entering the full path into the $path variable fails so it looks like it is the fact I am using a variable for -path is the issue even though everything I read says this should work.
Anyone have any ideas?