2014-07-02 29 views
1

我想运行一个命令只有在命令答案简化版,包括:X-UA兼容检查结果被包括在一个指令答案

所以我已经试过这样:

set APPCMD_EXEC=c:\Windows\System32\inetsrv\appcmd.exe 

for /f "delims=" %%a in ('%APPCMD_EXEC% list config "Default Web site" /section:httpProtocol /text:*') do @set VALUE_1=%%a 

IF %VALUE_1% EQU [] Echo List Empty 
IF %VALUE_1% NEQ [] Echo %VALUE_1% 


REM %APPCMD_EXEC% set config "Default Web site" /section:system.webserver/httpProtocol /+"customHeaders.[name='X-UA-Compatible',value='IE=EmulateIE9']" 

我得到的答案是

[redirectHeaders] 

,但如果我只运行命令我得到这一切:

CONFIG 
CONFIG.SECTION:"system.webServer/h 
path:"MACHINE/WEBROOT/APPHOST/Defa 
overrideMode:"Inherit" 
locked:"false" 
[system.webServer/httpProtocol] 
allowKeepAlive:"true" 
[customHeaders] 
    [add] 
    name:"X-UA-Compatible" 
    value:"IE=EmulateIE9" 
[redirectHeaders] 

那么我该如何做到这一点,以便我可以找到X-UA兼容是否存在于我的命令答案中?

回答

2

尝试这样的:

@echo off 

c:\Windows\System32\inetsrv\appcmd.exe list config "Default Web site" /section:httpProtocol /text:* | find "X-UA-Compatible" && goto:found || echo not found 
exit /b 

:found 
echo Here the command to execute 
0

这将只设置value_1如果长期X-UA-Compatible没有找到。 for循环中的find过滤器有助于此。

set APPCMD_EXEC=c:\Windows\System32\inetsrv\appcmd.exe 

set "value_1=" 
for /f "delims=" %%a in ('%APPCMD_EXEC% list config "Default Web site" /section:httpProtocol /text:* ^| find /v "X-UA-Compatible" ') do @set "VALUE_1=%%a" 

if not defined VALUE_1 Echo List Empty 
if defined VALUE_1 Echo %VALUE_1% 


REM %APPCMD_EXEC% set config "Default Web site" /section:system.webserver/httpProtocol /+"customHeaders.[name='X-UA-Compatible',value='IE=EmulateIE9']" 

所以,你可以做到这一点在你的榜样,以执行该命令只有在长期未发现:

set APPCMD_EXEC=c:\Windows\System32\inetsrv\appcmd.exe 

for /f "delims=" %%a in ('%APPCMD_EXEC% list config "Default Web site" /section:httpProtocol /text:* ^| find /v "X-UA-Compatible" ') do %APPCMD_EXEC% set config "Default Web site" /section:system.webserver/httpProtocol /+"customHeaders.[name='X-UA-Compatible',value='IE=EmulateIE9']"