2024-07-31 00:26:27 +00:00
2024-07-26 18:41:04 +00:00
Function Get-SCCMQueryBlock {
param (
[ string ] $comp ,
2024-09-23 21:39:12 +00:00
[ int ] $PCID = 1
2024-07-26 18:41:04 +00:00
)
2024-07-31 00:26:27 +00:00
#CONST
$SITENAME = " 100 "
$SCCMSERVER = " shscm01.int.samhealth.net "
$SCCMNAMESPACE = " root\sms\site_100 "
2024-07-26 18:41:04 +00:00
if ( ! ( Test-Connection -ComputerName $comp -Count 1 ) ) {
$compStatus = " Offline "
}
else {
$compStatus = " Online "
}
2024-09-23 21:39:12 +00:00
$progress = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Checking if disabled in AD' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
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) "
}
}
2024-09-23 21:39:12 +00:00
$progress + = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for Last Hardware Scan' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-31 00:26:27 +00:00
2024-07-26 18:41:04 +00:00
#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 ' "
2024-07-26 18:41:04 +00:00
$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
}
2024-09-23 21:39:12 +00:00
$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
2024-07-26 18:41:04 +00:00
if ( $FindIPSCCM . Count -gt 1 ) {
$FindIPSCCM = $FindIPSCCM [ 0 ]
}
2024-09-23 21:39:12 +00:00
$FindMacAddressSCCM = $SCCMResults | Select-Object -ExpandProperty MACAddresses
$FindMacAddressSCCM = if ( $FindMacAddressSCCM . Count -gt 1 ) {
$FindMacAddressSCCM [ 0 ]
}
$progress + = 10
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for Model' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$CompModelSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindModelQuery |
Select-Object -ExpandProperty Model
2024-09-23 21:39:12 +00:00
$progress + = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for Bios Version' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$FindBiosVerSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindBiosVerQuery |
Select-Object -ExpandProperty SMBIOSBIOSVersion
2024-09-23 21:39:12 +00:00
$progress + = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for HDD/SSD space' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$CompFreeSpaceSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindFreeSpaceQuery |
Select-Object -ExpandProperty Size
$CompFreeSpaceSCCMGB = [ math ] :: Round ( ( $CompFreeSpaceSCCM / 1000 ) , 2 )
2024-09-23 21:39:12 +00:00
$progress + = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for RAM' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$FindMemorySCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindMemoryQuery |
Select-Object -ExpandProperty TotalPhysicalMemory
$FindMemorySCCMGB = [ math ] :: Round ( $FindMemorySCCM / 1 MB )
2024-09-23 21:39:12 +00:00
$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
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' "
2024-09-23 21:39:12 +00:00
$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
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$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-09-23 21:39:12 +00:00
$progress + = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for Printers' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$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-09-23 21:39:12 +00:00
$progress + = 10
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for Chassis Type' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
$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' "
2024-07-26 18:41:04 +00:00
$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 ) {
2024-07-26 18:41:04 +00:00
" 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
#>
2024-09-23 21:39:12 +00:00
$progress + = 5
Write-Progress -Activity " Getting SCCM data for $comp " -Status 'Querying for CMDB data' -PercentComplete $progress -ParentID 0 -Id $PCID
2024-07-26 18:41:04 +00:00
2024-09-23 21:39:12 +00:00
$timeout = if ( $SparkQueryTimeoutSec ) { $SparkQueryTimeoutSec } else { 5 }
2024-08-13 21:19:17 +00:00
$uri = " $ {using:Tenant}/api/odata/businessobject/cis ` ? `$ filter=Name eq ' $comp '& `$ top=1& `$ skip=0 "
try {
2024-09-23 21:39:12 +00:00
$Query = Invoke-RestMethod -Method GET -uri $uri -headers $ { using : Headers } -TimeoutSec $timeout
2024-08-13 21:19:17 +00:00
} catch {
2024-09-23 21:39:12 +00:00
#Write-Host $_.Exception.Message
Write-Warning " Failed querying Spark! "
2024-08-13 21:19:17 +00:00
}
$cmdbData = $Query . Value
$LocationConstructors = @ (
" SHS_AssetLocality " ,
" ivnt_Location " ,
" SHS_Floor " ,
" SHS_Department " ,
" SHS_LocationDetails "
)
2024-09-23 21:39:12 +00:00
$LocationData = Foreach ( $Loc in $LocationConstructors ) {
2024-08-13 21:19:17 +00:00
2024-09-23 21:39:12 +00:00
if ( $Loc -eq 'SHS_Floor' ) {
$ ( if ( $cmdbData . $Loc -match '-' ) { $cmdbData . $Loc . split ( '-' ) [ -1 ] + " Floor " } else { $cmdbData . $Loc } )
} elseif ( ! [ string ] :: IsNullOrEmpty ( $cmdbData . $Loc ) ) {
$cmdbData . $Loc
2024-08-13 21:19:17 +00:00
}
2024-09-23 21:39:12 +00:00
}
$LocationData = $LocationData -join ' | '
if ( $FindLastHardwareScanSCCM ) {
Write-Host " `n `n Pulling cached SCCM data for $comp . " -ForegroundColor Yellow
Write-Host " Last Hardware Scan Time: $FindLastHardwareScanSCCM " -ForegroundColor Yellow
}
2024-08-13 21:19:17 +00:00
2024-09-23 21:39:12 +00:00
Write-Progress -Activity " Getting SCCM data for $comp " -Completed
2024-07-26 18:41:04 +00:00
$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 "
2024-08-13 21:19:17 +00:00
'CMDB Location' = " $LocationData "
2024-10-11 23:59:06 +00:00
'CMDB Status' = $cmdbData . Status
2024-07-26 18:41:04 +00:00
}
$obj = New-Object -TypeName PSObject -Property $props
2024-08-13 21:19:17 +00:00
if ( $cmdbData . SHS_IsException -eq 'True' ) {
Write-host " ***NOTICE: $ENV:COMPUTERNAME 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
}
2024-07-26 18:41:04 +00:00
return $obj
}