Using variable tor -path value

Gareth Davies 21 Reputation points
2020-08-14T17:33:31.42+00:00

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?

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,848 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2020-08-14T19:20:05.923+00:00

    First, remove the double-quotes from line 2. They're unnecessary.

    Second, remove the braces on lines 3 and 5. They're also unnecessary (at least in your example). If they're there because the New-ADUser is wrapped in a Try/Catch, you're missing the "-ErrorAction STOP" on the cmdlet.

    Third, you haven't shown what the content of the variable $locationentry is. Is it "Location" or "OU=Location"?

    Forth, if you want to see what's going on when that New-ADUser is running add "-Verbose" to the cmdlet. If you want to force it to die and throw an exception when there's an error add "-ErrorAction STOP" to the cmdlet. You can add both, too.

    0 comments No comments

  2. Gareth Davies 21 Reputation points
    2020-08-14T20:50:51.94+00:00

    Thank you, I was missing the OU= from $locationentry

    However, after fixing that it still doesn't work, adding verbose does not apear to have any effect, I still just see line 4 show up in the powershell window followed by the cursor on the next line.

    Note, if I remove the braces from lines 3-5 I get an error saying "New-ADUser : The object name has bad syntax
    17719-error.png

    adding the braces back results in the original behavior being seen again
    17765-2020-08-14-15-47-32-windows-powershell-ise.png


  3. Charles Gerard - Le Metayer 151 Reputation points
    2020-08-15T14:26:05.833+00:00

    Hello,

    1. As @Rich Matheisen noticed, looks like you have a missing space between -AccountPassword and the value return by the command line (Convert...)
    2. You are using the wrong variable for Path parameter. You are sending $OU instead of $Path
    3. Avoid using + symbol to make string concatenation. It will works for a lot of cases, but you can have sometimes bad surprise.
      You can try something like $Path = "{0},{1}" -f $location,$domain or $Path = "$location,$domain"
    4. Brackets on line 3 & 5 are creating a scriptblock which contains line 4, and it won't be executed as "." is missing before "{" (or not called in an invoke or start-process or equivalent).

  4. 2020-08-20T01:51:33.033+00:00

    Hi, given that this post has been quiet for a while, this is a quick question and answer. Has your question been solved? If so, please mark it as an answer so that users with the same question can find and get help.
    :)

    0 comments No comments