windows - 等待WMIC完成远程调用

标签 windows wmic

我正在使用 wmic 执行一些远程任务,但是,我不能强制它等待完成。例如说以下内容:

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

wmic 进程终止,而另一个 cmd 窗口正在休眠 10 秒。

我希望 wmic 进程等待打开的 cmd session 完成(即在本例中等待 10 秒)。这可能吗?

更新:我正在寻找非批处理解决方案

最佳答案

下一个批处理脚本可能适合您:

@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

==>

关于windows - 等待WMIC完成远程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599427/

相关文章:

c - IsAdminUser 返回不正确的值

java - 执行xp_cmdshell 'wmic ... "java -jar .. ."'时使用哪个帐户

WIC产品,其中 "name like..."-如果在批处理文件中运行,则为 "no instances available"

powershell - 如何检查 IIS 状态和正常运行时间?

c++ - Qt - 在 Windows 上按下 Alt 后防止菜单栏获取焦点

windows - Qt 5.1 Windows 7 - aboutToQuit() 未在 Windows 注销时调用

c++ - 使用 Windows API 检测网卡和以太网设备的 IP 地址

windows - Powershell ConvertTo-SecureString ObjectNotFound

python - 在 Windows 10 上从 Python 获取准确的文件创建日期

powershell - 通过ParentProcessID杀死进程