How to restart a remote system using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To restart the remote computer, you need to use the Restart-Computer command provided by the computer name. For example,

Restart-Computer -ComputerName Test1-Win2k12

The above command will restart computer Test1-Win2k12 automatically and if you have multiple remote computers to restart then you can provide multiple computers separated with comma (,).For example,

Restart-Computer -ComputerName Test1-Win2k12, Test2-Win2k12

Restart signal will be sent to both the computers at a time in the above example.

You can also use the Pipeline to restart remote computers. For example,

"Test1-Win2k12","Test2-Win2k12" | Restart-Computer -Verbose

OR

(Get-Content C:\Servers.txt) | Restart-Computer -Verbose

Few servers have dependencies on the other servers so main servers need to restart first. For example, AD and Exchange servers. If you are giving both the servers in the same command line then both servers will reboot at the same time and this is what we don't need it. To overcome this solution, you need to pass each server one at a time, write a few steps for server post-reboot checklist, and then move on to the next server. You can add servers to the array or text file and then pass a single value through foreach loop and the steps for the post-reboot checklist and move on to the next server.

People often confuse with the - Wait parameter in the Restart-Computer cmdlet that - Wait parameter will reboot one server at a time after the server post-reboot checklist completes but - Wait parameter only performs the three major checklists like WinRM, WMI and PowerShell connectivity check on the remote computer for each computer specified in the cmdlet after server comes up but it can't hold the execution of servers reboot.

Restart-Computer shoots reboot command on all the computers and - Wait parameter does the checks on each computer the specified tests and even if the remote computer failed in checks, servers will reboot.

When anyone is logged on to the remote server(s), PowerShell failed to restart the remote server and throws the below error message.

PS C:\Users\Administrator> Restart-Computer test1-win2k12 –Verbose
VERBOSE: Performing the operation "Enable the Remote shutdown access rights
and restart the computer." on target "test1-win2k12".
Restart-Computer : Failed to restart the computer test1-win2k12 with the
following error message:
The system shutdown cannot be initiated because there are other users logged
on to thecomputer.
At line:1 char:1+ Restart-Computer test1-win2k12 -Verbose+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (test1-win2k12:String) [Restart-
Computer], InvalidOperationException
+ FullyQualifiedErrorId :
RestartcomputerFailed,Microsoft.PowerShell.Commands.RestartComputerCommand

To restart the computer forcefully, you need to use -Force parameter.

Restart-Computer Test1-Win2k12 -Force
raja
Published on 24-Jul-2020 13:59:11
Advertisements