通过 WMI 使用 VBS 检测 .Net Framework 3.5 或更高版本
Detect .Net Framework 3.5 or higher using VBS through WMI
我创建了一个脚本,用于检测通过注册表安装的 .Net Framework。条件应该专门检测 3.5 或更高版本并继续该过程。但是,使用注册表似乎不可能。每次安装新版本,都必须搜索并输入注册表或修改脚本才能使其正常运行。
然后我在 google 上搜索它可以通过 WMI 完成,这似乎可行。我已将脚本修改为灵活,即使新安装的 .net 框架高于 3.5,它也会自动检测安装的 3.5 或更高版本。不幸的是,如果脚本检测到版本较低或未安装 .net 框架,则一个条件不工作,脚本应该退出并且不会继续该过程。
WriteLog "Checking if there is .Net Framework 4.5, .Net Framework 4.0 and .Net Framework 3.5 installed on the machine.."
If ((RegValueExists("HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL\{8E34682C-8118-31F1-BC4C-98CD9675E1C2}\")) AND (RegValueExists("HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL\{F5B09CFD-F0B2-36AF-8DF4-1DF6B63FC7B4}\"))) Then
WriteLog"Framework 4 detected on system. "
WriteLog "Proceeding with installation..."
ElseIf FrameworkCheck("3.5") Then
'Proceed with installation
End If
Function FrameworkCheck
Function FrameworkCheck(strVersion)
Dim strComputer, objWMIService, colItems, strVar, objItem
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name, Version from Win32_Product Where Name Like 'Microsoft .NET Framework%'")
For Each objItem in colItems
If objItem.Version => strVersion Then
WriteLog "Detected Framework Version: " & objItem.Version & " - " & objItem.Name
WriteLog "Proceeding with installation..."
ElseIf objItem.Version <> 0 Then
WriteLog "NOK-Framework 3.5 or later not detected on system. Installation not possible. Please check basic client installation"
WScript.Quit(-1)
End If
Next
End Function
您可能想要一个函数来获取最大框架版本,然后更改调用代码以查看它是否 returns >= 最低要求版本(我没有检查过这方面的语法) :
Function MaxFrameworkVersionCheck()
Dim strComputer, objWMIService, colItems, strVar, objItem, maxVersion
maxVersion = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name, Version from Win32_Product Where Name Like 'Microsoft .NET Framework%'")
For Each objItem in colItems
WriteLog "Detected Framework Version: " & objItem.Version & " - " & objItem.Name
If objItem.Version > maxVersion Then
maxVersion = objItem.Version
End If
Next
MaxFrameworkVersionCheck = maxVersion
End Function
您也可以使用 Environment.Version 检查 运行 时间版本而不是注册表。有关详细信息,请参阅此文档:https://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#clr_b
我创建了一个脚本,用于检测通过注册表安装的 .Net Framework。条件应该专门检测 3.5 或更高版本并继续该过程。但是,使用注册表似乎不可能。每次安装新版本,都必须搜索并输入注册表或修改脚本才能使其正常运行。
然后我在 google 上搜索它可以通过 WMI 完成,这似乎可行。我已将脚本修改为灵活,即使新安装的 .net 框架高于 3.5,它也会自动检测安装的 3.5 或更高版本。不幸的是,如果脚本检测到版本较低或未安装 .net 框架,则一个条件不工作,脚本应该退出并且不会继续该过程。
WriteLog "Checking if there is .Net Framework 4.5, .Net Framework 4.0 and .Net Framework 3.5 installed on the machine.."
If ((RegValueExists("HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL\{8E34682C-8118-31F1-BC4C-98CD9675E1C2}\")) AND (RegValueExists("HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL\{F5B09CFD-F0B2-36AF-8DF4-1DF6B63FC7B4}\"))) Then
WriteLog"Framework 4 detected on system. "
WriteLog "Proceeding with installation..."
ElseIf FrameworkCheck("3.5") Then
'Proceed with installation
End If
Function FrameworkCheck
Function FrameworkCheck(strVersion)
Dim strComputer, objWMIService, colItems, strVar, objItem
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name, Version from Win32_Product Where Name Like 'Microsoft .NET Framework%'")
For Each objItem in colItems
If objItem.Version => strVersion Then
WriteLog "Detected Framework Version: " & objItem.Version & " - " & objItem.Name
WriteLog "Proceeding with installation..."
ElseIf objItem.Version <> 0 Then
WriteLog "NOK-Framework 3.5 or later not detected on system. Installation not possible. Please check basic client installation"
WScript.Quit(-1)
End If
Next
End Function
您可能想要一个函数来获取最大框架版本,然后更改调用代码以查看它是否 returns >= 最低要求版本(我没有检查过这方面的语法) :
Function MaxFrameworkVersionCheck()
Dim strComputer, objWMIService, colItems, strVar, objItem, maxVersion
maxVersion = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name, Version from Win32_Product Where Name Like 'Microsoft .NET Framework%'")
For Each objItem in colItems
WriteLog "Detected Framework Version: " & objItem.Version & " - " & objItem.Name
If objItem.Version > maxVersion Then
maxVersion = objItem.Version
End If
Next
MaxFrameworkVersionCheck = maxVersion
End Function
您也可以使用 Environment.Version 检查 运行 时间版本而不是注册表。有关详细信息,请参阅此文档:https://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#clr_b