From 2991af9df2d176cd7205b7eb55a11dbf4049df09 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Thu, 5 Dec 2024 10:49:28 -0800 Subject: [PATCH] Bad commit with many changes, see full message - Fixed wildcard search so Spark can return more than 25 results - Added ports to Dell P2417H monitor in Devices query - Better error messages from hostname resolution step - Better CMDB error messages - Removed NoResolve flag since hostname resolution on correct hostnames isn't very intensive. Considering skipping AD queries or restructuring get-pc so each pc is processed asynchrounously, and each query is processed async (like in BatchInvokes) --- .gitignore | 3 +- Get-PC/Private/Find-ISMBusinessObject.ps1 | 2 +- Get-PC/Private/Get-Devices.ps1 | 1 + Get-PC/Private/Get-Hostname.ps1 | 12 ++++---- Get-PC/Private/PCLocal.ps1 | 15 ++++++++-- Get-PC/Private/SCCMQueryBlock.ps1 | 20 +++++++++---- Get-PC/Public/Get-PC.ps1 | 36 ++++++++++------------- Get-PC/patchnotes.txt | 7 ++++- 8 files changed, 60 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 81e899b..d74b324 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode Get-PC VS Code.code-workspace -ISMTools \ No newline at end of file +ISMTools +MusicPlayer \ No newline at end of file diff --git a/Get-PC/Private/Find-ISMBusinessObject.ps1 b/Get-PC/Private/Find-ISMBusinessObject.ps1 index 00d06dc..8933feb 100644 --- a/Get-PC/Private/Find-ISMBusinessObject.ps1 +++ b/Get-PC/Private/Find-ISMBusinessObject.ps1 @@ -175,7 +175,7 @@ Function Find-ISMBusinessObject { $Skip = $Results.Count - $URI = "$Tenant/api/odata/businessobject/$BusinessObject`?`$top=100&`$skip=$Skip`$search=$SearchQuery" + $URI = "$Tenant/api/odata/businessobject/$BusinessObject`?`$top=100&`$skip=$Skip&`$search=$SearchQuery" $SkipQuery = Invoke-RestMethod -Method GET -uri $URI -headers $Headers -TimeoutSec 15 diff --git a/Get-PC/Private/Get-Devices.ps1 b/Get-PC/Private/Get-Devices.ps1 index 0165ac0..b8d96ae 100644 --- a/Get-PC/Private/Get-Devices.ps1 +++ b/Get-PC/Private/Get-Devices.ps1 @@ -62,6 +62,7 @@ function Get-Devices { 'DELL P2319H'= @('DP','HDMI','VGA'); 'DELL P2422H'= @('DP','HDMI','VGA'); 'DELL P2412H'= @('DP','HDMI','VGA'); + 'DELL P2417H'= @('DP','HDMI','VGA'); } foreach($item in $monitors){ diff --git a/Get-PC/Private/Get-Hostname.ps1 b/Get-PC/Private/Get-Hostname.ps1 index 0930d8a..20c316b 100644 --- a/Get-PC/Private/Get-Hostname.ps1 +++ b/Get-PC/Private/Get-Hostname.ps1 @@ -73,8 +73,8 @@ function Get-Hostname ([string]$name) { if (Resolve-DnsName $cmdbData.Name) { Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1 return $cmdbData.Name,'' - } else { $errMsg += "CMDB name associated with Asset tag $name doesn't resolve to IP Address"} - } else { $errMsg += "Asset tag not in CMDB" } + } else { $errMsg += "CMDB name associated with Asset tag $name doesn't resolve to IP Address`n"} + } else { $errMsg += "Asset tag not in CMDB`n" } } Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB MAC Address search' -PercentComplete 80 -ParentId 1 if ($name -match "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$") { @@ -85,8 +85,8 @@ function Get-Hostname ([string]$name) { if (Resolve-DnsName $cmdbData.Name) { Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1 return $cmdbData.Name,'' - } else { $errMsg += "CMDB name associated with MAC Address $name doesn't resolve to IP Address"} - } else { $errMsg += "MAC Address not in CMDB" } + } else { $errMsg += "CMDB name associated with MAC Address $name doesn't resolve to IP Address`n"} + } else { $errMsg += "MAC Address not in CMDB`n" } } # Serial number Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB Serial Number search' -PercentComplete 80 -ParentId 1 @@ -97,8 +97,8 @@ function Get-Hostname ([string]$name) { if (Resolve-DnsName $cmdbData.Name) { Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1 return $cmdbData.Name,'' - } else { $errMsg += "CMDB name associated with Serial Number $name doesn't resolve to IP Address"} - } else { $errMsg += "Serial Number not in CMDB" } + } else { $errMsg += "CMDB name associated with Serial Number $name doesn't resolve to IP Address`n"} + } else { $errMsg += "Serial Number not in CMDB`n" } $errMsg += "Many CMDB matches found`n" } diff --git a/Get-PC/Private/PCLocal.ps1 b/Get-PC/Private/PCLocal.ps1 index b8c36c6..d3adb16 100644 --- a/Get-PC/Private/PCLocal.ps1 +++ b/Get-PC/Private/PCLocal.ps1 @@ -314,11 +314,22 @@ $uri = "$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!" + #Write-Warning "Failed querying Spark!" + $cmdbData = @{ + SHS_LocationDetails = "Failed querying Spark!" + Status = "Failed querying Spark!" + } } - $cmdbData = $Query.Value $LocationConstructors = @( "SHS_AssetLocality", diff --git a/Get-PC/Private/SCCMQueryBlock.ps1 b/Get-PC/Private/SCCMQueryBlock.ps1 index 4b467a6..0f83d64 100644 --- a/Get-PC/Private/SCCMQueryBlock.ps1 +++ b/Get-PC/Private/SCCMQueryBlock.ps1 @@ -5,7 +5,6 @@ Function Get-SCCMQueryBlock { [int]$PCID = 1 ) #CONST -$SITENAME="100" $SCCMSERVER="shscm01.int.samhealth.net" $SCCMNAMESPACE="root\sms\site_100" # Spark connection params @@ -242,14 +241,25 @@ $Headers = @{ 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 = "${using:Tenant}/api/odata/businessobject/cis`?`$filter=Name eq '$comp'&`$top=1&`$skip=0" + $uri = "$Tenant/api/odata/businessobject/cis`?`$filter=Name eq '$comp'&`$top=1&`$skip=0" try { - $Query = Invoke-RestMethod -Method GET -uri $uri -headers ${using:Headers} -TimeoutSec $timeout + $Query = Invoke-RestMethod -Method GET -uri $uri -headers $Headers -TimeoutSec $timeout + if ($Query) { + $cmdbData = $Query.Value + } else { + $cmdbData = @{ + Loc = "CMDB Mismatch!" + Status = "CMDB Mismatch!" + } + } } catch { #Write-Host $_.Exception.Message - Write-Warning "Failed querying Spark!" + #Write-Warning "Failed querying Spark!" + $cmdbData = @{ + Loc = "Failed querying Spark!" + Status = "Failed querying Spark!" + } } - $cmdbData = $Query.Value $LocationConstructors = @( "SHS_AssetLocality", diff --git a/Get-PC/Public/Get-PC.ps1 b/Get-PC/Public/Get-PC.ps1 index ebe5d8e..acff26d 100644 --- a/Get-PC/Public/Get-PC.ps1 +++ b/Get-PC/Public/Get-PC.ps1 @@ -73,7 +73,6 @@ Function Get-PC { -LogOffUser | remotely logs off user -Monitor | monitors computers for changes in status -NextPrinterName | generates next open printer name - -NoResolve | skips hostname resolution, assumes provided name is a hostname -Orion | opens orion to the mac address of the pc -PatchNotes | read the patch notes -PCCleanup | removes temp files and inactive user profiles @@ -130,7 +129,6 @@ Function Get-PC { [Switch]$LogOffUser, [Switch]$Monitor, [Switch]$NextPrinterName, - [Switch]$NoResolve, [switch]$Orion, [Switch]$PatchNotes, [Switch]$PCCleanup, @@ -188,7 +186,7 @@ begin { if ($Update) { #updates get pc Update-GetPC - return + break } if ($PatchNotes) { @@ -202,7 +200,7 @@ begin { #runs the printer query as a set of jobs $outPutArray = Get-PCBatchJob($ComputerName) -printers Write-Output $outPutArray - return + break } if($locationSearch){ @@ -212,34 +210,27 @@ begin { Write-Host "Searching CMDB for $searchInput ..." if($searchInput.ToCharArray().Length -lt 3){ Write-Host 'Location searches much contain more than 2 characters' -ForegroundColor Yellow - return + break } $searchResults = Search-CMDB -Location $searchInput | Sort-Object -Property Hostname if($null -eq $searchResults){ Write-Host 'No results for given search' -ForegroundColor Yellow - return + break } Write-Output $searchResults - return - - } - - #Generates an available printer hostname. Useful for supplying a new hostname in new printer requests - if ($NextPrinterName) { - Find-NextPrinterName $ComputerName[0] break - } + } $charA = $ComputerName.ToCharArray() if($charA -contains '*'){ if($charA -lt 4){ Write-Host "Wildcard searches need to be at least 4 characters long" -ForegroundColor Red - return + break } Write-Host "Starting CMDB Wildcard Search..." # $searchResults = Search-CMDB -hostname $ComputerName.Replace('*','') | Sort-Object -Property Hostname @@ -254,8 +245,15 @@ begin { if($TableView){ $output | Out-GridView -Title 'Get-PC Wildcard Search' } - return $output - + #return $output + Write-Output $output + break + } + + #Generates an available printer hostname. Useful for supplying a new hostname in new printer requests + if ($NextPrinterName) { + Find-NextPrinterName $ComputerName[0] + break } $getPCComputers = @() #List of computers that will get a batch query @@ -275,9 +273,7 @@ begin { continue } - if (-not ($NoResolve)) { - $comp,$msg = Get-Hostname $comp - } + $comp,$msg = Get-Hostname $comp Write-Progress -Activity "Get-PC flags processing" -Status "$comp ($PCID/$NumberofComputers)" -PercentComplete ($PCID*100/$NumberOfComputers) -Id 1 if ($msg -and -not $SHSPrinter) { if ($SoundEnabled) { diff --git a/Get-PC/patchnotes.txt b/Get-PC/patchnotes.txt index c069389..c2e4607 100644 --- a/Get-PC/patchnotes.txt +++ b/Get-PC/patchnotes.txt @@ -1,4 +1,9 @@ -Path: 2024-10-24 +Patch: +-Add Powershell input pipeline support, unoptimized +-Add CMDB fields now hold Spark warnings rather than printing to console in SCCM and normal queries +-Add ports for DELL P2417H in Devices query + +Patch: 2024-10-24 -Fix Enable flag also removes workstation from Disabled Computers group -Update Orion url -Perf. Devices and Unplugged Devices are now 70% faster!