2016-09-20 34 views
0

リモートタスクを実行するためにwmicを使用していますが、完了まで待つことはできません。たとえば、次のように言ってください。WMICがリモートコールを終了するまで待ちます

wmic process call create "cmd.exe /c ping 127.0.0.1 -n 10 > nul","C:\Data" 

wmicプロセスが終了し、別のcmdウィンドウが10秒間スリープ状態になっています。

開かれたcmdセッションが終了するまで待ちます(この場合、10秒待っています)。これは可能ですか?

UPDATE:私は

答えて

1

次のバッチスクリプトは、あなたのために働く可能性が非バッチソリューションを探しています:

@ECHO OFF >NUL 
SETLOCAL EnableExtensions DisableDelayedExpansion 

REM get the global process identifier 
set "_ProcessId=" 
for /F "tokens=*" %%G in (' 

    wmic process call create "cmd.exe /c ping 127.0.0.1 -n 10 > nul"^,"%temp%"^|find /I "ProcessId" 

') do for /F "tokens=1,2 delims=;= " %%g in ("%%~G") do set "_%%~g=%%~h" 

REM wait while the process is running 
If defined _ProcessId (
    call :waitForEnd 
) else (
    rem debugging output  echo NO process was created 
) 

ENDLOCAL 
goto :eof 

:waitForEnd 
    rem debugging output 
    echo waiting until [%_ProcessId%] terminates %time% 
    rem set appropriate number of seconds to wait in next timeout command 
    >NUL 2>&1 timeout /T 4 /NOBREAK 
    for /F "tokens=1 delims=:" %%G in (' 
     tasklist /FI "PID eq %_ProcessId%" /FO csv /NH 
    ') do if /I NOT [%%G]==[INFO] goto :waitForEnd 
    rem debugging output 
    echo  process [%_ProcessId%] terminated %time% 
goto :eof 

出力

==> D:\bat\SO\39599427.bat 

waiting until [2420] terminates 23:24:52,77 
waiting until [2420] terminates 23:24:56,21 
waiting until [2420] terminates 23:25:00,20 
     process [2420] terminated 23:25:04,22 

==> 
関連する問題