-ErrorAction SilentlyContinue location

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Arik Cher Arik Cher 1 month ago.

  • Author
    Posts
  • #67104
    Profile photo of Arik Cher
    Arik Cher
    Participant

    Hi

    I'm trying to run this

    $servers | foreach { (Get-Service -Name gservice* -ErrorAction SilentlyContinue -computername $_ ) | Select-Object machinename,Name,Status} |FT -autosize

    and I see errors although I added the "-ErrorAction SilentlyContinue" . I tried to move it to different locations but still , I see errors .

    I also tried "just" Get-Service -Name gservice -ErrorAction SilentlyContinue -computername abc and still ot errors .

    is there a way to add -ErrorAction SilentlyContinue to get-service or I need to change the script to try&catch or something else ?

  • #67119
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    It looks like if you don't have the wildcard then a non-terminating error is thrown, -ErrorAction only applies to non-terminating errors. You don't get nasty red output but you can view the error that's written to the error pipeline.

    If you do use a wildcard a terminating error is thrown which is why -ErrorAction has no effect. It's a terminating error so the cmdlet stops running.

    $error.Clear()
    
    Get-Service -ComputerName NonExistant -Name W -ErrorAction SilentlyContinue
    
    Write-Output $error[0]
    
    Get-Service -ComputerName NonExistant -Name W* -ErrorAction SilentlyContinue

    You will need to use a try/catch block for error handling.

    • #67401
      Profile photo of Arik Cher
      Arik Cher
      Participant

      It did the job
      Thanks

You must be logged in to reply to this topic.