Fixed SCCM queries, woopsie!

This commit is contained in:
Zachary Gorman 2024-07-30 17:26:27 -07:00
parent c7c4be8a6f
commit 43a85d110a
5 changed files with 76 additions and 49 deletions

View file

@ -12,7 +12,7 @@
RootModule = 'Get-PC.psm1'
# Version number of this module.
ModuleVersion = '0.3.16'
ModuleVersion = '0.3.17'
# Supported PSEditions
# CompatiblePSEditions = @()

View file

@ -348,25 +348,28 @@ function Get-PCBatchInvoke {
}
}
BatchInvokesProgressBar -stage 2
$jobs = Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) -AsJob
$invokeJob = Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) -AsJob
BatchInvokesProgressBar -stage 3
$offlineJobs = @()
if($OfflineComputers.count -gt 0){
foreach($computer in $OfflineComputers){
$jobs += Start-Job -ScriptBlock ${function:SCCMQueryBlock} -ArgumentList $computer
$offlineJobs += Start-Job -ScriptBlock ${function:Get-SCCMQueryBlock} -ArgumentList $computer
}
}
BatchInvokesProgressBar -stage 4
$output += $jobs | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName, PSSourceJobInstanceId
$output += $invokeJob | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName, PSSourceJobInstanceId
$output += $offlineJobs | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName, PSSourceJobInstanceId
$itemIndex = 0
$contactedhosts = $output.Hostname
BatchInvokesProgressBar -stage 5
$missedhosts = $OnlineComputers | Where-Object -FilterScript {$_ -notin $contactedhosts}
$missedJobs = @()
if($missedhosts.count -gt 0){
foreach($computer in $missedhosts){
$jobs += Start-Job -ScriptBlock ${function:SCCMQueryBlock} -ArgumentList $computer
$missedJobs += Start-Job -ScriptBlock ${function:Get-SCCMQueryBlock} -ArgumentList $computer
}
}
$output += $jobs | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName, PSSourceJobInstanceId
$output += $missedJobs | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName, PSSourceJobInstanceId
return $output

View file

@ -1,14 +1,14 @@
#CONST
$SITENAME="100"
$SCCMSERVER="shscm01.int.samhealth.net"
$SCCMNAMESPACE="root\sms\site_100"

Function Get-SCCMQueryBlock {
param (
[string]$comp,
[int]$NumberofComputers,
[int]$PCID
)
#CONST
$SITENAME="100"
$SCCMSERVER="shscm01.int.samhealth.net"
$SCCMNAMESPACE="root\sms\site_100"
if(!(Test-Connection -ComputerName $comp -Count 1)){
@ -26,9 +26,11 @@ Function Get-SCCMQueryBlock {
}
$i = 0
$i++ | ProgressBar $i $comp "SCCM Last Hardware Scan" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Last Hardware Scan" -PercentComplete (($i / 18) * 100)
#Last Hardware Scan
$FindLastHardwareScanQuery = "select SMS_G_System_WORKSTATION_STATUS.LastHardwareScan from SMS_R_System inner join SMS_G_System_WORKSTATION_STATUS on SMS_G_System_WORKSTATION_STATUS.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
$FindLastHardwareScanQuery = "select SMS_G_System_WORKSTATION_STATUS.LastHardwareScan from SMS_R_System inner join SMS_G_System_WORKSTATION_STATUS on SMS_G_System_WORKSTATION_STATUS.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindLastHardwareScanSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastHardwareScanQuery | Select-Object -ExpandProperty LastHardwareScan
@ -73,81 +75,93 @@ Function Get-SCCMQueryBlock {
return $obj
}
$i++ | ProgressBar $i $comp "Last User SCCM" $NumberofComputers $PCID
$SCCMLastUserLogOnQuery = "select SMS_R_System.LastLogonUserName from SMS_R_System where SMS_R_System.Name = '$ComputerName'"
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "Last User SCCM" -PercentComplete (($i / 18) * 100)
$SCCMLastUserLogOnQuery = "select SMS_R_System.LastLogonUserName from SMS_R_System where SMS_R_System.Name = '$comp'"
$LastUserSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $SCCMLastUserLogOnQuery |
Select-Object -ExpandProperty LastLogonUserName
$i++ | ProgressBar $i $comp "SCCM IP Query" $NumberofComputers $PCID
$FindIPQuery = "select SMS_R_System.IPAddresses from SMS_R_System where SMS_R_System.Name = '$ComputerName'"
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM IP Query" -PercentComplete (($i / 18) * 100)
$FindIPQuery = "select SMS_R_System.IPAddresses from SMS_R_System where SMS_R_System.Name = '$comp'"
$FindIPSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindIPQuery |
Select-Object -ExpandProperty IPAddresses
if($FindIPSCCM.Count -gt 1){
$FindIPSCCM = $FindIPSCCM[0]
}
$i++ | ProgressBar $i $comp "Find Model SCCM" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "Find Model SCCM" -PercentComplete (($i / 18) * 100)
$FindModelQuery = "select SMS_G_System_COMPUTER_SYSTEM.Model from SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on
SMS_G_System_COMPUTER_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_COMPUTER_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$CompModelSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindModelQuery |
Select-Object -ExpandProperty Model
$i++ | ProgressBar $i $comp "SCCM Bios Version" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Bios Version" -PercentComplete (($i / 18) * 100)
$FindBiosVerQuery = "select SMS_G_System_PC_BIOS.SMBIOSBIOSVersion from SMS_R_System inner join SMS_G_System_PC_BIOS on
SMS_G_System_PC_BIOS.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_PC_BIOS.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindBiosVerSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindBiosVerQuery |
Select-Object -ExpandProperty SMBIOSBIOSVersion
$i++ | ProgressBar $i $comp "SCCM HDD/SSD Space" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM HDD/SSD Space" -PercentComplete (($i / 18) * 100)
$FindFreeSpaceQuery = "select SMS_G_System_LOGICAL_DISK.Size from SMS_R_System inner join SMS_G_System_LOGICAL_DISK on
SMS_G_System_LOGICAL_DISK.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_LOGICAL_DISK.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$CompFreeSpaceSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindFreeSpaceQuery |
Select-Object -ExpandProperty Size
$CompFreeSpaceSCCMGB = [math]::Round(($CompFreeSpaceSCCM / 1000), 2)
$i++ | ProgressBar $i $comp "SCCM RAM" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM RAM" -PercentComplete (($i / 18) * 100)
$FindMemoryQuery = "select SMS_G_System_X86_PC_MEMORY.TotalPhysicalMemory from SMS_R_System inner join SMS_G_System_X86_PC_MEMORY on
SMS_G_System_X86_PC_MEMORY.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_X86_PC_MEMORY.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindMemorySCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindMemoryQuery |
Select-Object -ExpandProperty TotalPhysicalMemory
$FindMemorySCCMGB = [math]::Round($FindMemorySCCM / 1MB)
$i++ | ProgressBar $i $comp "SCCM Asset Tag" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Asset Tag" -PercentComplete (($i / 18) * 100)
$FindAssetTagQuery = "select SMS_G_System_SYSTEM_ENCLOSURE.SMBIOSAssetTag from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on
SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'"
SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$comp'"
$FindAssetTagSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindAssetTagQuery |
Select-Object -ExpandProperty SMBIOSAssetTag
$i++ | ProgressBar $i $comp "SCCM Service Tag" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Service Tag" -PercentComplete (($i / 18) * 100)
$FindServiceTagQuery = "select SMS_G_System_SYSTEM_ENCLOSURE.SerialNumber from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on
SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindServiceTagSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindServiceTagQuery |
Select-Object -ExpandProperty SerialNumber
$i++ | ProgressBar $i $comp "SCCM OS Name" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM OS Name" -PercentComplete (($i / 18) * 100)
$FindOSNameQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber from
SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID =
SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindOSNameSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery |
Select-Object -ExpandProperty Caption
$i++ | ProgressBar $i $comp "SCCM OS Architecture" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM OS Architecture" -PercentComplete (($i / 18) * 100)
$FindOSNameQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber from
SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID =
SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindOSArchSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery |
Select-Object -ExpandProperty OSArchitecture
$i++ | ProgressBar $i $comp "SCCM OS Build" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM OS Build" -PercentComplete (($i / 18) * 100)
$FindOSNameQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber from
SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID =
SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindOSBuild = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery |
Select-Object -ExpandProperty BuildNumber
$i++ | ProgressBar $i $comp "SCCM Encryption" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Encryption" -PercentComplete (($i / 18) * 100)
$FindEncryptionQuery = "select SMS_G_System_ENCRYPTABLE_VOLUME.ProtectionStatus from SMS_R_System inner join SMS_G_System_ENCRYPTABLE_VOLUME on
SMS_G_System_ENCRYPTABLE_VOLUME.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_ENCRYPTABLE_VOLUME.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindEncryptionSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindEncryptionQuery |
Select-Object -ExpandProperty ProtectionStatus
Switch($FindEncryptionSCCM){
@ -157,10 +171,10 @@ Function Get-SCCMQueryBlock {
Default{$FindEncryptionSCCM = "Encryption Not Found"}
}
$i++ | ProgressBar $i $comp "SCCM Last Boot Up Time" $NumberofComputers $PCID
$FindLastBootUpTimeSCCM = Get-SCCMLastBootUpTime $comp
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Last Boot Up Time" -PercentComplete (($i / 18) * 100)
$FindLastBootUpTimeQuery = "select SMS_G_System_OPERATING_SYSTEM.LastBootUpTime from SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on
SMS_G_System_OPERATING_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
SMS_G_System_OPERATING_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindLastBootUpTimeSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastBootUpTimeQuery |
Select-Object -ExpandProperty LastBootUpTime
@ -174,23 +188,29 @@ Function Get-SCCMQueryBlock {
$FindLastBootUpTimeSCCM = "$Month/$Day/$Year $Hour" + ":" + "$Minute" + ":" + "$Second"
}
$i++ | ProgressBar $i $comp "SCCM Printers" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Printers" -PercentComplete (($i / 18) * 100)
$FindPCPrinterQuery = "select SMS_G_System_PRINTER_DEVICE.Name, SMS_G_System_PRINTER_DEVICE.PortName from SMS_R_System inner join SMS_G_System_PRINTER_DEVICE
on SMS_G_System_PRINTER_DEVICE.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
on SMS_G_System_PRINTER_DEVICE.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindPCPrinterSCCM = (Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindPCPrinterQuery |
Where-Object -Property PortName -ne nul: |
Where-Object -Property PortName -ne PORTPROMPT: |
Where-Object -Property PortName -ne SHRFAX: | Select-Object -ExpandProperty Name) -join ' || '
$i++ | ProgressBar $i $comp "SCCM MACAddress" $NumberofComputers $PCID
$FindMACAddressSCCM = Get-SCCMFindMACAddress $comp
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM MACAddress" -PercentComplete (($i / 18) * 100)
$FindMacAddressQuery = "select SMS_R_System.MACAddresses from SMS_R_System where SMS_R_System.Name = '$comp'"
$FindMacAddressSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindMacAddressQuery | Select-Object -ExpandProperty MACAddresses
$FindMacAddressSCCM = if($FindMacAddressSCCM.Count -gt 1){
$FindMacAddressSCCM[0]
}
$i++ | ProgressBar $i $comp "SCCM Chassis Type" $NumberofComputers $PCID
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Chassis Type" -PercentComplete (($i / 18) * 100)
$FindProcessorQuery = "select SMS_G_System_PROCESSOR.Name from SMS_R_System inner join SMS_G_System_PROCESSOR on SMS_G_System_PROCESSOR.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on
SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'"
SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$comp'"
$FindProcessorSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindProcessorQuery | Select-Object -ExpandProperty Name
$Model = Get-SCCMFindModelQuery $ComputerName
$FindChassisTypeSCCM = Switch -Wildcard($Model){
$FindChassisTypeSCCM = Switch -Wildcard($CompModelSCCM){
"Optiplex*" {
Switch -Wildcard($FindProcessorSCCM){
"Intel(R) Core(TM) i5-9500 CPU*" {"SFF"} #5070

View file

@ -4,7 +4,7 @@
#region Module Import Block
$ErrorActionPreference = 'SilentlyContinue'
#$ErrorActionPreference = 'SilentlyContinue'
#DevStage can take either Dev or Prod as values
$devStage = 'Dev'
#Locations for dev build and prod build

View file

@ -1,3 +1,7 @@
Patch: 2024-07-30
-Fix SCCM scans that broke in last update. Woopsie!
-Add stronger filters to AppDiff
Patch: 2024-07-26
-Refactors
-Update BatchInvokes to utilize jobs for up to 4x speedup