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)
This commit is contained in:
Zachary Gorman 2024-12-05 10:49:28 -08:00
parent c039beac33
commit 2991af9df2
8 changed files with 60 additions and 36 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.vscode .vscode
Get-PC VS Code.code-workspace Get-PC VS Code.code-workspace
ISMTools ISMTools
MusicPlayer

View file

@ -175,7 +175,7 @@ Function Find-ISMBusinessObject {
$Skip = $Results.Count $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 $SkipQuery = Invoke-RestMethod -Method GET -uri $URI -headers $Headers -TimeoutSec 15

View file

@ -62,6 +62,7 @@ function Get-Devices {
'DELL P2319H'= @('DP','HDMI','VGA'); 'DELL P2319H'= @('DP','HDMI','VGA');
'DELL P2422H'= @('DP','HDMI','VGA'); 'DELL P2422H'= @('DP','HDMI','VGA');
'DELL P2412H'= @('DP','HDMI','VGA'); 'DELL P2412H'= @('DP','HDMI','VGA');
'DELL P2417H'= @('DP','HDMI','VGA');
} }
foreach($item in $monitors){ foreach($item in $monitors){

View file

@ -73,8 +73,8 @@ function Get-Hostname ([string]$name) {
if (Resolve-DnsName $cmdbData.Name) { if (Resolve-DnsName $cmdbData.Name) {
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1 Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
return $cmdbData.Name,'' return $cmdbData.Name,''
} else { $errMsg += "CMDB name associated with Asset tag $name doesn't resolve to IP Address"} } else { $errMsg += "CMDB name associated with Asset tag $name doesn't resolve to IP Address`n"}
} else { $errMsg += "Asset tag not in CMDB" } } else { $errMsg += "Asset tag not in CMDB`n" }
} }
Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB MAC Address search' -PercentComplete 80 -ParentId 1 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})$") { 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) { if (Resolve-DnsName $cmdbData.Name) {
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1 Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
return $cmdbData.Name,'' return $cmdbData.Name,''
} else { $errMsg += "CMDB name associated with MAC Address $name doesn't resolve to IP Address"} } else { $errMsg += "CMDB name associated with MAC Address $name doesn't resolve to IP Address`n"}
} else { $errMsg += "MAC Address not in CMDB" } } else { $errMsg += "MAC Address not in CMDB`n" }
} }
# Serial number # Serial number
Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB Serial Number search' -PercentComplete 80 -ParentId 1 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) { if (Resolve-DnsName $cmdbData.Name) {
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1 Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
return $cmdbData.Name,'' return $cmdbData.Name,''
} else { $errMsg += "CMDB name associated with Serial Number $name doesn't resolve to IP Address"} } else { $errMsg += "CMDB name associated with Serial Number $name doesn't resolve to IP Address`n"}
} else { $errMsg += "Serial Number not in CMDB" } } else { $errMsg += "Serial Number not in CMDB`n" }
$errMsg += "Many CMDB matches found`n" $errMsg += "Many CMDB matches found`n"
} }

View file

@ -314,11 +314,22 @@
$uri = "$Tenant/api/odata/businessobject/cis`?`$filter=Name eq '$ENV:COMPUTERNAME'&`$top=1&`$skip=0" $uri = "$Tenant/api/odata/businessobject/cis`?`$filter=Name eq '$ENV:COMPUTERNAME'&`$top=1&`$skip=0"
try { try {
$Query = Invoke-RestMethod -Method GET -uri $uri -headers $Headers -TimeoutSec $timeout $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 { } catch {
#Write-Host $_.Exception.Message #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 = @( $LocationConstructors = @(
"SHS_AssetLocality", "SHS_AssetLocality",

View file

@ -5,7 +5,6 @@ Function Get-SCCMQueryBlock {
[int]$PCID = 1 [int]$PCID = 1
) )
#CONST #CONST
$SITENAME="100"
$SCCMSERVER="shscm01.int.samhealth.net" $SCCMSERVER="shscm01.int.samhealth.net"
$SCCMNAMESPACE="root\sms\site_100" $SCCMNAMESPACE="root\sms\site_100"
# Spark connection params # 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 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} $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 { 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 { } catch {
#Write-Host $_.Exception.Message #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 = @( $LocationConstructors = @(
"SHS_AssetLocality", "SHS_AssetLocality",

View file

@ -73,7 +73,6 @@ Function Get-PC {
-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
-NoResolve | skips hostname resolution, assumes provided name is a hostname
-Orion | opens orion to the mac address of the pc -Orion | opens orion to the mac address of the pc
-PatchNotes | read the patch notes -PatchNotes | read the patch notes
-PCCleanup | removes temp files and inactive user profiles -PCCleanup | removes temp files and inactive user profiles
@ -130,7 +129,6 @@ Function Get-PC {
[Switch]$LogOffUser, [Switch]$LogOffUser,
[Switch]$Monitor, [Switch]$Monitor,
[Switch]$NextPrinterName, [Switch]$NextPrinterName,
[Switch]$NoResolve,
[switch]$Orion, [switch]$Orion,
[Switch]$PatchNotes, [Switch]$PatchNotes,
[Switch]$PCCleanup, [Switch]$PCCleanup,
@ -188,7 +186,7 @@ begin {
if ($Update) { if ($Update) {
#updates get pc #updates get pc
Update-GetPC Update-GetPC
return break
} }
if ($PatchNotes) { if ($PatchNotes) {
@ -202,7 +200,7 @@ begin {
#runs the printer query as a set of jobs #runs the printer query as a set of jobs
$outPutArray = Get-PCBatchJob($ComputerName) -printers $outPutArray = Get-PCBatchJob($ComputerName) -printers
Write-Output $outPutArray Write-Output $outPutArray
return break
} }
if($locationSearch){ if($locationSearch){
@ -212,34 +210,27 @@ begin {
Write-Host "Searching CMDB for $searchInput ..." Write-Host "Searching CMDB for $searchInput ..."
if($searchInput.ToCharArray().Length -lt 3){ if($searchInput.ToCharArray().Length -lt 3){
Write-Host 'Location searches much contain more than 2 characters' -ForegroundColor Yellow Write-Host 'Location searches much contain more than 2 characters' -ForegroundColor Yellow
return break
} }
$searchResults = Search-CMDB -Location $searchInput | Sort-Object -Property Hostname $searchResults = Search-CMDB -Location $searchInput | Sort-Object -Property Hostname
if($null -eq $searchResults){ if($null -eq $searchResults){
Write-Host 'No results for given search' -ForegroundColor Yellow Write-Host 'No results for given search' -ForegroundColor Yellow
return break
} }
Write-Output $searchResults 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 break
}
}
$charA = $ComputerName.ToCharArray() $charA = $ComputerName.ToCharArray()
if($charA -contains '*'){ if($charA -contains '*'){
if($charA -lt 4){ if($charA -lt 4){
Write-Host "Wildcard searches need to be at least 4 characters long" -ForegroundColor Red Write-Host "Wildcard searches need to be at least 4 characters long" -ForegroundColor Red
return break
} }
Write-Host "Starting CMDB Wildcard Search..." Write-Host "Starting CMDB Wildcard Search..."
# $searchResults = Search-CMDB -hostname $ComputerName.Replace('*','') | Sort-Object -Property Hostname # $searchResults = Search-CMDB -hostname $ComputerName.Replace('*','') | Sort-Object -Property Hostname
@ -254,8 +245,15 @@ begin {
if($TableView){ if($TableView){
$output | Out-GridView -Title 'Get-PC Wildcard Search' $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 $getPCComputers = @() #List of computers that will get a batch query
@ -275,9 +273,7 @@ begin {
continue 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 Write-Progress -Activity "Get-PC flags processing" -Status "$comp ($PCID/$NumberofComputers)" -PercentComplete ($PCID*100/$NumberOfComputers) -Id 1
if ($msg -and -not $SHSPrinter) { if ($msg -and -not $SHSPrinter) {
if ($SoundEnabled) { if ($SoundEnabled) {

View file

@ -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 -Fix Enable flag also removes workstation from Disabled Computers group
-Update Orion url -Update Orion url
-Perf. Devices and Unplugged Devices are now 70% faster! -Perf. Devices and Unplugged Devices are now 70% faster!