# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2007 # # NAME: Get-StoppedService.ps1 # # AUTHOR: Jeffery Hicks , SAPIEN Technologies # DATE : 4/13/2008 # # COMMENT: This function is meant to participate in the pipeline. he function # will also work for the local computer by simply running 'Get-StoppedService' with # no arguments. The function will return a WMI Win32_Service object. The # Get-Wmiobject filter is designed to return all service objects set to # automatically start that are not running. # A text file of all computer names that can't be checked will be created. # You can edit the function if you want to change the name and/or location # of the log file. # By default, the function will only list services that should be running. # If you include the -start parameter, each service will be started. You # will be prompted if you want to start each service. There is a -noprompt # parameter that you can also specify to disable any confirmation prompting. # If you use -start nothing will be written out to the pipeline. # You can dot source this script file, copy and paste the function into # your Powershell profile or copy and paste it into a PowerShell session. # !!! This function DOES NOT support alternate credentials!!! # EXAMPLES: # "serverA","serverB","serverC" | get-stoppedservice | format-table SystemName,Name,Displayname,State,Pathname,StartName -auto # get-content servers.txt | get-stoppedservice | Format-List SystemName,Name,State # get-content servers.txt | get-stoppedservice | export-clixml stopped.xml # get-content servers.txt | get-stoppedservice -start # # "serverA","serverB","serverC" | get-stoppedservice -start -noprompt # # ============================================================================================== Function Get-StoppedService { Param([switch]$start=$false,[switch]$noprompt=$false) BEGIN { #change $DebugPreference to "Continue" to turn on debug messages $DebugPreference="SilentlyContinue" Write-Debug ("Starting function:" + (get-date)) #this can be used to track how much time this function took #$start=Get-Date $errorActionPreference="SilentlyContinue" #this file will store the names of computers where you failed #the file will be overwritten if it already exists $errorlog="myerror.log" if ((Get-ChildItem $errorlog).Exists) { Write-Debug "Deleting $errorlog" Remove-Item $errorlog } #this is the WMI filter we will be using $filter="startmode='auto' AND state !='running'" Write-Debug "filter is $filter" } #end of BEGIN scriptblock PROCESS { if ($_) { $computer=$_ } else { Write-Debug "Setting `$computer to %computername%" $computer=$env:computername } Write-Debug "Examining $computer" #clear $services in case there was an earlier error #that didn't update $services if ($services) { Write-Debug "Removing services variable" Remove-Variable services } trap { #an error occurred so record the computer name Write-Debug $_ Write-Debug "Writing $computer to $errorlog" Write-Output $computer | Out-File $errorlog -append continue } Write-Debug "Running Get-WmiObject" $services=Get-WmiObject win32_service -filter $filter ` -computer $computer -ErrorAction stop if ($services) { Write-Debug ("WMI returned " + ($services).count + " services.") foreach ($svc in $services) { Write-Debug "Processing $svc" if ($start) { Write-Debug "Start parameter passed" #attempt to restart the service if ($noprompt) { $confirmed=$true } else { $confirm=Read-Host "Do you want to start the"$svc.DisplayName"service on"$svc.SystemName"?[YN]" If ($confirm -eq "Y") {$confirmed=$True } else { $confirmed=$false } } if ($confirmed) { Write-Debug ("Starting " + $svc.Name + "service on " + $svc.SystemName) $r=($svc.StartService()).ReturnValue Write-Debug $r if ($r -eq 0) { Write-Host "Successfully started"$svc.DisplayName` "("$svc.name") service on" $svc.SystemName ` -ForegroundColor Green } else { Write-Host "Failed to start"$svc.DisplayName` "("$svc.name") service on" $svc.SystemName"Return Value=$r" ` -ForegroundColor Red } } #end if $confirmed } #end start If else { #report only and write the full WMI Win32_Service object #to the pipeline Write-Debug "writing `$svc to the pipeline" Write-Output $svc } Write-Debug "End ForEach script block" } #end foreach } #end If($services) else { Write-Debug "No services found" } } #end PROCESS END { #display a message if the error log was created if ((Get-ChildItem $errorlog).Exists) { Write-Host "`nCheck $errorlog for connection failures" -ForegroundColor Red } Write-Debug ("Ending function:" + (get-date)) #this can be used to track how much time this function took #$end=Get-Date #$timespan=New-TimeSpan $start $end #[int]$totalseconds=$timespan.totalseconds #Write-Host "Total Time starting services $totalseconds seconds" } # end of END script block }