Ezine 191 - PowerShell's Restart-ComputerPowerShell Restart-ComputerStop Press: I forgot to mention that you need PowerShell v 2.0 for Restart-Computer. PowerShell v 2.0's Restart-Computer is very similar to the operating system's built-in shutdown /r command. A likely scenario is that you wish to automate the reboot of a local or remote server. With a tiny substitution to the cmdlet's verb you could change the command so that it simply shutdown the computer. Topics for PowerShell Restart-ComputerThis Week's SecretI have conflicting emotions about PowerShell's Stop-Computer cmdlet. On the one hand I want to use this simple command to encourage people to abandon old commands in favour of PowerShell; on the other hand I am disappointed that stop-Computer, and its sister cmdlet restart-Computer, are not as versatile as shutdown.exe. Just because I rarely use most of the extra switches in old shutdown does not alleviate my irritation that newer PowerShell commands are not both backward compatible and better. Restart-Computer and Stop-ComputerRestart-Computer is handy for situations where you wish to reboot not just one server, but a whole bunch. However, let us begin by investigating which PowerShell cmdlets contain the noun 'computer'. get-Command -noun computer Researching Restart-Computer Before we create a working example, as with any new PowerShell cmdlet, it's worth calling for help so that we can check the syntax and examine the parameters for restart-Computer get-Help restart-Computer -full Note 1: Because I like to inspect the examples, I rarely use get-Help without appending the -full switch. Two interesting parameters are -force and -credential. It's also worth highlighting that restart-Computer uses WMI, hence there are possible firewall restrictions on this command. Note 2: In the case of restart-Computer, I can see many opportunities to add the -confirm switch. Admittedly I reached this conclusion only after I had shot myself in the foot, and downed my local machine instead of the network server I was aiming at. Simple Example of Restart-ComputerFor once it's not easy to test this command on the local computer, unless you add the -confirm switch. As I will explain later, if you specify a network computer with restart-Computer you are probably going to have to disable the firewalls. restart-Computer -computerName LittleServer Example of Restart-Computer on Multiple Servers $Victims ="BigServer, LittleServer, GnomeServer" Note 1: This example reboots multiple computers, the names of which are stored in a variable called $Victims. You could extend this idea and employ get-Content to read the names of the servers stored in a text file. Incidentally, I have shortened the parameter -computerName to -computer, in PowerShell you can shorten parameters so long as the truncated word is unique and unambiguous. Problems with Restart-Computer
Summary of Windows PowerShell Restart-ComputerRestart-Computer is very similar to the old shutdown command which is built-in to generations of Windows operating systems. The advantage of restart-Computer is that it's simpler than shutdown, yet offers the ability to reboot a list of servers. My reason for featuring it is to give people more reasons for abandoning DOS and at least experimenting with PowerShell cmdlets. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever See more Microsoft PowerShell tutorials• Windows PowerShell Home • Introduction • Cmdlets • Exchange 2007 • Profile.ps1 • $_.Pipeline If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit. *
| |||||