I'd like to restart a remote computer that belongs to a domain. I have an administrator account but I don't know how to use it from powershell.
I know that there is a Restart-Computer
cmdlet and that I can pass credential but if my domain is for instance mydomain
, my username is myuser
and my password is mypassword
what's the right syntax to use it?
I need to schedule the reboot so I don't have to type the password.
I saw one example that uses Import/Export-CLIXML.
These are my favorite commands for the issue you're trying to resolve. And the simplest way to use them is.
So if the file doesn't locally exist it will prompt for the credentials and store them. This will take a
[pscredential]
object without issue and will hide the credentials as a secure string.Finally just use the credential like you normally do.
Note on Securty:
Securely store credentials on disk
Edit: Just reread the original question. The above will work so long as you've initialized the
[pscredential]
to the hard disk. That is if you drop that in your script and run the script once it will create that file and then running the script unattended will be simple.I have to run SCOM 2012 functions from a remote server that requires a different credential. I avoid clear-text passwords by passing the output of a password decryption function as input to ConvertTo-SecureString. For clarity, this is not shown here.
I like to strongly type my declarations. The type declaration for $strPass works correctly.
There is another way, but...
DO NOT DO THIS IF YOU DO NOT WANT YOUR PASSWORD IN THE SCRIPT FILE (It isn't a good idea to store passwords in scripts, but some of us just like to know how.)
Ok, that was the warning, here's the code:
$cred
will have the credentials from John Doe with the password "ABCDEF".Alternative means to get the password ready for use: