Apps only queries sccm when workstation isn't online and BatchInvokes now queries CMDB instead of returning empty PC

This commit is contained in:
Zachary Gorman 2025-02-13 14:41:15 -08:00
parent 52a0aa27e6
commit c4f3efa30b
4 changed files with 31 additions and 76 deletions

View file

@ -13,13 +13,14 @@ function Get-AppDiff {
param( param(
[Parameter(Mandatory)] [Parameter(Mandatory)]
$pc, $pc,
$CompStatus = $offline,
$TableView = $false $TableView = $false
) )
# Compares a pc's apps with a list of expected apps to see if the pc has any # 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 # 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 Write-Progress -Activity "Filtering out standard apps from $pc" -Status 'Removing version numbers and loading reference' -PercentComplete 10 -ParentId 1
$apps = Remove-Version $apps $apps = Remove-Version $apps
# Open example file # Open example file

View file

@ -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 if ($CompStatus -eq "Online") {
$86apps, $64apps = Get-SCCM_Apps $ComputerName Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying local apps' -PercentComplete 30 -ParentId 1
$sblock = {
Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying local apps' -PercentComplete 30 -ParentId 1 $apps = Get-ItemProperty $32RegPath |
#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 |
Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru | Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru |
Select-Object @{N='DisplayName';E={$_.Name}},* | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 32 -PassThru
Sort-Object -Property DisplayName $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 { } else {
$localOnly = $localapps Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying SCCM apps' -PercentComplete 10 -ParentId 1
} $86apps, $64apps = Get-SCCM_Apps $ComputerName
foreach ($item in $localOnly) { $apps = ($86apps + $64apps) | Sort-Object 'x86/x64',DisplayName -Unique | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ }
$a = New-Object -TypeName PSCustomObject
$a.PSObject.TypeNames.Insert(0, 'GetPC.App')
Copy-Property -From $item -To $a
$apps += $a
} }
if ($TableView) { if ($TableView) {
$apps | Out-GridView -Title "Get-PC Apps - $ComputerName" $apps | Out-GridView -Title "Get-PC Apps - $ComputerName"
} }

View file

@ -42,30 +42,8 @@ $Headers = @{
$FindLastHardwareScanSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastHardwareScanQuery | Select-Object -ExpandProperty LastHardwareScan $FindLastHardwareScanSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastHardwareScanQuery | Select-Object -ExpandProperty LastHardwareScan
if(!$FindLastHardwareScanSCCM){ if(!$FindLastHardwareScanSCCM){
$props = [Ordered]@{ Write-Progress -Activity "Getting SCCM data for $comp" -Completed
Hostname = "$comp" return
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
} }
$Year = $FindLastHardwareScanSCCM.substring(0,4) $Year = $FindLastHardwareScanSCCM.substring(0,4)

View file

@ -531,12 +531,12 @@ begin {
#Pulls a list of installed programs from SCCM and Get-Package #Pulls a list of installed programs from SCCM and Get-Package
if ($Apps) { if ($Apps) {
Get-Apps $comp $TableView Get-Apps $comp $compStatus $TableView
continue continue
} }
if ($AppDiff) { if ($AppDiff) {
Get-AppDiff $comp $TableView Get-AppDiff $comp $compStatus $TableView
continue continue
} }