'Invoke-Command with -credentials

I want to invoke a command on a remote server, I do not want to have to put in the password to run the script. I've tried encrypting the password and storing it in a txt file.

 $username = "Mydomain\service.account"
 $password = cat C:\username-password-encrypted.txt | convertto-securestring
 $cred = new-object -typename System.Management.Automation.PSCredential - argumentlist $username, $password
 Invoke-command $cred -Computer myserver -scriptblock {param([string]$LocalUser); Add-PSSnapin Citrix* ; Get-BrokerSession -max 10000 | Where-Object brokeringusername -eq  "mydomain\$($LocalUser)" | Stop-BrokerSession} -ArgumentList $user

Here is the error I get

 Invoke-Command : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.
 At \\uncpath\citrix\Installation Media\Citrix\Ticketing_script\Ticketing_Script - Copy (3).ps1:70 char:1
+ Invoke-command $cred -Computer MyServer -scriptblock {param([s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Invoke-Command],   ParameterBindingException
+ FullyQualifiedErrorId :   PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

There has to be an easier way to run this command on myserver without having to put in the password every time.



Solution 1:[1]

You just have to specify the -Credential parameter:

 Invoke-command -Credential $cred -Computer myserver -scriptblock {param([string]$LocalUser); Add-PSSnapin Citrix* ; Get-BrokerSession -max 10000 | Where-Object brokeringusername -eq  "mydomain\$($LocalUser)" | Stop-BrokerSession} -ArgumentList $user

Solution 2:[2]

4 years later but here goes:

Frode F. had the right idea in the comments. Your third line has - argumentlist but it must be -ArgumentList, otherwise it will throw New-Object : A positional parameter cannot be found that accepts argument 'ArgumentList'.

Also, when I copy and run your exact code it does not throw the same error which leads me to believe that the code you're running is not the same as the one you've written here. Here are the different errors you've received and the reasons for them:

Incorrect syntax

A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.

This means that you've not specified the flag correctly, Powershell thinks you're using a flag or parameter called System.Management.Automation.PSCredential. You can simulate this by adding a hyphen like New-Object -TypeName - System.Management.Automation.PSCredential and see for yourself, so your syntax is wrong somewhere.

Incorrect password format

Cannot find an overload for "PSCredential" and the argument count: "2".

This means that the $Password is not in the correct format. You're importing a file called password-encrypted.txt but after this you pass it to ConvertTo-SecureString. Are you sure that the information in the password-encrypted.txt file is passed to both ConvertTo-SecureString and then to ConvertFrom-SecureString before you saved it, like so?

"MyPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\username-password-encrypted.txt"

For more information on how to deal with passwords in Powershell, see this post that goes into detail on how to do credentials in Powershell:

https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/

In conclusion

If you run the code you've provided here using both -ArgumentList (instead of - argumentlist) and -Credentials $creds like others have suggested it should run fine. You're most likely not running the code that you've provided here because with these two adjustments the code runs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Brandl
Solution 2