NewRelic.Agents.psm1

<#
.Synopsis
    Confirms the user is running as an admin in an elevated session.
.Description
    Confirms the user is running as an admin in an elevated session. If not throws an error.
.Example
    Confirm-RunAsAdmin
#>

Function Confirm-RunAsAdmin {
    [CMDLetBinding()]
    [OutputType([System.Boolean])]
    Param (
    )
    if([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")){
        return $true
    }else{
        Throw "You must run this as an admin and in an elevated session to proceed."
    }
}

<#
.Synopsis
    Combines 2 HashTables into one HashTable
.Description
    Combines 2 HashTables into one HashTable. If they have duplicate keys the override value will be used.
.Example
    Join-HashTable -Base $Hash -Override $Hash2
#>

Function Join-HashTable {
    [CMDLetBinding()]
    [OutputType([System.Collections.Hashtable])]
    Param (
        [Parameter (Mandatory = $true)]
        [Hashtable] $Base,
        [Parameter (Mandatory = $true)]
        [Hashtable] $Override
    )

    # Copy keys into new hash not to modify the original
    $returnHash = @{}
    foreach ($key in $Base.Keys) {
        $returnHash.Add($key, $Base[$key])
    }

    foreach ($key in $Override.Keys) {
        if($null -eq $returnHash[$key]){
            Write-Verbose "Adding settings $key"
            $returnHash.Add($key, $Override[$key])
        }else{
            if($returnHash[$key] -ne $Override[$key]){
                Write-Verbose "Updating setting $key"
                $returnHash[$key] = $Override[$key]
            }
        }
    }

    return $returnHash
}

<#
.Synopsis
    Gets installed software from the registry
.Description
    Gets installed software from the registry at HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
.Example
    Get-InstalledSoftwareFromRegistry
.Example
    Get-InstalledSoftwareFromRegistry -Name 'New Relic .NET Agent*'
.Parameter Name
    Filters the list of software based on the name provided, can use wildcards
#>

Function Get-InstalledSoftwareFromRegistry {
    [CMDLetBinding()]
    Param (
        [Parameter (Mandatory = $false)]
        [String] $Name = '*'
    )

    $installList = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' | Select-Object Name

    $Software = @()
    foreach ($install in $installList) {
        $properties = Get-ItemProperty $install.Name.Replace('HKEY_LOCAL_MACHINE', 'HKLM:')

        if($properties -and $properties.DisplayName){
            $Software += [PSCustomObject]@{
                DisplayName = $properties.DisplayName
                DisplayVersion = $properties.DisplayVersion
            }
        }
    }

    Return $Software | Where-Object {$_.DisplayName -like $Name}
}