2

My client doesn't have Puppet or Chef or Choco, and I can't install them on his server to install my App.

  1. Check installed versions of dotnet framework.
  2. Convert to '.' split string
  3. Convert to System.Version (some have 2 split or 3 split array
  4. Compare with desired dotnetframework version e.g. 4.6.2
  5. If a higher version is installed abort
  6. If not then install MSI/EXE of desired framework version

Step 1: Check dotnet version as System.Version:

So far I have :

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
  Get-ItemProperty -name Version, Release -EA 0 |
  Where-Object { $_.PSChildName -match 'Full'} |
  Select-Object Version

But GetType, Split, Select-String, just aren't working for me.

Errors and code tried so far:

| Select-String $_

results in: Select-String : Cannot bind argument to parameter 'Pattern' because it is null.

| {Select-String $_}

results in: Expressions are only allowed as the first element of a pipeline.

|Select-Object $_.GetType

Does not list the type of Version

|Select-Object $_.Split(".")

results in: You cannot call a method on a null-valued expression.

Edit: @Theo answer is good it can now wrap the install .net 4.6.2 logic which is:

$SourceURI = "https://download.microsoft.com/download/F/9/4/F942F07D-F26F-4F30-B4E3-EBD54FABA377/NDP462-KB3151800-x86-x64-AllOS-ENU.exe"
$FileName = $SourceURI.Split('/')[-1]
$BinPath = Join-Path $env:TEMP -ChildPath "dotnet462\$FileName"
if (!(Test-Path $BinPath))
{
    Invoke-Webrequest -Uri $SourceURI -OutFile $BinPath
}
write-verbose "Executing $binpath /q /norestart"
Sleep 5
Start-Process -FilePath $BinPath -ArgumentList "/q /norestart" -Wait -NoNewWindow
OzBob
  • 4,227
  • 1
  • 39
  • 48
  • What, exactly, does happen? Is there an error? What does it say? Wrong data returned? What, what was expected? Something else? – vonPryz Feb 24 '20 at 07:40
  • and eventually want to do something like Select-Object $_.Split(".")| Select-Object [System.Management.Automation.SemanticVersion]::New($_.[0],$_.[1],$_.[2],$_.[3]) – OzBob Feb 24 '20 at 07:51

2 Answers2

2

I agree with Moerwald to cast the registry values to [Version] objects so comparing against a minimum version will become easy.

Instead of filtering on Full straight away, I personally would like to first get an array of all installed versions and select what I need from that:

$minVersion = [version]'4.6.2'

$netVersions = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
               Get-ItemProperty -name Version, Release -ErrorAction SilentlyContinue |
               Select-Object @{Name = 'Version'; Expression = {[Version]$_.Version}}, 
                             @{Name = 'Type'; Expression = {$_.PSChildName}}

# display all installed version if you like
# $netVersions

# get the highest installed Full version
$highestVersion = $netVersions | Where-Object { $_.Type -match 'Full' } | Sort-Object | Select-Object -Last 1

# and compare that to the minimum function you need
if ($highestVersion.Version -lt $minVersion) {
    Write-Host "Installing .NET Framework $($minVersion.ToString())"
    # do the install here
}
else {
    Write-Host "Nothing to do here, client has version $($highestVersion.Version.ToString())"
}
Theo
  • 57,719
  • 8
  • 24
  • 41
1

Based on your question I guess you've the problem convert the retrieved version (from the registry). If so you can do a simple cast like that:


> $versionObject = [System.Version]( Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
>>   Get-ItemProperty -name Version, Release -EA 0 |
>>   Where-Object { $_.PSChildName -match 'Full'} |
>>   Select-Object -ExpandProperty Version)

> $versionObject

Major  Minor  Build  Revision
-----  -----  -----  --------
4      8      4084   -1

Afterward you can use $versionObject.Major, $versionObject.Minor,... for further comparison.

As can be seen, I've added -ExpandProperty to Select-Object which returns a string. The string is then casted via [System.Version] to an appropriate object.

Hope that helps.

Moerwald
  • 10,448
  • 9
  • 43
  • 83