我如何测试“是否”;“Microsoft Windows 7家庭高级版”;操作系统是否使用VBScript?
我的第一次尝试是查询Win32_OperatingSystem中的标题,并测试标题是否“等于”我正在测试的操作系统:我如何测试“是否”;“Microsoft Windows 7家庭高级版”;操作系统是否使用VBScript?,vbscript,operating-system,wmi,wmi-query,Vbscript,Operating System,Wmi,Wmi Query,我的第一次尝试是查询Win32_OperatingSystem中的标题,并测试标题是否“等于”我正在测试的操作系统: Dim objWMIService, strComputer strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") msgbox getOperatingSystemCaption() msgbox meetsOperatingS
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,但是我仍然不明白为什么“标题”和我的字符串不相等。你确定你有“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(),我无法使字符串匹配。可能是bug女士?谢谢ghostdog74!事实证明,Caption属性在结尾加了一个空格——但目前仅适用于Microsoft Windows 7 Home Premium。因此,测试“Microsoft Windows 7 Home Premium”是一个匹配项。好的,在从函数返回之前,使用Trim()清除str选项中的空格。那应该可以