0

I'm trying to catch the error from service restart using function. The issue Im having that catch is not doing anything through the script.

Function Service_Restart ($Servers, $Service){
    try {
    Write-Host "$CurrentDateTime - Restarting Service $Service on Servers $Servers"
    ForEach($Serv in $Servers){
        

            Get-Service -ComputerName $Serv -Name $Service | Restart-Service
            
        }
        
    
    } catch [System.ServiceProcess.ServiceController] { 
        
        $error[0].Exception

        
}

Service_Restart Server1 ServiceName

I'm expecting to see an one line error not the full stack, however I see the entire error.

What I like to see as an example

Restart-Service : Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be stopped due to the following error: Cannot open AdobeARMservice service on computer 'Localhost'.

However I'm seeing

Restart-Service : Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be stopped due to the following error: Cannot open AdobeARMservice service on computer 'Localhost'.
At C:\user\UTILS_Version3.ps1:48 char:62
+ ...      Get-Service -ComputerName $Serv -Name $Service | Restart-Service
+                                                           ~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand

If I run $error[0].Exception manually I get the result I'm looking for but not through the function.

PS C:\Users\>  $error[0].Exception

Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be stopped due to the following error: Cannot open AdobeARMservice service on computer '127.0.0.1'.
PS C:\Users\> 
mklement0
  • 382,024
  • 64
  • 607
  • 775
Ahmad
  • 15
  • 1
  • In the `catch` block, `$_` is the exception object. Try `$_.Exception`. – AdminOfThings Jan 27 '21 at 14:56
  • I tried that and its not working – Ahmad Jan 27 '21 at 15:03
  • Your exception type is wrong. It needs to be `[Microsoft.PowerShell.Commands.ServiceCommandException]` – AdminOfThings Jan 27 '21 at 15:03
  • I tried that too, same result and getting the whole error stack instead one liner. – Ahmad Jan 27 '21 at 15:08
  • AdminOfThings' answer contains good additional information on how to identify the specific exception type to catch, but the primary problem - expecting a _non-terminating_ error to be caught with `try` / `catch` - is what makes this question a duplicate. – mklement0 Jan 27 '21 at 16:54

1 Answers1

1

When using try and catch, you must be working with terminating errors. Since some errors are non-terminating, you can force them to be terminating. To ensure your commands generate terminating errors, you can use the common parameter -ErrorAction Stop. Alternatively, you can set $ErrorActionPreference = 'Stop' and all commands within the session will implicitly apply -ErrorAction Stop.

In a catch block, $_ or $PSItem is the caught ErrorRecord object. When handling a specific error, it is best to use the error type's full name. You can retrieve that with $_.Exception.GetType().Fullname in the catch block or $error[0].Exception.GetType().Fullname outside of try {} catch {}.

Putting this all together, you can do the following:

try {
    Get-Service -ComputerName $Serv -Name $Service -ErrorAction Stop |
        Restart-Service -ErrorAction Stop
} catch [Microsoft.PowerShell.Commands.ServiceCommandException] {
    $_.Exception
}

See About Try Catch Finally for more information.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27