cmd.exe から PowerShell コマンド (スクリプトではない) を実行し、終了コードを適切に管理したい:
powershell.exe [bool]((get-service wsearch).status -eq 'Running')
しかし、ブール値のステータスをエラーレベルとして返したいと思います。
%errorlevel%
実行後にエコーし、それを使用してサービスのステータスを判断したいと思います。
cmd.exe から PowerShell コマンド (スクリプトではない) を実行し、終了コードを適切に管理したい:
powershell.exe [bool]((get-service wsearch).status -eq 'Running')
しかし、ブール値のステータスをエラーレベルとして返したいと思います。
%errorlevel%
実行後にエコーし、それを使用してサービスのステータスを判断したいと思います。
結果を引数として提供するPowerShellexit
コマンドを使用するだけです。例えば:
C:\>powershell -command "exit [int]$true;"
C:\>echo %errorlevel%
1
C:\>powershell -command "exit [int]$false;"
C:\>echo %errorlevel%
0
またはあなたの場合:
powershell.exe -command "exit [int]((get-service wsearch).status -eq 'Running')"
このバリアントを使用することもできます。
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