From c4f3efa30bba78fd6369fd2a01d48b63011dab27 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Thu, 13 Feb 2025 14:41:15 -0800 Subject: [PATCH] Apps only queries sccm when workstation isn't online and BatchInvokes now queries CMDB instead of returning empty PC --- Get-PC/Private/AppDiff.ps1 | 3 +- Get-PC/Private/Apps.ps1 | 74 +++++++++++-------------------- Get-PC/Private/SCCMQueryBlock.ps1 | 26 +---------- Get-PC/Public/Get-PC.ps1 | 4 +- 4 files changed, 31 insertions(+), 76 deletions(-) diff --git a/Get-PC/Private/AppDiff.ps1 b/Get-PC/Private/AppDiff.ps1 index ef8b5dc..cc52e4b 100644 --- a/Get-PC/Private/AppDiff.ps1 +++ b/Get-PC/Private/AppDiff.ps1 @@ -13,13 +13,14 @@ function Get-AppDiff { param( [Parameter(Mandatory)] $pc, + $CompStatus = $offline, $TableView = $false ) # Compares a pc's apps with a list of expected apps to see if the pc has any # extra apps that we need to consider downloading - $apps = Get-Apps $pc + $apps = Get-Apps $pc $CompStatus Write-Progress -Activity "Filtering out standard apps from $pc" -Status 'Removing version numbers and loading reference' -PercentComplete 10 -ParentId 1 $apps = Remove-Version $apps # Open example file diff --git a/Get-PC/Private/Apps.ps1 b/Get-PC/Private/Apps.ps1 index 81b64c7..f6d8f4d 100644 --- a/Get-PC/Private/Apps.ps1 +++ b/Get-PC/Private/Apps.ps1 @@ -1,58 +1,34 @@ -Function Get-Apps($ComputerName, $TableView) { +# Assumes 64 bit system, registry keys may be different on 32 bit systems +Function Get-Apps($ComputerName, $CompStatus, $TableView) { + $32RegPath = 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' + $64RegPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' - Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying SCCM apps' -PercentComplete 10 -ParentId 1 - $86apps, $64apps = Get-SCCM_Apps $ComputerName - - Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying local apps' -PercentComplete 30 -ParentId 1 - #Checks if local computer - if ($ComputerName -eq $env:COMPUTERNAME) { - $localapps = Get-Package -ProviderName Programs -IncludeWindowsInstaller | - Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru | - Select-Object @{N='DisplayName';E={$_.Name}},* | - Sort-Object -Property DisplayName - } else { - #Checks if pc is online so Invoke doesn't hang on offline pc - if (Test-Connection -ComputerName $ComputerName -Count 1) { - $localapps = Invoke-Command -ComputerName $ComputerName -SessionOption $(New-PSSessionOption -MaxConnectionRetryCount 1 -NoMachineProfile) -ScriptBlock { - Get-Package -ProviderName Programs -IncludeWindowsInstaller | + if ($CompStatus -eq "Online") { + Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying local apps' -PercentComplete 30 -ParentId 1 + $sblock = { + $apps = Get-ItemProperty $32RegPath | Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru | - Select-Object @{N='DisplayName';E={$_.Name}},* | - Sort-Object -Property DisplayName - } + 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 + $apps | Sort-Object 'x86/x64',DisplayName -Unique | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ } + } + #Checks if local computer + if ($ComputerName -eq $env:COMPUTERNAME) { + $apps = Invoke-Command -ScriptBlock $sblock + } else { + $apps = Invoke-Command -ComputerName $ComputerName -SessionOption $(New-PSSessionOption -MaxConnectionRetryCount 1 -NoMachineProfile) -ScriptBlock $sblock } - } - - Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Combining apps from SCCM' -PercentComplete 50 -ParentId 1 - $out = $86apps + $64apps - $apps = @() - foreach ($item in $out) { - $a = New-Object -TypeName PSCustomObject - $a.PSObject.TypeNames.Insert(0, 'GetPC.App') - Copy-Property -From $item -To $a - $apps += $a - } - - # Blank entry for visual break - $a = [PSCustomObject]@{ - DisplayName = "" - } - $a.PSObject.TypeNames.Insert(0, 'GetPC.App') - $apps += $a - - Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Combining apps from local query' -PercentComplete 75 -ParentId 1 - # Grab apps that are only returned by local query - if ($out) { - $localOnly = Compare-Object $localapps $out -Property DisplayName -PassThru | Where-Object {$_.SideIndicator -eq '<='} } else { - $localOnly = $localapps - } - foreach ($item in $localOnly) { - $a = New-Object -TypeName PSCustomObject - $a.PSObject.TypeNames.Insert(0, 'GetPC.App') - Copy-Property -From $item -To $a - $apps += $a + 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" } diff --git a/Get-PC/Private/SCCMQueryBlock.ps1 b/Get-PC/Private/SCCMQueryBlock.ps1 index 0f83d64..c18cdd8 100644 --- a/Get-PC/Private/SCCMQueryBlock.ps1 +++ b/Get-PC/Private/SCCMQueryBlock.ps1 @@ -42,30 +42,8 @@ $Headers = @{ $FindLastHardwareScanSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastHardwareScanQuery | Select-Object -ExpandProperty LastHardwareScan if(!$FindLastHardwareScanSCCM){ - $props = [Ordered]@{ - Hostname = "$comp" - Status = "$compStatus" - 'Current User' = $null - 'Last User(s)' = $null - 'IP | MAC' = $null - Model = $null - 'OS' = $null - 'OS Build' = $null - 'BIOS Ver' = $null - Encryption = $null - 'Free Space' = $null - RAM = $null - 'SSO Client' = $null - 'Kiosk Role' = $null - 'Asset Tag' = $null - 'Service Tag' = $null - 'Last Reboot' = $null - Printers = $null - } - - $obj = New-Object -TypeName PSObject -Property $props - - return $obj + Write-Progress -Activity "Getting SCCM data for $comp" -Completed + return } $Year = $FindLastHardwareScanSCCM.substring(0,4) diff --git a/Get-PC/Public/Get-PC.ps1 b/Get-PC/Public/Get-PC.ps1 index 06a405f..5da0561 100644 --- a/Get-PC/Public/Get-PC.ps1 +++ b/Get-PC/Public/Get-PC.ps1 @@ -531,12 +531,12 @@ begin { #Pulls a list of installed programs from SCCM and Get-Package if ($Apps) { - Get-Apps $comp $TableView + Get-Apps $comp $compStatus $TableView continue } if ($AppDiff) { - Get-AppDiff $comp $TableView + Get-AppDiff $comp $compStatus $TableView continue }