0

I am working on a script that takes a powershell variable and uses it in a script block running the net command.

Here is the code:

$User = "domain\user"
Invoke-Command -ComputerName $ComputerName -ScriptBlock { net localgroup administrators $User /add } -Credential $cred

When I run it, this is the error message I get:

The group already exists.
+ CategoryInfo          : NotSpecified: (The group already exists.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName        : GMCR77569

More help is available by typing NET HELPMSG 2223.

So it appears that the net command is trying to create the administrators group. I haven't found much on Google for that.

Also, I am using Invoke-Command because I need to be able to pass credentials.

I am sure this is something simple, but I cannot figure it out. Thank you in advance!

1

Because you are invoking a remote command, the script portion is trying to resolve the $User variable in the remote session. If you want to pass a variable, then you could modify your Invoke-Command with the -Args parameter. I think something like the following should work:

Invoke-Command -ComputerName $ComputerName -ScriptBlock { net localgroup administrators $args[0] /add } -Credential $cred -Args $User

This PowerShell Blog article does a good job explaining things and gives some other ways you could resolve your issue.

https://blogs.msdn.microsoft.com/powershell/2009/12/29/how-to-pass-arguments-for-remote-commands/

1
  • Awesome! Thank you very much. I should have realized the remote PC would be trying to resolve $User and not have any luck doing so. It's working great! Upvote and marked as answer!
    – Drew
    Feb 18 '16 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.