Who do I catch a second, third etc Exception

by Sep 2, 2015

Greetings,

Long ago I create a tool to restart a service.  Since then I learn a bit more PowerShell and added a bit more and now I'm wanting to add more still.  This is one of my learning scripts.

My Psudo logic
Ping PC
If PC not alive write to file and screen
If PC is alive Restart Service – write to screen and file

Here is where I noticed the issue.  Some PC's don't have the patch client so there is no service to restart.  I now know how to get the exception error.

Question 1

Do I create another catch for the service/error?

in my search I've seen some add another catch and all would be in line and I've seen other add some to a sub script block.

Question 2

How do I modify it to execute it to multiple systems at once (I'm thinking about creating a separate thread with this question to help me with my learning)

 As Always – Thank you for your guidance

$computers = Get-Content -Path 'C:UsersItsMeDesktopComputers.txt'
Clear-Content -Path C:UsersItsMeDesktopWorking.txt

ForEach ($computer in $computers) {
Try {
Test-Connection -ComputerName $computer -Count 1 -ErrorAction Stop | Out-Null
Write-Host "$computer located" -ForegroundColor Green
Restart-Service -InputObject $(Get-Service -Computer $computer -Name BESClient)
$Good = "Service Restarted on $Computer"
Write-Host "$computer Service Restarted" -ForegroundColor Green
Add-Content -Path C:UsersItsMeDesktopWorking.txt -Value $Good
}
Catch [System.Net.NetworkInformation.PingException] {
Write-Host "$computer Not found" -ForegroundColor Red
$Err = "Computer Name $Computer Not Reachable"
Add-Content -Path C:UsersItsMeDesktopWorking.txt -Value $Err
}
}