none
Restart Services with try catch silently continue RRS feed

  • Question

  • I am trying to automate a reboot script. At the end it restarts some services to verify and guarentee everything will come up correctly. The restarting of the services is causing the most issue and since we run it at night I haven't found out what this issue is. What is the proper method to silently continue but also log the error? Is it something like

    try{

    Restart-Service-ErrorAction SilentlyContinue -InputObject$(Get-Service-Computer"dc1-prod-vc01"-Namevpxd)

    Add-Content $output "Send restart command"

    } Catch {

      Add-Content $output "There was an error" + $Error[0]

    }

    Monday, July 27, 2015 3:06 PM

Answers

  • 1. Why do you need to restart a service after a reboot?

    2. try / catch only catches terminating errors. So you would have to use -ErrorAction Stop rather than -ErrorAction SilentlyContinue.


    -- Bill Stewart [Bill_Stewart]

    Monday, July 27, 2015 3:18 PM
    Moderator

All replies

  • 1. Why do you need to restart a service after a reboot?

    2. try / catch only catches terminating errors. So you would have to use -ErrorAction Stop rather than -ErrorAction SilentlyContinue.


    -- Bill Stewart [Bill_Stewart]

    Monday, July 27, 2015 3:18 PM
    Moderator
  • Also -

    try{
        Get-Service -Computer"dc1-prod-vc01" -Name vpxd |
            Restart-Service -EA Stop
            Add-Content $output "Send restart command"
    }
    Catch{
      Add-Content $output "$_"
    }
    


    \_(ツ)_/

    Monday, July 27, 2015 3:36 PM