首页 > 解决方案 > 我的脚本有问题,但我不知道是什么

问题描述

我正在尝试创建一个批处理脚本,它将根据计算机操作系统和体系结构下载文件。我唯一的问题是每当我运行批处理文件时,它会在检查计算机使用的架构后关闭。我觉得它是另一个 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

我期待批处理脚本找到操作系统(要知道要使用哪个 powershell 命令)然后是架构(要知道要下载什么版本)然后运行下载命令。实际发生的是脚本将一直运行到注册表查询,然后在开始处理 if 语句之前,它会停止并关闭窗口。

标签: batch-filecmd

解决方案


这是一种使用WMICand执行检查的方法GoTO

@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的 's 之间添加您的特定命令。

希望能帮助到你!


推荐阅读