PoshCode Logo PowerShell Code Repository

Get-WMIVersions by halr9000 24 months ago
View followups from halr9000 | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1627"></script>download | new post

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

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