我有一些问题与我的当前批处理脚本,以检查版本的窗口,然后激活与某个键。这就是我到目前为止所得到的
@echo on
:7
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1
if [%errorlevel%]==[0] (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if [%errorlevel%]==[0] (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO VISTA)
:VISTA
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) Vista"> NUL 2>&1
if [%errorlevel%]==[0] (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if [%errorlevel%]==[0] (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO END)
:END
pause
exit /b发布于 2014-06-28 05:16:54
解决方案1:延迟扩展
在示例批处理文件上应用David Ruhmann的解决方案:
@echo on
setlocal enabledelayedexpansion
:7
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1
if [!errorlevel!]==[0] (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if [!errorlevel!]==[0] (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO VISTA)
:VISTA
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) Vista"> NUL 2>&1
if [!errorlevel!]==[0] (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if [!errorlevel!]==[0] (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO END)
:END
endlocal
pause
exit /b注意:命令ver的输出也可以通过findstr进行过滤,如上面所示,使用各种Window字符串(或版本号)来查找version of Windows。使用ver比使用
cscript /nologo c:\windows\system32\slmgr.vbs /xpr因为ver是一个内部命令。
解决方案2:测试错误级别
Matt Williamson提供的解决方案还考虑到
Microsoft
请参阅堆栈溢出问题C++: How can I return a negative value in main.cpp
任何程序员都不应该退出具有负值的应用程序。这简直是个坏主意。
在示例批处理文件上应用Matt Williamson的解决方案:
@echo on
:7
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1
if not errorlevel 1 (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if not errorlevel 1 (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO VISTA)
:VISTA
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) Vista"> NUL 2>&1
if not errorlevel 1 (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if not errorlevel 1 (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO END)
:END
pause
exit /b发布于 2014-06-28 11:36:16
以下是安排脚本的另一种方法:
&&表示错误级别0,反之,||表示错误级别1
@echo off
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1 && set num=1XXXX-XXXXX-XXXXX-XXXXX-XXXXX
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) Vista"> NUL 2>&1 && set num=2XXXX-XXXXX-XXXXX-XXXXX-XXXXX
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1 && (
slmgr.vbs /ipk %num%
slmgr.vbs /ato
)
pause
exit /bhttps://stackoverflow.com/questions/22381137
复制相似问题