# Assumes 64 bit system, registry keys may be different on 32 bit systems
Function Get-Apps($ComputerName, $CompStatus, $TableView) {
    if ($CompStatus -eq "Online") {
        Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying local apps' -PercentComplete 30 -ParentId 1
        $SHSAppsPath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\SHSApps.json'
        $SHSApps = Get-Content $SHSAppsPath | ConvertFrom-Json
        $sblock = {
            $32RegPath = 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
            $64RegPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
            $apps = Get-ItemProperty $32RegPath | 
                Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru |
                Add-Member -NotePropertyName x86/x64 -NotePropertyValue 32 -PassThru 
            $apps += Get-ItemProperty $64RegPath | 
                Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru |
                Add-Member -NotePropertyName x86/x64 -NotePropertyValue 64 -PassThru 
            $apps += Get-Package | 
                Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru 
            foreach ($app in ${using:SHSApps}) {
                if (Invoke-Expression $app.Test) { 
                    $apps += [PSCustomObject]@{
                        DisplayName = $app.DisplayName
                        Computer = $env:ComputerName
                    }
                }
            }
            $apps | Sort-Object 'x86/x64',DisplayName -Unique 
        }
        #Checks if local computer
        if ($ComputerName -eq $env:COMPUTERNAME) {
            $apps = Invoke-Command -ScriptBlock $sblock | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ }
        } else {
            $apps = Invoke-Command -ComputerName $ComputerName -SessionOption $(New-PSSessionOption -MaxConnectionRetryCount 1 -NoMachineProfile) -ScriptBlock $sblock | 
                Select-Object -Property * -ExcludeProperty PSComputerName | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ }
        }
    } else {
        Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying SCCM apps' -PercentComplete 10 -ParentId 1
        $86apps, $64apps = Get-SCCM_Apps $ComputerName
        $apps = ($86apps + $64apps) | Sort-Object 'x86/x64',DisplayName -Unique | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ }
    }


    if ($TableView) {
        $apps | Out-GridView -Title "Get-PC Apps - $ComputerName"
    }
    else {
        Write-Output $apps
    }
    Write-Progress -Activity "Getting apps for $ComputerName" -Completed
}