While running remoting commands like Enable-PsSession
, Invoke-command
etc. we need to provide credentials with those commands.
I don't want to provide the credentials every time while executing these command.
Also lets say I stored the username in variable & using the variable while executing the command. I want to do this for the password as well. Could I do that ?
Eg:
Invoke-Command -ComputerName mycomputer -ScriptBlock { Get-ChildItem C:\ } -credential mydomain\administrator
So here I am providing the password everytime while executing these command.
How should the commands take username & password automatically either from variable & some other mechanism ?
You can do:
$cred = get-credential #fill you credential in the pop-up window
and then:
Invoke-Command -ComputerName mycomputer -ScriptBlock { Get-ChildItem C:\ } -credential $cred
Remember that the password in $cred
is easily recoverable in clear text!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With