我的脚本出了点问题,但是我不知道是什么

时间:2018-12-30 18:40:08

标签: batch-file cmd

我正在尝试创建一个批处理脚本,该脚本将根据计算机的操作系统和体系结构下载文件。我唯一的问题是,每当我运行批处理文件时,它都会在检查计算机使用的架构后关闭。我觉得它在另一个if语句中的if语句,但是我不确定。 (此外,我知道它专门说没有模糊的标题,但我不确定会有更好的标题。)

我的意图是拥有一个运行exe文件的便携式程序,并在缺少该文件时运行该文件。我尝试了多种检查操作系统的方法,并尝试重新排列了if语句,但到目前为止没有任何效果。

setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set WINOS=%%i.%%j
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /I "x86" > NUL && set ARCHCHECK=32BIT || set ARCHCHECK=64BIT
if %WINOS%==10.0 (
   if %ARCHCHECK%==32BIT (
      echo You are running Windows 10 on a 32 Bit architecture. Getting right files.
      powershell -Command "IInvoke-WebRequest https://example.com/example.zip -OutFile example.zip"
   if %ARCHCHECK%==64BIT (
      echo You are running Windows 10 on a 64 Bit architecture. Getting right files.
      powershell -Command "Invoke-WebRequest https://example.com/example.zip -OutFile example.zip"
if %WINOS%==6.1 (
   if %ARCHCHECK%==32BIT (
      echo You are running Windows 7 on a 32 Bit architecture. Getting right files.
      powershell -Command "(New-Object Net.WebClient).DownloadFile('https://example.com/example.zip', 'example.zip')"
   if %ARCHCHECK%==64BIT (
      echo You are running Windows 7 on a 64 Bit architecture. Getting right files.
      powershell -Command "(New-Object Net.WebClient).DownloadFile('https://example.com/example.zip', 'example.zip')"

对于Windows 8依此类推

我希望批处理脚本先找到OS(要使用哪个powershell命令),然后再找到体系结构(要下载哪个版本),然后运行download命令。实际发生的是该脚本将一直运行到注册表查询,然后在开始对if语句进行操作之前,它将停止并关闭窗口。

1 个答案:

答案 0 :(得分:1)

这是一种使用WMICGoTO进行检查的方法:

@Echo Off
Set "MJ="
Set "MN="
For /F "EOL=O Delims=" %%A In ('"WMIc OS Get OSArchitecture,Version 2>Nul"'
) Do For /F "Tokens=1,3-4 Delims=-. " %%B In ("%%A"
) Do Set/A "OA=%%B, MJ=%%C, MN=%%D"
If Not Defined MN (If Not Defined MJ (Echo Unsupported OS&Pause&Exit/B) Else (
    Set/A MJ=5, MN=1))
Call :v%MJ%%MN%x%OA%
Pause
Exit/B

:v51x32
Echo Windows XP, Whistler Server x%OA% Commands
GoTo :EOF

:v51x64
Echo Windows XP, Whistler Server x%OA% Commands
GoTo :EOF

:v52x32
Echo Windows NET Server, Home Server, Server 2003 x%OA% Commands
GoTo :EOF

:v52x64
Echo Windows NET Server, Home Server, Server 2003 x%OA% Commands
GoTo :EOF

:v60x32
Echo Windows Longhorn, Vista, Server 2008 x%OA% Commands
GoTo :EOF

:v60x64
Echo Windows Longhorn, Vista, Server 2008 x%OA% Commands
GoTo :EOF

:v61x32
Echo Windows 7, Server 2008 R2, Home Server 2011 x%OA% Commands
GoTo :EOF

:v61x64
Echo Windows 7, Server 2008 R2, Home Server 2011 x%OA% Commands
GoTo :EOF

:v62x32
Echo Windows 8, Server 2012 x%OA% Commands
GoTo :EOF

:v62x64
Echo Windows 8, Server 2012 x%OA% Commands
GoTo :EOF

:v63x32
Echo Windows 8.1, Server 2012 R2 x%OA% Commands
GoTo :EOF

:v63x64
Echo Windows 8.1, Server 2012 R2 x%OA% Commands
GoTo :EOF

:v100x32
Echo Windows 10, Server 2016 x%OA% Commands
GoTo :EOF

:v100x64
Echo Windows 10, Server 2016 x%OA% Commands
GoTo :EOF

您可以替换Echo命令或在它们与它们各自的GoTo之间添加特定的命令。

希望有帮助!