如何使用批处理来确定计算机运行的Windows版本?

问题描述 投票:0回答:4

在编写脚本时,有必要确定计算机正在运行的Windows版本。唯一的问题是Windows 10和Server 2016具有相同的版本号。

这是当前的代码。在此状态下,Windows10和Server2016标识有相同的编号(不起作用)。 qazxsw poi输出= qazxsw poi

wmic os get version

我想做的是parse10.0.18329获取操作系统名称。这显示了一些挑战,因为输出是多行,并且在操作系统名称('Windows 10')之前和之后有任意数量的字符。


这是windows 10 pro上for /f "tokens=4-5 delims=. " %%i in ('ver') do set Operating=%%i.%%j if "%Operating%" == "6.3" set Operating=Windows81 if "%Operating%" == "6.2" set Operating=Windows8 if "%Operating%" == "10.0" set Operating=Windows10 if "%Operating%" == "10.0" set Operating=Server2016 echo Is %Operating% your operating system? 的输出:

wmic os get name

这是Server 2016上wmic os get name的输出:

Name
Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk1\Partition3
windows batch-file scripting wmic
4个回答
4
投票

我知道你已经选择了一个解决方案,但是如果这与你提出的最后一个问题有关,现在已经删除了,你只想在wmic os get nameName Microsoft Windows Server 2016 Standard|C:\Windows|\Device\Harddisk0\Partition2 Windows 8Windows 8.1之间确定,那么我会建议这些问题。 :

Windows 10

[编辑/]

...对于通用方法,你可能会发现这很有用,特别是使用Windows Server 2016属性,(我建议使用@Echo Off Set "_S=" For /F "EOL=P Tokens=*" %%A In ('"WMIC OS Get ProductType,Version 2>Nul"' ) Do For /F "Tokens=1-3 Delims=. " %%B In ("%%A") Do Set /A _S=%%B,_V=%%C%%D If Not Defined _S Exit /B If %_V% Lss 62 Exit /B If %_S% Equ 1 (If %_V% Equ 62 Set "_S=Windows8" If %_V% Equ 63 Set "_S=Windows81" If %_V% Equ 100 Set "_S=Windows10" ) Else If %_V% Equ 100 (Set "_S=Server2016") Else Exit /B Set _S Pause Exit /B 属性,将其拆分为第一个管道字符,通过另一个Caption循环运行它)要好得多然后删除一个或多个尾随空格字符):

Name

要测试它只需添加第二行:

For

注意:此方法可能在某些早期操作系统上存在问题,因为它们会阻止它们找到@For /F Tokens^=6Delims^=^" %%A In ('WMIC OS Get Caption/Format:MOF')Do Set "_S=%%A" 文件的位置。有一些解决方法,要么将@Set _S&Pause 文件从“语言”目录复制到一个级别,要么提供.xsl文件的完整路径,例如: .xsl


3
投票

怎么样直接......

.xsl

1
投票

这很简单:

/Format:"%__AppDir__%wbem\en-US\MOF.xsl"

包含所有Windows版本的另一种可能的解决方案:

@echo off
for /f "skip=1 delims=|" %%i in ('wmic os get name') do echo %%i & exit /b

这将提取OS,在两种情况下最后减去qazxsw poi。

在第二种情况下,使用了两个循环,因为qazxsw poi具有不寻常的行结尾(qazxsw poi)。


-2
投票

你可以使用@echo off for /F "skip=1 tokens=2-4 delims=| " %%A IN ('wmic os get name') do ( set "os=%%A %%B %%C" ) echo You are using %os%. 命令,这是一个例子:

@echo off

for /F "skip=1 delims=|" %%A IN ('wmic os get name') do (
    for /F "delims=" %%B IN ("%%A") do (
        set "os=%%B"
    )
)

echo You are using %os:Microsoft =%.
pause
exit /b

使用变量Microsoft<space>作为您运行的版本,我用它来wmic版本,但你可以用它做任何事情

© www.soinside.com 2019 - 2024. All rights reserved.