Reputation: 531
I want to run a PowerShell command (not script) from a cmd.exe and manage the exit code properly:
powershell.exe [bool]((get-service wsearch).status -eq 'Running')
But I would like to return the boolean status as the error level.
I would like to echo %errorlevel%
after running it and use it to determine service status.
Upvotes: 3
Views: 2077
Reputation: 2888
You can also use this variant:
CMD I> Set cmd=powershell -c "((gsv wsearch -ea 0).status -eq 'Running')"
CMD I> %cmd% |>nul find "True" && echo RUNNING||echo NOT RUNNING
RUNNING
CMD I> Set cmd=powershell -c "((gsv youka -ea 0).status -eq 'Running')"
CMD I> %cmd% |>nul find "True" && echo RUNNING||echo NOT RUNNING
NOT RUNNING
Upvotes: 0
Reputation: 29450
Just use the PowerShell exit
command providing the result as an argument. For example:
C:\>powershell -command "exit [int]$true;"
C:\>echo %errorlevel%
1
C:\>powershell -command "exit [int]$false;"
C:\>echo %errorlevel%
0
Or for your case:
powershell.exe -command "exit [int]((get-service wsearch).status -eq 'Running')"
Upvotes: 3