PoshCode Logo PowerShell Code Repository

Get-WMIVersions (modification of post by halr9000 view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1628"></script>download | new post

Use this script to detect installed .NET versions on a remote server using WMI. Requires credentials and a computername.

  1. #Requires -Version 2
  2. param ( $Credential, $ComputerName )
  3.  
  4. # The official way to detect .NET versions is to look at their known location on the hard drive as per
  5. # this article: http://msdn.microsoft.com/en-us/kb/kb00318785.aspx
  6.  
  7. # thanks to David M (http://twitter.com/makovec) for the WQL
  8. $query = "select name from win32_directory where name like 'c:\\windows\\microsoft.net\\framework\\v%'"
  9.  
  10. $res = Get-WmiObject -query $query -Credential $Credential -ComputerName $ComputerName | ForEach-Object {
  11.         Split-Path $_.name -Leaf } | # returns directories
  12.                 Where-Object { $_ -like 'v*' } | # only include those that start with v
  13.                         ForEach-Object { [system.version]( $_ -replace "^v" ) } # remove "v" from the string and convert to version object
  14.  
  15. # Create hashtable with computername and version details
  16. $prop = @{
  17.         ComputerName    = $ComputerName
  18.         V1Present               = &{ if ( $res | Where-Object { $_.Major -eq 1 -and $_.Minor -eq 0 } ) { $true } }
  19.         V1_1Present             = &{ if ( $res | Where-Object { $_.Major -eq 1 -and $_.Minor -eq 1 } ) { $true } }
  20.         V2Present               = &{ if ( $res | Where-Object { $_.Major -eq 2 -and $_.Minor -eq 0 } ) { $true } }
  21.         V3_5Present             = &{ if ( $res | Where-Object { $_.Major -eq 3 -and $_.Minor -eq 5 } ) { $true } }
  22.         VersionDetails  = $res
  23. }
  24.  
  25. # Create and output PSobject using hashtable
  26. New-Object PSObject -Property $prop

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me