Get-PC/Private/SCCMQueryBlock.ps1

305 lines
16 KiB
PowerShell
Raw Normal View History

2024-07-31 00:26:27 +00:00

Function Get-SCCMQueryBlock {
param (
[string]$comp,
[int]$NumberofComputers,
[int]$PCID
)
2024-07-31 00:26:27 +00:00
#CONST
$SITENAME="100"
$SCCMSERVER="shscm01.int.samhealth.net"
$SCCMNAMESPACE="root\sms\site_100"
if(!(Test-Connection -ComputerName $comp -Count 1)){
$compStatus = "Offline"
}
else{
$compStatus = "Online"
}
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)"
}
}
$i = 0
2024-07-31 00:26:27 +00:00
$i++
Write-Progress -Activity "Scanning PC $comp" -Status "SCCM Last Hardware Scan" -PercentComplete (($i / 18) * 100)
#Last Hardware Scan
2024-07-31 00:26:27 +00:00
$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($null -eq $FindLastHardwareScanSCCM){
return $null
}
$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
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
}
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
$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]
}
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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)
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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)
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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
2024-07-31 00:26:27 +00:00
$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 =
2024-07-31 00:26:27 +00:00
SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindOSNameSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery |
Select-Object -ExpandProperty Caption
2024-07-31 00:26:27 +00:00
$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 =
2024-07-31 00:26:27 +00:00
SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindOSArchSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery |
Select-Object -ExpandProperty OSArchitecture
2024-07-31 00:26:27 +00:00
$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 =
2024-07-31 00:26:27 +00:00
SMS_R_System.ResourceId where SMS_R_System.Name = '$comp'"
$FindOSBuild = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery |
Select-Object -ExpandProperty BuildNumber
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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"}
}
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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
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"
}
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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 ' || '
2024-07-31 00:26:27 +00:00
$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]
}
2024-07-31 00:26:27 +00:00
$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
2024-07-31 00:26:27 +00:00
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
2024-07-31 00:26:27 +00:00
$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
#>
if($FindLastHardwareScanSCCM){
Write-Host "`n`nPulling cached SCCM data for $comp." -ForegroundColor Yellow
Write-Host "Last Hardware Scan Time: $FindLastHardwareScanSCCM" -ForegroundColor Yellow
}
$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"
}
$obj = New-Object -TypeName PSObject -Property $props
<#if($cmdbData.values.ConfigurationItem._SHSDELAsset -eq 'True'){
$delInfo = Get-CMDBDELInfo $cmdbData
$obj | Add-Member -MemberType NoteProperty -Name 'DEL Owner' -Value $delInfo.Owner
$obj | Add-Member -MemberType NoteProperty -Name 'DEL Vendor PC' -Value $delInfo.Vendor
$obj | Add-Member -MemberType NoteProperty -Name 'DEL Description' -Value $delInfo.Description
} #>
return $obj
}