Detect .Net Framework 3.5 or higher using VBS thro

2019-05-23 14:52发布

问题:

I have created a script which detects installed .Net Framework installed through registry. The condition should specifically detect 3.5 or higher version and continue the process. However, using the registry it seems not possible. Every time there is a new version installed, you have to search and input the registry or modify the script just to make it works.

Then I searched it up on google that it can be done through WMI and this seems gonna work. I have modified the script to be flexible even though there are new installed .net framework higher than 3.5 it will automatically detect 3.5 or higher version installed. Unfortunately, one condition is not working if the scripts detect that there is lower version or no installed .net framework installed, the script should quit and will not continue the process.

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

回答1:

You may instead want to have a function to get the max framework version, then change the calling code to see if it returns >= the minimum required version (I have not checked the syntax on this):

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


回答2:

You can also use Environment.Version for checking the run time version instead of registry. Please refer to this documentation for more details : https://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#clr_b