PoshCode Archive  Artifact [a9563ed0c3]

Artifact a9563ed0c32d1f8015fc65f3a20fc0a87dcc00d7bd4f11bb35e0665a3ab5a7c5:

  • File Get-WMIVersions.ps1 — part of check-in [0213b24305] at 2018-06-10 13:35:59 on branch trunk — Use this script to detect installed .NET versions on a remote server using WMI. Requires credentials and a computername. (user: halr9000 size: 1813)

# encoding: ascii
# api: powershell
# title: Get-WMIVersions
# description: Use this script to detect installed .NET versions on a remote server using WMI. Requires credentials and a computername.
# version: 0.1
# author: halr9000
# license: CC0
# x-poshcode-id: 4077
# x-archived: 2016-09-20T12:51:27
# x-published: 2013-04-05T18:00:00
#
#
#Requires -Version 2
param ( $Credential, $ComputerName )

# The official way to detect .NET versions is to look at their known location on the hard drive as per
# this article: http://msdn.microsoft.com/en-us/kb/kb00318785.aspx

# thanks to David M (http://twitter.com/makovec) for the WQL 
$query = "select name from win32_directory where name like 'c:\\windows\\microsoft.net\\framework\\v%'"

$res = Get-WmiObject -query $query -Credential $Credential -ComputerName $ComputerName | ForEach-Object {
	Split-Path $_.name -Leaf } | # returns directories
		Where-Object { $_ -like 'v*' } | # only include those that start with v
			ForEach-Object { [system.version]( $_ -replace "^v" ) } # remove "v" from the string and convert to version object

# Create hashtable with computername and version details
$prop = @{
	ComputerName	= $ComputerName
	V1Present		= &{ if ( $res | Where-Object { $_.Major -eq 1 -and $_.Minor -eq 0 } ) { $true } }
	V1_1Present		= &{ if ( $res | Where-Object { $_.Major -eq 1 -and $_.Minor -eq 1 } ) { $true } }
	V2Present		= &{ if ( $res | Where-Object { $_.Major -eq 2 -and $_.Minor -eq 0 } ) { $true } }
	V3_5Present		= &{ if ( $res | Where-Object { $_.Major -eq 3 -and $_.Minor -eq 5 } ) { $true } }
	V4Present 		= &{ if ( $res | Where-Object { $_.Major -eq 4 -and $_.Minor -eq 0 } ) { $true } }
	VersionDetails	= $res
}

# Create and output PSobject using hashtable
New-Object PSObject -Property $prop