我在当前的Batch脚本中遇到一些问题,无法检查Windows的版本,然后使用某个键激活。这是我到目前为止所得到的
@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
答案 0 :(得分:0)
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
过滤,如上所示,使用各种Windows字符串(或版本号)来查找{{3} }}。使用ver
比使用
cscript /nologo c:\windows\system32\slmgr.vbs /xpr
因为ver
是内部命令。
version of Windows提供的解决方案也考虑了
How to conditionally take action if FINDSTR fails to find a string的解决方案应用于示例批处理文件:
@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
答案 1 :(得分:0)
这是安排脚本的另一种方法:
&&
表示错误级别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 /b