解析Windows命令行批处理文件中的命令行输出

时间:2015-12-20 11:09:06

标签: windows parsing batch-file command-line

我有这个命令扫描文件并返回摘要。

例如,在运行此命令时

omsCmdLineUtil.exe process C:\test.exe Default

结果输出为:

Ticket:[ 2214271306 ]

Process Details
---------------


File: [ C:\test.exe ]
   MD5: [ D41D8CD98F00B204E9800998ECF8427E ]
   SHA1: [ DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ]
   SHA256: [ E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 ]
   File Size: [ 0 bytes ]
   File Type  Category: [ O ]
   File Type: [ - ]
   File Type Description: [ empty ]

[ Clean ] Ahnlab scan engine [ 1 ms ]
[ Clean ] ClamAV scan engine [ 1 ms ]
[ Clean ] BitDefender scan engine [ 1 ms ]
[ Clean ] Avira scan engine [ 1 ms ]
[ Clean ] Quick Heal scan engine [ 1 ms ]
[ Clean ] ThreatTrack scan engine [ 1 ms ]
[ Clean ] ESET scan engine [ 1 ms ]
[ Clean ] Total Defense scan engine [ 1 ms ]

Scan Completion
---------------
        [ Clean ]
        Ticket: [ 2214271306 ]
        File path: C:\test.exe
        Scan time: 1 ms [12/20/2015 13:00:06:791]


Process Completion
------------------
        Ticket: [ 2214271306 ]
        User agent: Default
        Profile: Default
        Result: [ Allowed ]
        File processed: C:\test.exe

我想创建一个批处理文件,通过搜索输出行Result:来解析此结果,检查它是[ Allowed ]还是[ Blocked ]并返回0 } allowed1 blocked

我尝试过类似的东西,但它并没有真正发挥作用:

omsCmdLineUtil.exe process C:\test.exe Default | set ts = findstr /C:"Result: [ Allowed ]"

if %ts% == "Result: [ Allowed ]" return 0
else return 1

为了获得预期的结果,需要对代码进行哪些修改?

1 个答案:

答案 0 :(得分:1)

Batch中没有return。我想,你想要exit /b <errorlevel>

omsCmdLineUtil.exe process C:\test.exe Default | find "Result: [ Allowed ]" >nul && Exit /b 0 || Exit /b 1

您可以使用Exit 0而不是set ts=0而使用echo。或者改为使用>nul

一些解释:

&&将输出重定向到必杀技,保持屏幕清洁。

||充当“如果上一个命令成功,那么......”(找到字符串)

find充当“如果以前的命令不成功,则......”(未找到字符串)

我更喜欢在可能的情况下使用findstr /C:"Result: [ Allowed ]",因为它的语法更简单,但当然 'Preffix' => '03-1/35', 'Author_resolution' => '[pcho_P_admin]', '$OSN_Eventcode' => 'ctrl_rezolucia_00_00', '\$OSN_Eventcode' => 'ctrl_rezolucia_00_00' 也可以使用