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.
The problem with
Get-Credential
is that it will always prompt for a password. There is a way around this however but it involves storing the password as a secure string on the filesystem.The following article explains how this works:
In summary, you create a file to store your password (as an encrypted string). The following line will prompt for a password then store it in
c:\mysecurestring.txt
as an encrypted string. You only need to do this once:Wherever you see a
-Credential
argument on a PowerShell command then it means you can pass aPSCredential
. So in your case:You may need a different
-Authentication
switch value because I don't know your environment.Solution
For Build Machines
In the previous code replace user name and password values by secret ("hidden from logs") environment variables of your build-machine
Test results by
and you'll see
Regarding storing credentials, I use two functions(that are normally in a module that is loaded from my profile):
And this one:
You use it like this:
If the credential file doesnt exist, you will be prompted the first time, at that point it will store the credentials in an encrypted string inside an XML file. The second time you run that line, the xmlfile is there and will be opened automatically.
Here are two ways you could do this, if you are scheduling the reboot.
First you could create a task on one machine using credentials that have rights needed to connect and reboot another machine. This makes the scheduler responsible for securely storing the credentials. The reboot command (I'm a Powershell guy, but this is cleaner.) is:
SHUTDOWN /r /f /m \\ComputerName
The command line to create a scheduled task on the local machine, to remotely reboot another, would be:
SCHTASKS /Create /TN "Reboot Server" /TR "shutdown.exe /r /f /m \\ComputerName" /SC ONCE /ST 00:00 /SD "12/24/2012" /RU "domain\username" /RP "password"
I prefer the second way, where you use your current credentials to create a scheduled task that runs with the system account on a remote machine.
SCHTASKS /Create /TN "Reboot Server" /TR "shutdown.exe /r /f" /SC ONCE /ST 00:00 /SD "12/24/2012" /RU SYSTEM /S ComputerName
This also works through the GUI, just enter SYSTEM as the user name, leaving the password fields blank.
Be very careful with storing passwords this way... it's not as secure as ...
why dont you try something very simple?
use psexec with command 'shutdown /r /f /t 0' and a PC list from CMD.