This is a bit of an oddly-specific one, but the request was “we need to shut down a specific set of services, in a specific order, for a number of servers”. And then the reverse to start them back up again.
The scripts takes two files as inputs, one for the list of services (in the order you want them to stop/start in), and the second with a list of computer names to perform the actions on.
This is definitely not a pretty solution, as the time-frame I was given meant that any elegance was thrown out, in favour of “just get it done”.
I was able to include some basic error handling as well – which means that you can see exactly how far into stopping/starting services the script got, making rolling forward/backward much easier.
Script to stop services:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# Get list of services from txt file $tostopservices = get-content "c:\temp\servicelist.txt"; $computerlist = get-content "c:\temp\computerlist.txt"; # Loop through list of computers foreach ($computer in $computerlist) { # Create arrays for tracking success and failure $unstoppedservices = [System.Collections.ArrayList]$tostopservices; [System.Collections.ArrayList]$stoppedservices = @(); # Loop through each service foreach ($tostopservice in $tostopservices) { # Try to stop the service try { get-service -name $tostopservice -computername $computer -erroraction stop | stop-service -force -erroraction stop | out-null # If stopping the service fails, output and break execution } catch { write-host "[FAIL] Execution halted - service" $tostopservice "does not exist or could not be stopped on" $computer -foregroundcolor "red"; write-host ""; write-host "Stopped Services" write-host "================" write-host $stoppedservices; write-host ""; write-host "Not Stopped Services" write-host "====================" write-host $unstoppedservices; break; } # Check current state of service $checkstateservice = get-service -computername $computer -name $tostopservice; # Check if service is NOT stopped, output and break execution if ($checkstateservice.status -ne "Stopped") { write-host "[FAIL] Execution halted - service" $tostopservice "not stopped on" $computer -foregroundcolor "red"; write-host ""; write-host "Stopped Services" write-host "================" write-host $stoppedservices; write-host ""; write-host "Not Stopped Services" write-host "====================" write-host $unstoppedservices; break; # If the service stopped correctly, output, update success/failure arrays, and continue through loop } else { write-host "[SUCCESS] Service" $tostopservice "stopped on" $computer -foregroundcolor "green" $stoppedservices.add($tostopservice) | out-null; $unstoppedservices.remove($tostopservice) | out-null; } } } |
Which looks like this when run ( I used ‘localhost’ in my computerlist.txt file):
And this script is almost identical to the above, but starts the services in the order specified:
(And don’t forget to list the services in the reverse order to the above!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# Get list of services from txt file $tostartservices = get-content "c:\temp\servicelist.txt"; $computerlist = get-content "c:\temp\computerlist.txt"; # Loop through list of computers foreach ($computer in $computerlist) { # Create arrays for tracking success and failure $unstartedservices = [System.Collections.ArrayList]$tostartservices; [System.Collections.ArrayList]$startedservices = @(); # Loop through each service foreach ($tostartservice in $tostartservices) { # Try to start the service try { get-service -name $tostartservice -computername $computer -erroraction stop | start-service -erroraction stop | out-null # If starting the service fails, output and break execution } catch { write-host "[FAIL] Execution halted - service" $tostartservice "does not exist or could not be started on" $computer -foregroundcolor "red"; write-host ""; write-host "Started Services" write-host "================" write-host $startedservices; write-host ""; write-host "Unstarted Services" write-host "====================" write-host $unstartedservices; break; } # Check current state of service $checkstateservice = get-service -computername $computer -name $tostartservice; # Check if service is NOT started, output and break execution if ($checkstateservice.status -ne "Running") { write-host "[FAIL] Execution halted - service" $tostartservice "not stopped on" $computer -foregroundcolor "red"; write-host ""; write-host "Started Services" write-host "================" write-host $startedservices; write-host ""; write-host "Not Unstarted Services" write-host "====================" write-host $unstartedservices; break; # If the service started correctly, output, update success/failure arrays, and continue through loop } else { write-host "[SUCCESS] Service" $tostartservice "started on" $computer -foregroundcolor "green" $startedservices.add($tostartservice) | out-null; $unstartedservices.remove($tostartservice) | out-null; } } } |
And the result: