我知道这是一个老问题,但我认为这些足够有用,可以供人们搜索。
第一个是获取正确版本的简单批处理方法。您可以在另一个进程中找出它是服务器还是工作站(如果这很重要)。我只是没有花时间添加它。
我们在代码中使用这种结构来确保符合要求。我敢肯定还有很多更优雅的方式,但这总是有效的。
:: -------------------------------------
:: Check Windows Version
:: 5.0 = W2K
:: 5.1 = XP
:: 5.2 = Server 2K3
:: 6.0 = Vista or Server 2K8
:: 6.1 = Win7 or Server 2K8R2
:: 6.2 = Win8 or Server 2K12
:: 6.3 = Win8.1 or Server 2K12R2
:: 0.0 = Unknown or Unable to determine
:: --------------------------------------
echo OS Detection: Starting
ver | findstr /i "5\.0\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 2000
)
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows XP
)
ver | findstr /i "5\.2\."
if %ERRORLEVEL% EQU 0 (
echo OS = Server 2003
)
ver | findstr /i "6\.0\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Vista / Server 2008
)
ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 7 / Server 2008R2
)
ver | findstr /i "6\.2\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8 / Server 2012
)
ver | findstr /i "6\.3\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8.1 / Server 2012R2
)
第二个不是所要求的,但它可能对寻找的人有用。
这是一个提供版本信息的 VBscript 函数,包括它是否是服务器(相对于工作站)。
private function GetOSVer()
dim strOsName: strOsName = ""
dim strOsVer: strOsVer = ""
dim strOsType: strOsType = ""
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
strOsName = objOS.Caption
strOsVer = left(objOS.Version, 3)
Select Case strOsVer
case "5.0" 'Windows 2000
if instr(strOsName, "Server") then
strOsType = "W2K Server"
else
strOsType = "W2K Workstation"
end if
case "5.1" 'Windows XP 32bit
strOsType = "XP 32bit"
case "5.2" 'Windows 2003, 2003R2, XP 64bit
if instr(strOsName, "XP") then
strOsType = "XP 64bit"
elseif instr(strOsName, "R2") then
strOsType = "W2K3R2 Server"
else
strOsType = "W2K3 Server"
end if
case "6.0" 'Vista, Server 2008
if instr(strOsName, "Server") then
strOsType = "W2K8 Server"
else
strOsType = "Vista"
end if
case "6.1" 'Server 2008R2, Win7
if instr(strOsName, "Server") then
strOsType = "W2K8R2 Server"
else
strOsType = "Win7"
end if
case "6.2" 'Server 2012, Win8
if instr(strOsName, "Server") then
strOsType = "W2K12 Server"
else
strOsType = "Win8"
end if
case "6.3" 'Server 2012R2, Win8.1
if instr(strOsName, "Server") then
strOsType = "W2K12R2 Server"
else
strOsType = "Win8.1"
end if
case else 'Unknown OS
strOsType = "Unknown"
end select
Next
GetOSVer = strOsType
end Function 'GetOSVer