如何在VBScript中不使用WMI查找操作系统名称?
的VBScript:
Set oShell = CreateObject("WScript.Shell")
os_name=oShell.ExpandEnvironmentStrings("%OS%")
WScript.Echo os_name
Option Explicit
Dim oShell
Dim oShellExec, oStdOutputText, sText, iElement, aOS, sOS
Set oShell = CreateObject("Wscript.Shell")
Set oShellExec = oShell.Exec("%comspec% /c ver")
Set oStdOutputText = oShellExec.StdOut
Do While Not oStdOutputText.AtEndOfStream
sText = oStdOutputText.ReadLine
aOS = Array("Windows 95", "Windows 98", "Windows NT", "Windows 2000", "Windows XP", "Microsoft Windows [Version")
For iElement = LBound(aOS) To UBound(aOS)
If InStr(sText, aOS(iElement)) <> 0 Then
If aOS(iElement) = "Microsoft Windows [Version" Then
If InStr(sText, "Version6.0") <> 0 Then
sOS = "Windows Vista"
ElseIf InStr(sText, "Version 6.1")<>0 Then
sOS = "Windows 7"
Else
sOS = "Windows 2003"
End If
Else
sOS = aOS(iElement)
End If
End If
Next
Loop
WScript.Echo sOS
这不区分XP和Vista/Win7,在Win7上这返回Windows 2003 – toddstavish 2012-08-12 18:35:44
@toddstavish:已修复支持Vista和Windows 7. – 2012-08-13 23:37:15
我们必须分析在用户路径在2K/XP上不同的帐户,然后他们在Vista/Win7上。 comspec返回如下所示:Microsoft Windows [Version 6.1.7600]。 2k/XP是版本5.x和Vista/Win7是版本6.x.
Set shell = CreateObject("WScript.Shell")
Set getOSVersion = shell.exec("%comspec% /c ver")
version = getOSVersion.stdout.readall
wscript.echo version
Select Case True
Case InStr(version, "n 5.") > 1 : GetOS = "XP"
Case InStr(version, "n 6.") > 1 : GetOS = "Vista"
Case Else : GetOS = "Unknown"
End Select
wscript.echo GetOS`
谢谢! :)我使用了case语句的修改版本,但是使用WMI对象获得了版本号:'Set oWMI = GetObject(“winmgmts:\\。\ root \ cimv2”)'...和:'Set rows = oWMI.ExecQuery(“从Win32_OperatingSystem选择版本”,48)'...接着循环“行”(仅1)以获取版本。 – Chiramisu 2014-08-23 01:33:05
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each os in oss
Wscript.Echo "Caption: " & os.Caption
Wscript.Echo "Code Set: " & os.CodeSet
Wscript.Echo "Country Code: " & os.CountryCode
Wscript.Echo "Debug: " & os.Debug
Wscript.Echo "Encryption Level: " & os.EncryptionLevel
dtmConvertedDate.Value = os.InstallDate
dtmInstallDate = dtmConvertedDate.GetVarDate
Wscript.Echo "Install Date: " & dtmInstallDate
Wscript.Echo "Licensed Users: " & os.NumberOfLicensedUsers
Wscript.Echo "Organization: " & os.Organization
Wscript.Echo "OS Language: " & os.OSLanguage
Wscript.Echo "OS Product Suite: " & os.OSProductSuite
Wscript.Echo "OS Type: " & os.OSType
Wscript.Echo "Primary: " & os.Primary
Wscript.Echo "Serial Number: " & os.SerialNumber
Wscript.Echo "Version: " & os.Version
Next
该解决方案将返回Windows_NT为WinXP中,Vista中,和Win7。我需要区分不同版本的NT。 – toddstavish 2012-08-12 17:53:59