1

次のようなスクリプトの行が必要です。

if (results from PowerShell command not empty) do something

PowerShellコマンドは基本的に

powershell -command "GetInstalledFoo"

試しif (powershell -command "GetInstalledFoo" != "") echo "yes"ましたが、エラーが発生します-command was unexpected at this time.これは可能ですか?このコマンドは、最終的にはへのコマンドとして実行されcmd /kます。

4

3 に答える 3

3

BartekBの答えは、出力の少なくとも1行がFOR / F eol文字(デフォルトは;)で始まらず、完全に区切り文字(デフォルトはスペースとタブ)で構成されていない限り機能します。適切なFOR/Fオプションを使用すると、常に機能するようにできます。

しかし、これは常に機能するはずの複数行の出力を処理するためのより簡単な(そして私はより速いと信じている)方法です。

for /f %%A in ('powershell -noprofile -command gwmi win32_process ^| find /v /c ""') do if %%A gtr 0 echo yes

もう1つの方法は、一時ファイルを使用することです。

powershell -noprofile -command gwmi win32_process >temp.txt
for %%F in (temp.txt) if %%~zF gtr 0 echo yes
del temp.txt
于 2012-05-17T22:23:16.837 に答える
2

3番目の方法:PowerShellスクリプトから環境変数を設定し、バッチファイルでテストしますか?

于 2012-05-18T05:44:07.673 に答える
1

そのための最善の解決策ではないかと思います。代わりに使用for /fします:

for /f %R in ('powershell -noprofile -command echo foo') do @echo bar

それはあなたに「バー」を与えるはずですが、これは:

for /f %R in ('powershell -noprofile -command $null') do @echo bar

... いけない。実際の.bat/.cmdファイルでは、%(%% R)を2倍にする必要があります

またはさらに良いことに、多くのバーが返されることを望まない場合は...:

(for /f %R in ('powershell -noprofile -command gwmi win32_process') do @echo bar) | find "bar" > nul && echo worked
于 2012-05-17T21:15:05.513 に答える