Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file Cmd脚本返回非零代码,ansible获取零代码_Batch File_Cmd_Ansible - Fatal编程技术网

Batch file Cmd脚本返回非零代码,ansible获取零代码

Batch file Cmd脚本返回非零代码,ansible获取零代码,batch-file,cmd,ansible,Batch File,Cmd,Ansible,我正在服务器上运行一个.bat脚本,该脚本只检查服务是否正在运行,如下所示: nssm.exe start MyService for /F "tokens=3 delims=: " %%H in ('sc query "MyService" ^| findstr "STATE"') do ( if /I "%%H" NEQ "RUNNING" ( exit /b 1 ) else ( exit /b 0 ) ) 此ansible调用此.bat文件: - n

我正在服务器上运行一个.bat脚本,该脚本只检查服务是否正在运行,如下所示:

nssm.exe start MyService
for /F "tokens=3 delims=: " %%H in ('sc query "MyService" ^| findstr "STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
      exit /b 1
  ) else (
      exit /b 0
  )
)
此ansible调用此.bat文件:

- name: Start service
  win_command: StartService.bat
  args:
     chdir: "C:\temp"
  register: result
  failed_when: result.rc != 0
但无论是失败还是成功,我总是得到一个0错误代码。
如果我手动运行脚本,并检查
%errorlevel%
它始终显示正确的脚本。

请查看此版本:

nssm.exe start MyService
sc query "MyService" | findstr /I "RUNNING"
exit /b %errorlevel%
您是否尝试过使用并设置它的
args:executable:cmd.exe
,看看这是否有帮助?我相信你正在被所有使用powershell的ansible
win.*
任务所折磨,但你使用的是
cmd.exe
,他们并不总是意见一致;事实上,最好的结果可能是您真正使用powershell,因为它实际上更具表现力