等到完成 WMIC 进程调用创建 cmd.exe 来运行 bat

等到完成 WMIC 进程调用创建 cmd.exe 来运行 bat

我正在运行位于远程服务器上的 bat 文件。命令是:wmic /node:[服务器名称] process call create "cmd.exe /c [bat 文件位置]。我希望命令等到批处理文件执行完成。换句话说:该命令是 Jenkins (Hudson) 作业的一部分,窗口批处理命令之后的下一步在批处理文件任务完成之前启动。我希望下一步在批处理文件执行完成后立即执行。* 批处理文件任务(内容)正在恢复数据库。

答案1

下一个评论代码片段可以完成这项工作。不幸的是,我只能在本地节点上测试它。

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

set "_toRun=cmd.exe /C pause"   change to meet your circumstances 
set "_where=."                  sorry testing merely local computer 

set "_toGet=ProcessId ReturnValue"
set "_ProcessId="
set "_ReturnValue="
    rem %%G loop to get raw return values from `create` like next two lines
    rem ProcessId = 6476;
    rem ReturnValue = 0;
for /F "tokens=*" %%G in ('
  wmic /node:"%_where%" process call create "%_toRun%" ^| findstr "%_toGet%"
') do (
        rem %%g loop to assign values to variables `_ProcessId` and `_ReturnValue`
    for /F "tokens=1,2 delims=;= " %%g in ("%%~G") do ( 
      set "_%%~g=%%~h"
    )
)
if NOT "%_ReturnValue%"=="0" (
  echo %_toRun% failed, Return Value %_ReturnValue%
  goto :notest 
)
echo process %_toRun% runs as %_ProcessId%

    rem build WQL query
set "_query=ProcessId = %_ProcessId% and CommandLine = '%_toRun:\=\\%'"

    rem loop while queried process runs 
:test
set "_CommandLine="
for /F %%G in ('
  wmic /node:"%_where%" process where "%_query%" get CommandLine /value ^| findstr "="
') do set "_%%~G"
if not defined _CommandLine goto :notest
set _Comman
>NUL timeout /T 5 /NOBREAK 
goto :test

:notest

资源(必读,未完成):

相关内容