如何批量提取查询中的数据

时间:2013-05-22 18:00:59

标签: batch-file psexec

我正在尝试检查服务ACR BRIDGE是否正在运行并且启动它不是。

cd C:\Users\ET012364\Desktop\PSEXEC
psservice \\ccapps query "ACR BRIDGE"
pause

我知道我可以使用错误级别和stop start命令的组合来实现这一点。但是,我更想知道如何操作此命令返回的数据。

SERVICE_NAME: ACRBridge
DISPLAY_NAME: ACR Bridge
ACR Bridge for controlling both Master and Standby Collect Corp Recorders.  If t
he service is down, it will restart after one minute.
        GROUP             : someorder
        TYPE              : 10 WIN32_OWN_PROCESS
        STATE             : 4  RUNNING
                               (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE   : 0  (0x0)
        SERVICE_EXIT_CODE : 0  (0x0)
        CHECKPOINT        : 0x0
        WAIT_HINT         : 0 ms

这是返回的内容以及我想从中提取STATE值的内容。

这是我最终使用的代码。我认为这是最简单的解决方案。

 @echo off
 cd C:\Users\ET012364\Desktop\PSEXEC


 psservice \\ccapps query "ACR BRIDGE" | find "RUNNING"

 if "%ERRORLEVEL%"=="0" (
     echo ACR BRIDGE is running
 ) else (
     echo ACR BRIDGE on CCAPPS is not running
     pause
 )


 psservice \\ccapps query "DIALERMESSAGEMONITOR"| find "RUNNING"

 if "%ERRORLEVEL%"=="0" (
     echo Dialer MessageMonitor is running
 ) else (
     echo Dialer MessageMonitor on CCAPPS is not running
     pause
 )

2 个答案:

答案 0 :(得分:0)

试试这个:

for /f "tokens=1-3 delims=: " %%i in ('psservice \\ccapps query "ACR BRIDGE" 2^>nul') do if "%%i"=="STATE" set "state=%%j %%k"
REM                        ^----put a tabstop after the ":"
echo %state%

答案 1 :(得分:0)

这会在回复您的问题时修改上述评论。这是未经测试的。

@echo off
cd /d "C:\Users\ET012364\Desktop\PSEXEC"
psservice \\ccapps query "ACR BRIDGE" | FIND "STATE" | FIND "RUNNING" >nul || net start "ACR BRIDGE"