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 ?
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.