DotNetVersionLister.psm1

function Get-DotNetVersion {
    <#
    .SYNOPSIS
        Get installed .NET versions from remote computers.
        Hardcoded .NET versions so the script will
        need to be updated regularly (when new versions are released).
                 
    .DESCRIPTION
        Uses remote registry access or PSRemoting.
     
        Online documenation here:
        http://www.powershelladmin.com/wiki/Script_for_finding_which_dot_net_versions_are_installed_on_remote_workstations
             
        If you use the -PSRemoting parameter against computers that don't have .NET 4.0
        or higher installed, you will get this error:
        Unable to open registry: Method invocation failed because [Microsoft.Win32.RegistryKey] doesn't contain a method named 'OpenBaseKey'.
         
    .PARAMETER ComputerName
        Target computers to retrieve .NET versions from via remote registry access or PSRemoting.
    .PARAMETER PSRemoting
        Use PowerShell remoting instead of remote registry access. Remote registry access requires RPC,
        which in turn requires lots of firewall openings. PSRemoting requires .NET 4.0 on the target
        computer to work properly, otherwise you'll get the error you can read more about in the module's description.
    .PARAMETER ExportToCSV
        Export to a CSV file as well as files containing online and offline computers.
    .PARAMETER ContinueOnPingFail
        Try to gather even if the remote computer does not reply to ping.
    .PARAMETER NoSummary
        Do not display the end summary with Write-Host.
    .PARAMETER Clobber
        Only in use with -ExportToCSV. Overwrite potentially existing files without prompting.
        Date and time is in the file name by default.
    .EXAMPLE
        Get-DotNetVersion adminsrv1 -Verbose
        VERBOSE: Script start time: 02/09/2017 13:28:44
        VERBOSE: adminsrv1 is online.
        VERBOSE: adminsrv1: Successfully connected to registry.
 
 
        ComputerName : adminsrv1
        >=4.x : 4.5.2 or later
        v4\Client : Installed
        v4\Full : Installed
        v3.5 : Installed
        v3.0 : Installed
        v2.0.50727 : Installed
        v1.1.4322 : Not installed (no key)
        Ping : True
        Error :
 
            Script start time: 02/09/2017 13:28:44
            Script end time: 02/09/2017 13:28:45
    .EXAMPLE
        PS D:\> Get-DotNetVersion adminsrv1 -Verbose -PSRemoting
        VERBOSE: Script start time: 02/09/2017 13:36:48
        VERBOSE: adminsrv1 is online.
 
 
        ComputerName : adminsrv1
        >=4.x : 4.5.2 or later
        v4\Client : Installed
        v4\Full : Installed
        v3.5 : Installed
        v3.0 : Installed
        v2.0.50727 : Installed
        v1.1.4322 : Not installed (no key)
        Ping :
        Error :
 
            Script start time: 02/09/2017 13:36:48
            Script end time: 02/09/2017 13:36:50
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][Alias('Cn', 'PSComputerName')][string[]] $ComputerName,
        [switch] $PSRemoting,
        [switch] $ExportToCSV,
        [switch] $ContinueOnPingFail,
        [switch] $NoSummary,
        [switch] $Clobber)
    ## Author: Joakim Svendsen
    ## Copyright (C) 2011, Joakim Svendsen
    ## All rights reserved.
    ## BSD 3-clause license
    # 2016-01-13: v1.2 - Added support for .NET 4.6.1.
    # 2016-05-29: v1.3 - Code quality improvements, standardization.
    # 2016-10-10: v1.4 - Added support for .NET 4.6.2.
    # 2017-02-06: v1.5 - Making it a function and module, and more standards-compliant (return objects).
    # Adding the parameters -ExportToCSV, -PSRemoting, -ContinueOnPingFail and -NoSummary.
    # Lots of small changes and improvements. Properly closing and disposing registry objects.
    # Added a [gc]::Collect() in the end block.
    begin {
        Set-StrictMode -Version Latest
        $MyEAP = 'Stop'
        $ErrorActionPreference = $MyEAP
        $StartTime = [datetime]::Now
        function ql { $args }
        Write-Verbose -Message "Script start time: $StartTime" #-Verbose
        if ($ExportToCSV) {
            $Date = $StartTime.ToString('yyyy-MM-dd_HH.mm')
            $OutputOnlineFile  = ".\DotNetOnline-${Date}.txt"
            $OutputOfflineFile = ".\DotNetOffline-${Date}.txt"
            $CsvOutputFile = ".\DotNet-Versions-${Date}.csv"
            if (-not $Clobber) {
                $FoundExistingLog = $false
                foreach ($File in $OutputOnlineFile, $OutputOfflineFile, $CsvOutputFile) {
                    if (Test-Path -PathType Leaf -Path $File) {
                        $FoundExistingLog = $true
                        "$File already exists"
                    }
                }
                if ($FoundExistingLog -eq $true) {
                    $Answer = Read-Host "The above mentioned log file(s) exist. Overwrite? [yes]"
                    if ($Answer -imatch '^n') {
                        Write-Error -Message 'User aborted due to not wanting to overwrite existing files' -ErrorAction Stop
                        exit 1 # should be redundant
                    }
                }
            }
            # Deleting existing log files if they exist (assume they can be deleted...)
            Remove-Item $OutputOnlineFile -ErrorAction SilentlyContinue
            Remove-Item $OutputOfflineFile -ErrorAction SilentlyContinue
            Remove-Item $CsvOutputFile -ErrorAction SilentlyContinue
        }
        $Counter    = 0
        $DotNetData = @{}
        $DotNetVersionStrings = ql v4\Client v4\Full v3.5 v3.0 v2.0.50727 v1.1.4322
        $DotNetRegistryBase   = 'SOFTWARE\Microsoft\NET Framework Setup\NDP'
        function SetDataHashObject {
            [CmdletBinding()]
            param(
                [string] $Computer,
                [bool] $PSRemoting,
                [string[]] $DotNetVersionStrings)
            if ($PSRemoting) {
                $DotNetData = @{}
                $DotNetData.$Computer = New-Object -TypeName PSObject
                $DotNetRegistryBase   = 'SOFTWARE\Microsoft\NET Framework Setup\NDP'
            }
            $ErrorActionPreference = 'Stop'
            $RegSuccess = $false
            try {
                if ($PSRemoting) {
                    # Requires .NET 4.0 to use OpenBaseKey()
                    $Registry = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Default')
                    $RegSuccess = $?
                }
                else {
                    $Registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer)
                    $RegSuccess = $?
                }
            }
            catch {
                #$OldError = $_.Exception.Message
                if ($_.ToString() -like "*Method invocation failed because *Microsoft.Win32.RegistryKey* doesn't contain a method named 'OpenBaseKey'.*") {
                    Write-Verbose -Message "${Computer}: Caught an exception indicating the computer doesn't have .NET 4.0 or higher, so it's missing the 'OpenBaseKey' method in the registry class. Going to try to hack it with reg.exe instead."
                    #$ErrorActionPreference = 'Stop' # already "Stop"
                    try {
                        #$Registry = [Microsoft.Win32.RegistryKey]::OpenSubKey('LocalMachine', 'Software\Microsoft\NET Framework Setup\NDP\')
                        #$RegSuccess = $?
                        if (Get-Command -Name reg -ErrorAction SilentlyContinue) {
                            #Write-Verbose -Message "${Computer}: Found reg.exe"
                            foreach ($DotNetVersion in $DotNetVersionStrings) {
                                $ErrorActionPreference = 'SilentlyContinue'
                                $Output = reg.exe query "HKLM\$DotNetRegistryBase\$DotNetVersion" /v Install
                                $ErrorActionPreference = 'Stop'
                                if ($Output -match '\s*Install\s+REG_DWORD\s+0x1') {
                                    $Value = 'Installed'
                                }
                                else {
                                    $Value = "Not installed. Key value: '$($Output -split '[\r\n]+' -join '; ')'"
                                }
                                $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name $DotNetVersion -Value $Value
                            }
                        }
                        else {
                            Write-Warning -Message "${Computer}: Didn't find reg.exe on remote computer and got this error when trying to open registry with .NET: $_"
                            $DotNetData.$Computer | Add-Member -Name Error -Value "Didn't find reg.exe on remote computer and got this error when trying to open registry with .NET: $_" -MemberType NoteProperty
                            return $DotNetData.$Computer
                        }
                    }
                    catch {
                        Write-Warning -Message "${Computer}: Unable to open $(if (-not $PSremoting) { 'remote ' })registry: $_"
                        $DotNetData.$Computer | Add-Member -Name Error -Value "Unable to open remote registry: $_" -MemberType NoteProperty
                        return $DotNetData.$Computer
                    }
                    #$ErrorActionPreference = $MyEAP
                }
                else {#> # wasn't the usual error
                    Write-Warning -Message "${Computer}: Unable to open $(if (-not $PSremoting) { 'remote ' })registry: $_"
                    $DotNetData.$Computer | Add-Member -Name Error -Value "Unable to open $(if (-not $PSremoting) { 'remote ' })registry: $_" -MemberType NoteProperty
                    return $DotNetData.$Computer
                }
                if ($PSRemoting) {
                    return $DotNetData.$Computer # return this to the calling scope, populate the other data hash there, pretty hacky, this
                }
            }
            $ErrorActionPreference = 'Continue'
            Write-Verbose -Message "${Computer}: Successfully connected to registry."
            foreach ($VerString in $DotNetVersionStrings) {
                if ($RegKey = $Registry.OpenSubKey("$DotNetRegistryBase\$VerString")) {
                    if ($RegKey.GetValue('Install') -eq '1') {
                        Add-Member -Name $VerString -Value 'Installed' -MemberType NoteProperty -InputObject $DotNetData.$Computer
                    }
                    else {
                        Add-Member -Name $VerString -Value 'Not installed' -MemberType NoteProperty -InputObject $DotNetData.$Computer
                    }
                }
                else {
                    Add-Member -Name $VerString -Value 'Not installed (no key)' -MemberType NoteProperty -InputObject $DotNetData.$Computer
                }
            }
            # Tacking on 4.5.x and 4.6 detection, as someone requested... this script really needs a rewrite to be
            # more standards-conforming, but I'm mentally exhausted.
            # https://msdn.microsoft.com/en-us/library/hh925568 - for release numbers
            # 2016-01-13: Adding 4.6.1.
            # 2016-10-10: Added 4.6.2. (rewrote parts earlier).
            # 2017-02-06: Changing to a switch statement as part of rewriting to a module/function and adding features.
            if ($RegKey) {
                $RegKey.Close()
                $RegKey.Dispose()
            }
            $RegKey = $null
            if ($RegKey = $Registry.OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full")) {
                if ($DotNet4xRelease = [int] $RegKey.GetValue('Release')) {
                    switch ($DotNet4xRelease) {
                        394806 { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value '4.6.2 or later'; break }
                        394254 { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value '4.6.1 or later'; break }
                        393295 { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value '4.6 or later'; break }
                        379893 { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value '4.5.2 or later'; break }
                        378675 { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value '4.5.1 or later'; break }
                        378389 { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value '4.5 or later'; break }
                        'default' { $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value 'Universe imploded'; break }
                    }
                }
                else {
                    $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value "Error (no 'Release' key?)"
                }
            }
            else {
                $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name '>=4.x' -Value 'Not installed (no key)'
            }
            if ($RegKey) {
                $RegKey.Close()
                $RegKey.Dispose()
            }
            $Registry.Close()
            $Registry.Dispose()
            $RegKey, $Registry = $null, $null
            if ($PSRemoting) {
                $DotNetData.$Computer # return this to the calling scope, populate the other data hash there, pretty hacky, this
            }
        }
    }
    process {
        foreach ($Computer in $ComputerName) {
            $Counter++
            $DotNetData.$Computer = New-Object -TypeName PSObject
            # This one is for the latched-on PSRemoting feature ...
            $PingReply = $false
            if (Test-Connection -Quiet -Count 1 $Computer) {
                $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name Ping -Value $true
                $PingReply = $true
                Write-Verbose -Message "$Computer is online."
                if ($ExportToCSV) {
                    $Computer | Add-Content $OutputOnlineFile
                }
            }
            else {
                $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name Ping -Value $false
                $PingReply = $false # explicitly..
                if ($ExportToCSV) {
                    $Computer | Add-Content $OutputOfflineFile
                }
                if (-not $ContinueOnPingFail) {
                    $DotNetData.$Computer | Add-Member -Name Error -Value "No ping reply" -MemberType NoteProperty
                    Write-Warning -Message "${Computer} is offline (no ping / ICMP echo reply)."
                    continue
                }
            }
            # Monkey patching on PSRemoting to the existing design ...
            if ($PSRemoting) {
                try {
                    $DotNetData.$Computer = Invoke-Command -ComputerName $Computer -ScriptBlock (Get-Item function:\SetDataHashObject).ScriptBlock -ArgumentList $Computer, $true, $DotNetVersionStrings -ErrorAction Stop
                    # -Verbose:$(if ($VerbosePreference -match 'Stop|Continue') { $true } else { $false })
                }
                catch {
                    $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name Error -Value "PSRemoting failure: $_"
                }
                $DotNetData.$Computer | Add-Member -MemberType NoteProperty -Name Ping -Value $PingReply -Force
            }
            else {
                SetDataHashObject -Computer $Computer -PSRemoting:$PSRemoting -DotNetVersionStrings $DotNetVersionStrings
            }
        }
    }
    end {
        $CsvHeaders = @('>=4.x') + @($DotNetVersionStrings) + @('Ping', 'Error')
        $DotNetData.GetEnumerator() | Sort -Property Name | foreach {
            $c = $_.Name
            $_.Value | Select -Property $CsvHeaders
        } | Select @{n='ComputerName';e={$c}}, * # pass to pipeline instead #| Export-Csv -Encoding UTF8 -LiteralPath $CsvOutputFile
        if ($ExportToCSV) {
            $DotNetData.GetEnumerator() | Sort -Property Name | foreach {
                $c = $_.Name
                $_.Value | Select -Property $CsvHeaders
            } | Select @{n='ComputerName';e={$c}}, * | Export-Csv -Encoding UTF8 -LiteralPath $CsvOutputFile
        }
        [gc]::Collect()
        if (-not $NoSummary) {
    Write-Host -ForegroundColor Green @"
    Script start time: $StartTime
    Script end time: $(Get-Date)
    $(if ($ExportToCSV) {
    "Output files: $CsvOutputFile, $OutputOnlineFile, $OutputOfflineFile"
    })
"@

        }
    }
}