Get-PC/Get-PC/Private/SCCMQueryBlock.ps1
2025-06-26 10:43:04 -07:00

306 lines
15 KiB
PowerShell

Function Get-SCCMQueryBlock {
param (
[string]$comp,
[int]$PCID = 1
)
#CONST
$SCCMSERVER="shscm01.int.samhealth.net"
$SCCMNAMESPACE="root\sms\site_100"
# Spark connection params
$Tenant = 'https://samaritanhealth-amc.ivanticloud.com'
$Headers = @{
"Content-Type" = "application/json"
"Authorization" = 'rest_api_key=EB68123D62F8489295C807353C92D75B'
"Accept" = "*/*"
"Accept-Encoding" = "gzip, deflate, br"
}
if(!(Test-Connection -ComputerName $comp -Count 1)){
$compStatus = "Offline"
}
else{
$compStatus = "Online"
}
$progress = 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Checking if disabled in AD' -PercentComplete $progress -ParentID 0 -Id $PCID
if(get-module -ListAvailable -Name 'ActiveDirectory'){
try { $adTest = ((Get-ADComputer $comp).DistinguishedName -match "Disabled Computers") } catch { $adTest = $true }
if($adTest){
$compStatus += " (disabled or off the domain)"
}
}
$progress += 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Last Hardware Scan' -PercentComplete $progress -ParentID 0 -Id $PCID
#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 = '$comp'"
$FindLastHardwareScanSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastHardwareScanQuery | Select-Object -ExpandProperty LastHardwareScan
if(!$FindLastHardwareScanSCCM){
Write-Progress -Activity "Getting SCCM data for $comp" -Completed
return
}
$Year = $FindLastHardwareScanSCCM.substring(0,4)
$Month = $FindLastHardwareScanSCCM.substring(4,2)
$Day = $FindLastHardwareScanSCCM.substring(6,2)
$Hour = $FindLastHardwareScanSCCM.substring(8,2)
$Minute = $FindLastHardwareScanSCCM.substring(10,2)
$Second = $FindLastHardwareScanSCCM.substring(12,2)
$FindLastHardwareScanSCCM = "$Month/$Day/$Year $Hour" + ":" + "$Minute" + ":" + "$Second"
#End Last Hardware Scan
$progress += 10
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Last User Logon' -PercentComplete $progress -ParentID 0 -Id $PCID
$SCCMQuery = "select SMS_R_System.LastLogonUserName,SMS_R_System.IPAddresses,SMS_R_System.MACAddresses from SMS_R_System where SMS_R_System.Name = '$comp'"
$SCCMResults = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $SCCMQuery
$LastUserSCCM = $SCCMResults | Select-Object -ExpandProperty LastLogonUserName
$FindIPSCCM = $SCCMResults | Select-Object -ExpandProperty IPAddresses
if($FindIPSCCM.Count -gt 1){
$FindIPSCCM = $FindIPSCCM[0]
}
$FindMacAddressSCCM = $SCCMResults | Select-Object -ExpandProperty MACAddresses
$FindMacAddressSCCM = if($FindMacAddressSCCM.Count -gt 1){
$FindMacAddressSCCM[0]
} else { $FindMacAddressSCCM }
$progress += 10
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Model' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$comp'"
$CompModelSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindModelQuery |
Select-Object -ExpandProperty Model
$progress += 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Bios Version' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$comp'"
$FindBiosVerSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindBiosVerQuery |
Select-Object -ExpandProperty SMBIOSBIOSVersion
$progress += 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for HDD/SSD space' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$comp'"
$CompFreeSpaceSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindFreeSpaceQuery |
Select-Object -ExpandProperty Size
$CompFreeSpaceSCCMGB = [math]::Round(($CompFreeSpaceSCCM / 1000), 2)
$progress += 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for RAM' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$comp'"
$FindMemorySCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindMemoryQuery |
Select-Object -ExpandProperty TotalPhysicalMemory
$FindMemorySCCMGB = [math]::Round($FindMemorySCCM / 1MB)
$progress += 10
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Asset Tag' -PercentComplete $progress -ParentID 0 -Id $PCID
$SCCMQuery = "select SMS_G_System_SYSTEM_ENCLOSURE.SMBIOSAssetTag, 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 inner join SMS_G_System_SYSTEM on SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$comp'"
$SCCMResults = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $SCCMQuery
$FindAssetTagSCCM = $SCCMResults | Select-Object -ExpandProperty SMBIOSAssetTag
$FindServiceTagSCCM = $SCCMResults | Select-Object -ExpandProperty SerialNumber
$progress += 10
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for OS Name' -PercentComplete $progress -ParentID 0 -Id $PCID
$SCCMQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber,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 = '$comp'"
$SCCMResults = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $SCCMQuery
$FindOSNameSCCM = $SCCMResults | Select-Object -ExpandProperty Caption
$FindOSArchSCCM = $SCCMResults | Select-Object -ExpandProperty OSArchitecture
$FindOSBuild = $SCCMResults | Select-Object -ExpandProperty BuildNumber
$FindLastBootUpTimeSCCM = $SCCMResults | Select-Object -ExpandProperty LastBootUpTime
if($null -ne $FindLastBootUpTimeSCCM){
$Year = $FindLastBootUpTimeSCCM.substring(0,4)
$Month = $FindLastBootUpTimeSCCM.substring(4,2)
$Day = $FindLastBootUpTimeSCCM.substring(6,2)
$Hour = $FindLastBootUpTimeSCCM.substring(8,2)
$Minute = $FindLastBootUpTimeSCCM.substring(10,2)
$Second = $FindLastBootUpTimeSCCM.substring(12,2)
$FindLastBootUpTimeSCCM = "$Month/$Day/$Year $Hour" + ":" + "$Minute" + ":" + "$Second"
}
$progress += 10
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Encryption' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$comp'"
$FindEncryptionSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindEncryptionQuery |
Select-Object -ExpandProperty ProtectionStatus
Switch($FindEncryptionSCCM){
0{$FindEncryptionSCCM = "BitLocker (Disabled)"}
1{$FindEncryptionSCCM = "BitLocker (Enabled [Unlocked])"}
2{$FindEncryptionSCCM = "BitLocker (Enabled [Locked])"}
Default{$FindEncryptionSCCM = "Encryption Not Found"}
}
$progress += 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Printers' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$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 ' || '
$progress += 10
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Chassis Type' -PercentComplete $progress -ParentID 0 -Id $PCID
$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 = '$comp'"
$FindProcessorSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindProcessorQuery | Select-Object -ExpandProperty Name
$FindChassisTypeSCCM = Switch -Wildcard($CompModelSCCM){
"Optiplex*" {
Switch -Wildcard($FindProcessorSCCM){
"Intel(R) Core(TM) i5-9500 CPU*" {"SFF"} #5070
"Intel(R) Core(TM) i5-6500 CPU*" {"SFF"} #7040
"Intel(R) Core(TM) i5-6500T*" {"Micro"} #7040
"Intel(R) Core(TM) i7-6700 CPU*" {"SFF"} #7040
"Intel(R) Core(TM) i7-6700T*" {"Micro"} #7040
"Intel(R) Core(TM) i5-9500T*" {"Micro"} #5070
"Intel(R) Core(TM) i5-8500 CPU*" {"SFF"} #5060
"Intel(R) Core(TM) i5-8500T*" {"Micro"} #5060
"Intel(R) Core(TM) i5-7500*" {"SFF"} #5050
"Intel(R) Core(TM) i5-4670 CPU*" {"SFF"} #9020
"Intel(R) Core(TM) i5-4590 CPU*" {"SFF"} #9020
"Intel(R) Core(TM) i5-4590T CPU*" {"Micro"} #9020M
"Intel(R) Core(TM) i5-4690 CPU*" {"SFF"} #9020
"Intel(R) Core(TM) i5-3550 CPU*" {"SFF"} #9010
"Intel(R) Core(TM) i5-2400 CPU*" {"SFF"} #990
Default {"Optiplex - Chassis Type - Unknown"}
}
}
"Latitude*" {"Laptop"}
"Precision*"{"Laptop"}
"M24*" {"Anesthesia Cart"}
"Medix*"{"Anesthesia Cart"}
Default {"Unknown Model/Chassis"}
}
if($FindAssetTagSCCM -is [array]){
Write-Warning "Dupe record in SCCM - $comp"
$FindAssetTagSCCM = $FindAssetTagSCCM[0]
<#Get-CMDBFallback $comp#>
}
<#$cmdbData = Get-CMDBData $comp $FindAssetTagSCCM
$MDBLcmdblocation = Get-Cocation $cmdbData
#>
$progress += 5
Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for CMDB data' -PercentComplete $progress -ParentID 0 -Id $PCID
$timeout = if ($SparkQueryTimeoutSec) {$SparkQueryTimeoutSec} else {5}
$uri = if ($assetTag -ne '') {
"$Tenant/api/odata/businessobject/cis`?`$filter=AssetTag eq '$assetTag'"
} elseif ($serviceTag -ne ''){
"$Tenant/api/odata/businessobject/cis`?`$filter=SerialNumber eq '$serviceTag'"
} else {
"$Tenant/api/odata/businessobject/cis`?`$filter=Name eq '$env:ComputerName'&`$top=1&`$skip=0"
}
try {
$Query = Invoke-RestMethod -Method GET -uri $uri -headers $Headers -TimeoutSec $timeout
if ($Query) {
$cmdbData = $Query.Value
} else {
$cmdbData = @{
SHS_LocationDetails = "CMDB Mismatch!"
Status = "CMDB Mismatch!"
}
}
} catch {
#Write-Host $_.Exception.Message
#Write-Warning "Failed querying Spark!"
$cmdbData = @{
SHS_LocationDetails = "Failed querying Spark!"
Status = "Failed querying Spark!"
}
}
$LocationConstructors = @(
"SHS_AssetLocality",
"ivnt_Location",
"SHS_Floor",
"SHS_Department",
"SHS_LocationDetails"
)
$LocationData = Foreach($Loc in $LocationConstructors){
if ($Loc -eq 'SHS_Floor'){
$(if ($cmdbData.$Loc -match '-'){$cmdbData.$Loc.split('-')[-1] + " Floor"} else{$cmdbData.$Loc})
} elseif (![string]::IsNullOrEmpty($cmdbData.$Loc)){
$cmdbData.$Loc
}
}
$LocationData = $LocationData -join ' | '
if($FindLastHardwareScanSCCM){
Write-Host "`n`nPulling cached SCCM data for $comp." -ForegroundColor Yellow
Write-Host "Last Hardware Scan Time: $FindLastHardwareScanSCCM" -ForegroundColor Yellow
}
Write-Progress -Activity "Getting SCCM data for $comp" -Completed
$props = [Ordered]@{
Hostname = "$comp"
Status = "$compStatus"
'Current User' = "Not Available"
'Last User(s)' = "$LastUserSCCM"
'IP | MAC' = "$FindIPSCCM | $FindMACAddressSCCM"
Model = "$CompModelSCCM ($FindChassisTypeSCCM)"
'OS' = "$FindOSNameSCCM" + " ($FindOSArchSCCM)"
'OS Build' = "Build #$FindOSBuild"
'BIOS Ver' = "$FindBiosVerSCCM"
Encryption = "$FindEncryptionSCCM"
'Free Space' = "$CompFreeSpaceSCCMGB" + " GB"
RAM = "$FindMemorySCCMGB" + " GB "
'SSO Client' = "Not Available"
'Kiosk Role' = "Not Available"
'Asset Tag' = "$FindAssetTagSCCM"
'Service Tag' = "$FindServiceTagSCCM"
'Last Reboot' = "$FindLastBootUpTimeSCCM"
Printers = "$FindPCPrinterSCCM"
'CMDB Location' = "$LocationData"
'CMDB Status' = $cmdbData.Status
}
$obj = New-Object -TypeName PSObject -Property $props
if($cmdbData.SHS_IsException -eq 'True'){
Write-host "***NOTICE: $comp is a Device Exception computer. Please check CMDB/Asset Mgmt prior to supporting this workstation. ***" -BackgroundColor Black -ForegroundColor Yellow
$obj | Add-Member -MemberType NoteProperty -Name 'DEL Owner' -Value $cmdbData.SHS_ExceptionContact
$obj | Add-Member -MemberType NoteProperty -Name 'DEL Vendor PC' -Value $cmdbData.SHS_IsVendorPC
$obj | Add-Member -MemberType NoteProperty -Name 'DEL Description' -Value $cmdbData.SHS_ExceptionNotes
}
return $obj
}