Mr. Roboto
Showstopper: Round Two
These routines take last month's tool a few steps further.
- By Jeffery Hicks
- 06/01/2008
Last month, faithful reader Tom wanted to identify all the services on a remote computer that are configured to start automatically, but for one reason or another had stopped running.
Why not let PowerShell do it? Take advantage of the PowerShell pipeline and construct an expression like this to find services that should be running but aren't, and then attempt to start them:
PS C:\> Get-WmiObject
win32_service -filter
"startmode='auto' AND state
!='running'"" -computer "APP
SRV23" | foreach {$_.StartService()
| Select ReturnValue}
I'm piping the expression to the ForEach cmdlet, which acts on each object it receives. In this case, it calls the StartService() method. The $_ represents the current object in the pipeline, which in my example is the Spooler service. This command will write another object to the pipeline, but all we need is the ReturnValue property. If the command was successful, you'll see a ReturnValue of 0. You may have to scroll to the right of your PowerShell window to see the value.
What if there are multiple services that must be started? How does this work when run against a list of servers? We're not using a cmdlet to start the service, so we don't have common parameters such as -Confirm, but we can achieve the same result. Here's a step in that direction:
$c=$env:computername
Get-WmiObject win32_service -filter
"startmode='auto' AND state !='running'"
-computer $c | foreach {
$confirm=Read-Host "Do you want to
start" $_.Name service "on $c [YN]"
If ($confirm -eq "Y") {
Write-Host "Starting" $_.Name
service "on $c"
$r=($_.StartService()).ReturnValue
if ($r -eq 0) {
Write-Host "Successfully started"
$_.Name service "on $c." `
-ForegroundColor Green
}
else {
Write-Host "Failed to start"
$_.Name service "on $c. Return
Value=$r" `
-ForegroundColor Red
}
}
}
Now for every stopped service, you'll be asked if you want to restart the service using the Read-Host cmdlet. Answer "Y" to do so. When the service starts, you'll get a confirmation message in green-if it doesn't, you'll get a red failure message.
Stop the Presses
Last month, I also showed you a filtering function called Show-Stopped that you could use like this:
PS C:\>
"serverA","serverB","serverC" | show-stopped | format-table
SystemName,Name,Displayname,State,
Pathname,StartName -autosize
Originally, I thought I could take the above code and write a second function to take the output object of Show-Stopped and start each service. Instead, I blended the two functions into one and called it Get-StoppedService. Because of some limitations with PowerShell version 1.0 with Windows Management Instrumentation (WMI), you can't use alternate credentials with this function. You'll need to make sure you're running it with full administrative rights.
You can pipe one or more computer names to the function without any parameters to get the same result you got with Show-Stopped:
PS C:\>
"serverA","serverB","serverC" | get-
stoppedservice | format-table Sys-
temName,Name,Displayname,State,
Pathname,StartName -autosize
Perhaps you'd like to export the information for offline analysis:
PS C:\> get-content servers.txt |
get-stoppedservice | export-clixml
stopped-services.xml
Now you can specify -start as a parameter and you'll be prompted to see if you want to start each stopped service:
PS C:\> get-content servers.txt |
get-stoppedservice -start
The script files won't do anything alone. You'll need to copy and paste the functions into your PowerShell session, dot source the scripts to load the functions, or create a new script that includes these functions. Now you and Tom have a choice of tools and I hope you continue to see the benefit of PowerShell.
Download Mr. Roboto's Show-Stopped function at jdhitsolutions.com/scripts.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.