1

I have following command to add AD group in PowerShell script but AD group has space in name and that is causing an issue.

Set-Variable -Name APP_GROUP -Value My AD Group
Add-LocalGroupMember -Group "Administrators" -Member "MyDomain\"$APP_GROUP

Error: CategoryInfo: NotSpecified: RemoteException
+ FullyQualifiedErrorId : NativeCommandError

1 Answer 1

3

Quote it in single quotes, because it's a plain string with nothing else happening, and put the double quotes around the whole thing in the second line (double-quotes because it has a variable name in it which PowerShell needs to expand):

Set-Variable -Name APP_GROUP -Value 'My AD Group'
Add-LocalGroupMember -Group "Administrators" -Member "MyDomain\$APP_GROUP"

It would be more typical to see it written:

$APP_GROUP = 'My AD Group'
Add-LocalGroupMember -Group "Administrators" -Member "MyDomain\$APP_GROUP"
Sign up to request clarification or add additional context in comments.

5 Comments

I can put SD group in quote because its coming from other script and I can not change that script
@DharmendraSingh: if the name (with embedded spaces) stored in $APP_GROUP comes from another script, you don't need to quote it again, but if want to make the value part of a single argument to pass to Add-LocalGroupMember, you must make $APP_GROUP part of the double-quoted string, as this answer shows. Alternatively, omit double quotes altogether (which works in this case, but may not always): MyDomain\$APP_GROUP. Also note that there is no obvious connection between your command and your error message, given that Add-LocalGroupMember is not a native command.
@DharmendraSingh I don't understand; the code in your question shows you tying the group name in; how do you get the group name out of the other script?
@TessellatingHeckler I have one aws user data script which will create windows Ec2 instance and this script is part of user data and we are getting ad group name from variable teraform. example I have assigned ad group value in variable.sh file readonly APP_GROUP="My AD Group" and I get this value in UserData script like Set-Variable -Name APP_GROUP -Value My AD Group
I have not used teraform; that still is not clear to me, how does the text My AD Group get into the line Set-Variable -Name APP_GROUP -Value My AD Group in your script? Is teraform using your script as a template, and replacing part of it? If so, can you put quotes around where the value will appear like Set-Variable -Name APP_GROUP -Value 'TERAFORM_THING_HERE' ?

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.