2

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
xka
  • 53
  • 4
  • 11

2 Answers2

3

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
user700390
  • 2,287
  • 1
  • 19
  • 26
  • Will try this, could you explain why did you set it on "0". I tried running the script without framework installed and it returns nothing. – xka Mar 19 '15 at 14:47
  • I used to code a lot of C so I am just in the habit of always initializing my variables. I didn't check all the syntax or try to run the code, but conceptually this should achieve what you want. If there is no framework installed I would expect the function to return 0. – user700390 Mar 19 '15 at 16:03
0

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

webcodervk
  • 145
  • 8
  • 1
    That would only give you the framework version that was currently running, not necessarily the maximum framework version installed on the system. – user700390 Mar 20 '15 at 18:00