【问题标题】:How can I test whether or not "Microsoft Windows 7 Home Premium" is the operating system using VBScript?如何测试“Microsoft Windows 7 Home Premium”是否是使用 VBScript 的操作系统?
【发布时间】:2010-01-23 00:17:02
【问题描述】:

我的第一次尝试是查询 Win32_OperatingSystem 的标题,并测试标题是否“等于”我正在测试的操作系统:

Dim objWMIService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")


msgbox getOperatingSystemCaption()
msgbox meetsOperatingSystemRequirement()



Function getOperatingSystemCaption()

     Dim strCaption, colOperatingSystems, objOperatingSystem

     Set colOperatingSystems = objWMIService.ExecQuery _
          ("Select * from Win32_OperatingSystem")

     For Each objOperatingSystem in colOperatingSystems
        strCaption = objOperatingSystem.Caption
        Exit For 
     Next

    getOperatingSystemCaption = strCaption

End Function





Function meetsOperatingSystemRequirement()

    meetsOperatingSystemRequirement = False 

    If getOperatingSystemCaption() = "Microsoft Windows 7 Home Premium" Then 

       meetsOperatingSystemRequirement = True

    End If 


End Function

我想我可以使用 InStr,但是我仍然不明白为什么“Caption”和我的字符串不相等。

【问题讨论】:

    标签: vbscript operating-system wmi wmi-query


    【解决方案1】:

    您确定您使用的是“Microsoft Windows XP”而不是“Microsoft Windows XP Professional”吗?如果您使用“=”符号,那么您将无法捕捉到它,因为它希望匹配精确的字符串。如果您想要部分匹配,使用 instr() 会更好。否则,添加“专业”

    找到标题后可以进行一些调试

    ....
            msgbox strCaption & " " & len(strCaption)
            getOperatingSystemCaption = strCaption
    ....
    

    尝试不同的方式

    .....
        myCaption = getOperatingSystemCaption()
         msgbox myCaption & " " & len(myCaption)
        If myCaption = "Microsoft Windows XP Premium Home" Then 
    ......
    

    还要检查长度...

    【讨论】:

    • 感谢您接受。我编辑了我的问题以反映我正在尝试检查“Microsoft Windows 7 Home Premium”。出于某种原因,如果不使用 instr(),我无法让字符串匹配。可能的 MS 错误?
    • 谢谢ghostdog74!事实证明,Caption 属性在末尾填充了一个空格——但仅适用于 Microsoft Windows 7 Home Premium(到目前为止)。因此,对“Microsoft Windows 7 Home Premium”的测试是匹配的。
    • 好的,然后在从函数返回之前使用 Trim() 去除 strCaption 中的空格。应该这样做
    猜你喜欢
    • 2012-07-01
    • 2011-01-07
    • 1970-01-01
    • 2013-09-04
    • 2016-05-03
    • 2015-10-25
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多