Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getdotnetversion.ps

(view source code of getdotnetversion.ps as plain text)

  1. param(
  2. 		[ValidatePattern("^([2-4](\.[0-9](\.[0-9][0-9.]*)?)?)?$")]
  3. 		[string]$requiredVersion = "1",
  4. 		[parameter( ValueFromRemainingArguments = $true )]
  5. 		[string[]]$ignoredArgs,
  6. 		[switch]$h
  7. )
  8.  
  9. function Compare-Versions {
  10. 	param(
  11. 		[parameter( Mandatory = $true )]
  12. 		[string]$vera,
  13. 		[parameter( Mandatory = $true )]
  14. 		[string]$verb
  15. 	)
  16.  
  17. 	# [version] cast throws errors on some strings that LOOK acceptable, so
  18. 	# we'll just split the version strings at the dots and compare each "digit"
  19.  
  20. 	# add 3 "digits" so we'll have at least 4 "digits" to compare
  21. 	$vera   = "$vera.0.0.0"
  22. 	$verb   = "$verb.0.0.0"
  23. 	# convert to arrays that can be iterated through
  24. 	$array1 = $vera -split "\."
  25. 	$array2 = $verb -split "\."
  26. 	# default if versions match
  27. 	$result = 0;
  28. 	for ( $i = 0; $i -lt 4; $i++ ) {
  29. 		if ( $array1[$i] -gt $array2[$i] ) {
  30. 			# vera greater than verb
  31. 			$result = 1
  32. 			# abort to ignore "digits" of lesser significance
  33. 			break
  34. 		} elseif ( $array1[$i] -lt $array2[$i] ) {
  35. 			# verb greater than vera
  36. 			$result = -1
  37. 			# abort to ignore "digits" of lesser significance
  38. 			break
  39. 		}
  40. 	}
  41. 	$result
  42. }
  43.  
  44. function Get-Version {
  45. 	# return the "Version" value if it exists at the current registry path
  46. 	Get-ChildItem -Path . | ForEach-Object {
  47. 		$_ | Get-ItemProperty -Name "Version" -ErrorAction SilentlyContinue
  48. 	}
  49. }
  50.  
  51. if ( $h -or $ignoredArgs ) {
  52. 	Write-Host
  53. 	Write-Host "GetDotNETVersion.ps1,  Version 1.00"
  54. 	Write-Host "List the .NET Framework versions installed in Windows"
  55. 	Write-Host
  56. 	Write-Host "Usage:  powershell  ./GetDotNETVersion.ps1  [ required ]"
  57. 	Write-Host
  58. 	Write-Host "Where:  required    is the minimum version required"
  59. 	Write-Host "                    (2..4.9.*, e.g. 3 or 3.5.30729.5420 or 4.6)"
  60. 	Write-Host
  61. 	Write-Host "Note:   The return code will be 0 if the script completed"
  62. 	Write-Host "        successfully, 1 if the minimum version requirement"
  63. 	Write-Host "        is not met or in case of (command line) errors"
  64. 	Write-Host
  65. 	Write-Host "Written by Rob van der woude"
  66. 	Write-Host "http://www.robvanderwoude.com"
  67. 	Write-Host
  68.  
  69. 	Exit 1
  70. } else {
  71. 	# initial location, to be restored when done
  72. 	$startlocation = ( Get-Location )
  73.  
  74. 	# is a minimumrequirement set?
  75. 	[boolean]$reqVerSet = ( $requiredVersion -ne "1" )
  76.  
  77. 	# .NET List<string> object to contain all unique version strings we will find
  78. 	[Collections.Generic.List[String]]$uniqueVersions = @( )
  79.  
  80. 	# set the starting location in the registry from where we will search for version strings
  81. 	Set-Location -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP"
  82. 	# iterate through all subkeys recursively
  83. 	Get-ChildItem -Path . -Recurse | ForEach-Object -ErrorAction SilentlyContinue {
  84. 		$regpath = ( "Registry::" + $_.Name )
  85. 		Set-Location -Path $regpath
  86. 		# a separate function was used here because the sript
  87. 		# often tripped on the nested ForEach-Object invocations
  88. 		$version = ( Get-Version ).Version
  89. 		if ( $version ) {
  90. 			# in case more than one version is presented in the string...
  91. 			$version -split "\s" | ForEach-Object {
  92. 				# prevent duplicates, add unique versions only
  93. 				if ( -not $uniqueVersions.Contains( $_ ) ) {
  94. 					$uniqueVersions.Add( $_ )
  95. 				}
  96. 			}
  97. 		}
  98. 	}
  99.  
  100. 	# sort the list
  101. 	$uniqueVersions.Sort( )
  102.  
  103. 	# worst case return code
  104. 	$rc = 1
  105.  
  106. 	# Display the results
  107. 	$uniqueVersions | ForEach-Object {
  108. 		if ( $reqVerSet ) {
  109. 			if ( ( Compare-Versions $_ $requiredVersion ) -gt -1 ) {
  110. 				# matches minimum requirement: show in green
  111. 				Write-Host $_ -ForegroundColor Green
  112. 				# at least one match, hence return code 0
  113. 				$rc = 0
  114. 			} else {
  115. 				# does not match minimum requirement: show in red
  116. 				Write-Host $_ -ForegroundColor Red
  117. 			}
  118. 		} else {
  119. 			Write-Host $_
  120. 			# at least one match, hence return code 0
  121. 			$rc = 0
  122. 		}
  123. 	}
  124.  
  125. 	# restore initial location
  126. 	Set-Location $startlocation
  127.  
  128. 	Exit $rc
  129. }

page last modified: 2024-04-16; loaded in 0.0244 seconds