Merge branch 'master' of S:/Powershell/Get-PC_Update_2023/Get-PC

This commit is contained in:
Zachary Gorman 2025-02-13 16:03:30 -08:00
commit 7a01bf51b0
6 changed files with 374 additions and 455 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) {
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 Write-Progress -Activity "Getting apps for $ComputerName" -Status 'Querying local apps' -PercentComplete 30 -ParentId 1
$sblock = {
$32RegPath = 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
$64RegPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
$apps = Get-ItemProperty $32RegPath |
Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru |
Add-Member -NotePropertyName x86/x64 -NotePropertyValue 32 -PassThru
$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
}
#Checks if local computer #Checks if local computer
if ($ComputerName -eq $env:COMPUTERNAME) { if ($ComputerName -eq $env:COMPUTERNAME) {
$localapps = Get-Package -ProviderName Programs -IncludeWindowsInstaller | $apps = Invoke-Command -ScriptBlock $sblock | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ }
Add-Member -NotePropertyName Computer -NotePropertyValue $env:ComputerName -PassThru |
Select-Object @{N='DisplayName';E={$_.Name}},* |
Sort-Object -Property DisplayName
} else { } else {
#Checks if pc is online so Invoke doesn't hang on offline pc $apps = Invoke-Command -ComputerName $ComputerName -SessionOption $(New-PSSessionOption -MaxConnectionRetryCount 1 -NoMachineProfile) -ScriptBlock $sblock |
if (Test-Connection -ComputerName $ComputerName -Count 1) { Select-Object -Property * -ExcludeProperty PSComputerName | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'GetPC.App'); $_ }
$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 |
Select-Object @{N='DisplayName';E={$_.Name}},* |
Sort-Object -Property DisplayName
} }
}
}
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"
} }
@ -61,10 +37,3 @@
} }
Write-Progress -Activity "Getting apps for $ComputerName" -Completed Write-Progress -Activity "Getting apps for $ComputerName" -Completed
} }
function Copy-Property ($From, $To) {
$properties = Get-Member -InputObject $From -MemberType NoteProperty
foreach ($p in $properties) {
$To | Add-Member -MemberType NoteProperty -Name $p.Name -Value $From.$($p.Name) -Force
}
}

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)
@ -248,7 +226,7 @@ $Headers = @{
$cmdbData = $Query.Value $cmdbData = $Query.Value
} else { } else {
$cmdbData = @{ $cmdbData = @{
Loc = "CMDB Mismatch!" SHS_LocationDetails = "CMDB Mismatch!"
Status = "CMDB Mismatch!" Status = "CMDB Mismatch!"
} }
} }
@ -256,7 +234,7 @@ $Headers = @{
#Write-Host $_.Exception.Message #Write-Host $_.Exception.Message
#Write-Warning "Failed querying Spark!" #Write-Warning "Failed querying Spark!"
$cmdbData = @{ $cmdbData = @{
Loc = "Failed querying Spark!" SHS_LocationDetails = "Failed querying Spark!"
Status = "Failed querying Spark!" Status = "Failed querying Spark!"
} }
} }

View file

@ -13,17 +13,54 @@ function Get-SHSPrinter {
[string]$printer [string]$printer
) )
Write-Progress -Activity "Getting printer details for $name" -Status 'Getting IP Address' -PercentComplete 10 -ParentId 1 # 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"
}
Write-Progress -Activity "Getting printer details for $printer" -Status 'Getting IP Address' -PercentComplete 10 -ParentId 1
$snmp = New-Object -ComObject olePrn.OleSNMP $snmp = New-Object -ComObject olePrn.OleSNMP
#Gets the printer ip and full domain name #Gets the printer ip and full domain name
$result = Get-PrinterIP $printer $domains = @('.gsrmc.int.samhealth.net','.avery.int.samhealth.net','.sagh.int.samhealth.net','.snlh.int.samhealth.net','.slch.int.samhealth.net','.spch.int.samhealth.net')
$result = Resolve-DnsName $printer -ErrorAction SilentlyContinue
if($null -eq $result){
foreach ($domain in $domains) {
$search = "$printer$domain"
$result = Resolve-DnsName $search -ErrorAction SilentlyContinue
if($null -ne $result){ break }
}
}
#CMDB Data #CMDB Data
Write-Progress -Activity "Getting printer details for $name" -Status 'Getting CMDB data' -PercentComplete 20 -ParentId 1 Write-Progress -Activity "Getting printer details for $printer" -Status 'Getting CMDB data' -PercentComplete 20 -ParentId 1
# $cmdbRecord = Get-LANDeskCMDBItem -Name $printer # $cmdbRecord = Get-LANDeskCMDBItem -Name $printer
$cmdbRecord = Search-ISMBO -BO cis -filter "Name eq '$Printer'" -RawFilter $timeout = if ($SparkQueryTimeoutSec) {$SparkQueryTimeoutSec} else {5}
$uri = "$Tenant/api/odata/businessobject/cis`?`$filter=Name eq '$printer'&`$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 = @( $LocationConstructors = @(
"SHS_AssetLocality", "SHS_AssetLocality",
@ -36,63 +73,73 @@ function Get-SHSPrinter {
$LocationData = Foreach($Loc in $LocationConstructors){ $LocationData = Foreach($Loc in $LocationConstructors){
if ($Loc -eq 'SHS_Floor'){ if ($Loc -eq 'SHS_Floor'){
$(if ($cmdbRecord.$Loc -match '-') { $cmdbRecord.$Loc.split('-')[-1] + " Floor" } else { $cmdbRecord.$Loc }) $(if ($cmdbData.$Loc -match '-'){$cmdbData.$Loc.split('-')[-1] + " Floor"} else{$cmdbData.$Loc})
} } elseif (![string]::IsNullOrEmpty($cmdbData.$Loc)){
elseif (![string]::IsNullOrEmpty($cmdbRecord.$Loc)) { $cmdbData.$Loc
$cmdbRecord.$Loc
} }
} }
$LocationData = $LocationData -join ' | ' $LocationData = $LocationData -join ' | '
# $locationData = $cmdbRecord.SHS_AssetLocality + " | " + $cmdbRecord.ivnt_Location + " | " + + " | " + $cmdbRecord.SHS_Department + " | " + $cmdbRecord.SHS_LocationDetails if ($cmdbData) {
# if($cmdbRecord.values._SHSLocation3.Length -gt $cmdbRecord.values._SHSCalcLocationString.Length){
# $locationData = $cmdbRecord.values._SHSLocation3
# }
# else{
# $locationData = $cmdbRecord.values._SHSCalcLocationString
# }
if ($cmdbRecord) {
# $CMDB_POA = $cmdbRecord.values._SHSPOANumber
# $CMDB_AssetTag = $cmdbRecord.values._SHSAssetTag
# $CMDB_Location = $locationData
# $CMDB_MAC = $cmdbRecord.values._SHSMACAddress
# $CMDB_model = $cmdbRecord.values._Model
# $CMDB_serial = $cmdbRecord.values._SerialNumber
# $CMDB_IP = $cmdbRecord.values._IPAddress
### Spark Properties ### Spark Properties
$CMDB_POA = $cmdbRecord.SHS_POANumber $CMDB_POA = $cmdbData.SHS_POANumber
$CMDB_AssetTag = $cmdbRecord.AssetTag $CMDB_AssetTag = $cmdbData.AssetTag
$CMDB_Location = $locationData $CMDB_Location = $locationData
$CMDB_MAC = $cmdbRecord.MACAddress $CMDB_MAC = $cmdbData.MACAddress
$CMDB_model = $cmdbRecord.Model $CMDB_model = $cmdbData.Model
$CMDB_serial = $cmdbRecord.SerialNumber $CMDB_serial = $cmdbData.SerialNumber
$CMDB_IP = $cmdbRecord.IPAddress $CMDB_IP = $cmdbData.IPAddress
$CMDB_Status = $cmdbData.Status
}
if ($null -eq $result.IPAddress) {
if ($CMDB_IP) {
$offlineIP = 'CMDB IP - ' + $CMDB_IP
} }
else { else {
$CMDB_POA = "*CMDB Mismatch - check CMDB*" $offlineIP = 'No DNS Entry'
$CMDB_AssetTag = "*CMDB Mismatch - check CMDB*" }
$CMDB_Location = "*CMDB Mismatch - check CMDB*" $props = [Ordered]@{
$CMDB_MAC = "*CMDB Mismatch - check CMDB*" Machine = $printer
$CMDB_model = "*CMDB Mismatch - check CMDB*" Status = 'No DNS Entry'
$CMDB_serial = "*CMDB Mismatch - check CMDB*" IP = $offlineIP
$CMDB_IP = "*CMDB Mismatch - check CMDB*" DomainName = 'No DNS Entry'
MAC = $CMDB_MAC
Model = $CMDB_model
Serial = $CMDB_serial
#Comment = $comment
Color = 'No DNS Entry'
Trays = 'No DNS Entry'
PageCount = 'No DNS Entry'
'CMDB POA' = $CMDB_POA
'CMDB AssetTag' = $CMDB_AssetTag
'CMDB Location' = $CMDB_Location
'CMDB Status' = $CMDB_Status
}
$obj = New-Object -TypeName PSObject -Property $props
return $obj
} }
if ($null -ne $result.IP) {
Write-Progress -Activity "Getting printer details for $name" -Status 'Connecting to printer' -PercentComplete 30 -ParentId 1 Write-Progress -Activity "Getting printer details for $name" -Status 'Connecting to printer' -PercentComplete 30 -ParentId 1
$printerip = $result.IP $printerip = $result.IPAddress
$domainName = $result.Path $null = $result.Name -match '[^.]*\.(.*)'
$domainName = $Matches[1]
#checks to see if the printer is online #checks to see if the printer is online
$online = Test-Connection $printerip -ErrorAction SilentlyContinue $online = Test-Connection $printerip -ErrorAction SilentlyContinue
if (-not $online) {
$status = "Offline"
if ($online) { $MAC = $CMDB_MAC
$model = $CMDB_model
$serial = $CMDB_serial
$color = "Offline"
$trays = "Offline"
$pagecount = "Offline"
}
else {
#opens snmp connection to the printer #opens snmp connection to the printer
$snmp.open($printerip, 'public', 2, 3000) $snmp.open($printerip, 'public', 2, 3000)
@ -339,15 +386,6 @@ function Get-SHSPrinter {
}#end if ($model) }#end if ($model)
} }
else {
$status = "Offline"
$MAC = $CMDB_MAC
$model = $CMDB_model
$serial = $CMDB_serial
$color = "Offline"
$trays = "Offline"
$pagecount = "Offline"
}
$props = [Ordered]@{ $props = [Ordered]@{
Machine = $printer Machine = $printer
@ -368,90 +406,16 @@ function Get-SHSPrinter {
'CMDB POA' = $CMDB_POA 'CMDB POA' = $CMDB_POA
'CMDB AssetTag' = $CMDB_AssetTag 'CMDB AssetTag' = $CMDB_AssetTag
'CMDB Location' = $CMDB_Location 'CMDB Location' = $CMDB_Location
'CMDB Status' = $CMDB_Status
} }
$obj = New-Object -TypeName PSObject -Property $props $obj = New-Object -TypeName PSObject -Property $props
$snmp.close() $snmp.close()
Write-Progress -Activity "Getting printer details for $name" -Completed Write-Progress -Activity "Getting printer details for $name" -Completed
return $obj return $obj
}#end if ($printerIp)
if ($CMDB_IP) {
$offlineIP = 'CMDB IP - ' + $CMDB_IP
}
else {
$offlineIP = 'No DNS Entry'
}
$props = [Ordered]@{
Machine = $printer
Status = 'No DNS Entry'
IP = $offlineIP
DomainName = 'No DNS Entry'
MAC = $CMDB_MAC
Model = $CMDB_model
Serial = $CMDB_serial
#Comment = $comment
Color = 'No DNS Entry'
Trays = 'No DNS Entry'
PageCount = 'No DNS Entry'
'CMDB POA' = $CMDB_POA
'CMDB AssetTag' = $CMDB_AssetTag
'CMDB Location' = $CMDB_Location
}
$obj = New-Object -TypeName PSObject -Property $props
$snmp.close()
return $obj
} }
function Get-PrinterIP {
param (
[parameter(ValueFromPipeline)]
[string]$printer
)
$hit = $false
$domains = @('.gsrmc.int.samhealth.net','.avery.int.samhealth.net','.sagh.int.samhealth.net','.snlh.int.samhealth.net','.slch.int.samhealth.net','.spch.int.samhealth.net')
$result = Resolve-DnsName $printer -ErrorAction SilentlyContinue
if($result -eq $null){
$hit = $false
foreach ($domain in $domains) {
$search = "$printer$domain"
$result = Resolve-DnsName $search -ErrorAction SilentlyContinue
if($result -ne $null){
$hit = $true
$name = $result.Name
$ip = $result.IPAddress
}
}
if($result -eq $null){
if($hit -eq $false){
$ip = $null
$name = $null
}
}
}
else{
$name = $result.Name
$ip = $result.IPAddress
}
$props = [ordered]@{Hostname=$printer
IP=$ip
Path=$name
}
$obj = New-Object -TypeName PSObject -Property $props
return $obj
}
function Start-SHSPrinterWeb { function Start-SHSPrinterWeb {
param( param(
$printer $printer

View file

@ -70,7 +70,9 @@ Function Get-PC {
-Excel | exports collected data to csv and opens it with excel -Excel | exports collected data to csv and opens it with excel
-Filesystem | access the file system -Filesystem | access the file system
-GPUpdate | runs a gpupdate -GPUpdate | runs a gpupdate
-HostnamesByPrinter | grabs all hostnames that have the requested printer installed on it
-InstallNet35 | installs .Net 3.5 -InstallNet35 | installs .Net 3.5
-JobsPrinters | optimized version of shsprinter for querying many printers
-LogOffUser | remotely logs off user -LogOffUser | remotely logs off user
-Monitor | monitors computers for changes in status -Monitor | monitors computers for changes in status
-NextPrinterName | generates next open printer name -NextPrinterName | generates next open printer name
@ -531,12 +533,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
} }

View file

@ -1,3 +1,8 @@
Patch 2025-2-13
-Add HostnamesByPrinter to list hostnames with printer installed
-Add JobsPrinters to query many printers faster
-Update Apps no longer queries sccm when workstation is online
Patch: 2025-2-4 Patch: 2025-2-4
-Add ADGroups flag to list out a workstation or user's AD groups -Add ADGroups flag to list out a workstation or user's AD groups
-Fix Devices reports correct ports for Dell P2425H -Fix Devices reports correct ports for Dell P2425H