How do you make powershell ignore failures on legacy commands?
All powershell modules support -ErrorAction
, however that does not work with legacy commands like NET STOP
NET STOP WUAUSERV
echo $?
true
I want to attempt to stop the service, and if the service is already stopped, continue. An easy way to reproduce this is to try and stop the service twice.
Things I've tried
$ErrorActionPreference = "SilentlyContinue"
NET STOP WUAUSERV
NET STOP WUAUSERV
echo $LASTEXITCODE
Tried using AND and OR operrators
and
NET STOP WUAUSERV || $true
NET STOP WUAUSERV || $true
echo $LASTEXITCODE
or
NET STOP WUAUSERV && $true
NET STOP WUAUSERV && $true
echo $LASTEXITCODE
I've also tried redirecting errors
NET STOP WUAUSERV 2>nul
NET STOP WUAUSERV 2>nul
echo $LASTEXITCODE
As recommended in a similar question, I've also tried
cmd.exe /C "NET STOP WUAUSERV"
How can I make this legacy command idempotent?