From e9435d15d344625ac73d80a702e9723e47821fcb Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Tue, 13 Aug 2024 14:19:17 -0700 Subject: [PATCH] Get-Hostname queries CMDB for asset tags correctly and SCCMQueryBlock now gets CMDB data --- Private/Get-Hostname.ps1 | 6 ++++- Private/SCCMQueryBlock.ps1 | 40 ++++++++++++++++++++++++++++----- Private/UserProfileBackup.ps1 | 4 ++-- Private/UserProfileTransfer.ps1 | 3 ++- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/Private/Get-Hostname.ps1 b/Private/Get-Hostname.ps1 index b21ccc1..6c88132 100644 --- a/Private/Get-Hostname.ps1 +++ b/Private/Get-Hostname.ps1 @@ -6,7 +6,7 @@ function Get-Hostname ([string]$name) { try { $cmdbData = Search-ISMBO -bo cis -filter "AssetTag eq '$name'" -RawFilter } catch { $cmdbData = $null } - if ( $cmdbData ) { return $cmdbData.values.title, '' } + if ( $cmdbData ) { return $cmdbData.Name, '' } else { $errMsg += "$name Asset Tag not in SMBIOS or CMDB. " } } # Regex to match IP Address brought to you by https://stackoverflow.com/a/36760050 @@ -21,6 +21,10 @@ function Get-Hostname ([string]$name) { if ($name.Length -eq 7) { $res = Get-ServiceTagConversion $name if ($res) { return $res,'' } + try { + $cmdbData = Search-ISMBO -bo cis -filter "SerialNumber eq '$name'" -RawFilter + } catch { $cmdbData = $null } + if ( $cmdbData ) { return $cmdbData.Name, '' } else { $errMsg += "$name Service Tag not found in SCCM"} } # Regex to match MAC Address brought to you by https://stackoverflow.com/a/4260512 diff --git a/Private/SCCMQueryBlock.ps1 b/Private/SCCMQueryBlock.ps1 index de4a873..8e2cee8 100644 --- a/Private/SCCMQueryBlock.ps1 +++ b/Private/SCCMQueryBlock.ps1 @@ -271,6 +271,33 @@ $SCCMNAMESPACE="root\sms\site_100" Write-Host "Last Hardware Scan Time: $FindLastHardwareScanSCCM" -ForegroundColor Yellow } + $uri = "${using:Tenant}/api/odata/businessobject/cis`?`$filter=Name eq '$comp'&`$top=1&`$skip=0" + Write-Host $uri + try { + $Query = Invoke-RestMethod -Method GET -uri $uri -headers ${using:Headers} + } catch { + Write-Host $_.Exception.Message + } + $cmdbData = $Query.Value + + $LocationConstructors = @( + "SHS_AssetLocality", + "ivnt_Location", + "SHS_Floor", + "SHS_Department", + "SHS_LocationDetails" + ) + + $LocationData = Foreach($Loc in $LocationConstructors){ + + if ($Loc -eq 'SHS_Floor'){ + $(if ($cmdbData.$Loc -match '-'){$cmdbData.$Loc.split('-')[-1] + " Floor"} else{$cmdbData.$Loc}) + } elseif (![string]::IsNullOrEmpty($cmdbData.$Loc)){ + $cmdbData.$Loc + } + } + + $LocationData = $LocationData -join ' | ' $props = [Ordered]@{ Hostname = "$comp" @@ -291,14 +318,15 @@ $SCCMNAMESPACE="root\sms\site_100" 'Service Tag' = "$FindServiceTagSCCM" 'Last Reboot' = "$FindLastBootUpTimeSCCM" Printers = "$FindPCPrinterSCCM" + 'CMDB Location' = "$LocationData" } $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 - } #> + 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 + } return $obj } diff --git a/Private/UserProfileBackup.ps1 b/Private/UserProfileBackup.ps1 index 4d0e693..a5cf97e 100644 --- a/Private/UserProfileBackup.ps1 +++ b/Private/UserProfileBackup.ps1 @@ -157,7 +157,7 @@ New-Item -Path $Destination\backuplog.txt -Force | Write-Verbose $log = Join-Path -Path $Destination -ChildPath backuplog.txt - Invoke-UserProfileTransfer $SourceRoot $Destination + Invoke-UserProfileTransfer $SourceRoot $Destination -ErrorAction 'Continue' if(($site -ge 0 ) -and ($site -lt $numSites)){ #Manually backup sticky notes since we don't always want them to transfer @@ -236,6 +236,6 @@ Write-Warning "The backup you have chosen does not exist" break } - Invoke-UserProfileTransfer $FullRestorationSource $SourceRoot + Invoke-UserProfileTransfer $FullRestorationSource $SourceRoot -ErrorAction 'Continue' } } diff --git a/Private/UserProfileTransfer.ps1 b/Private/UserProfileTransfer.ps1 index e60e412..59be4d6 100644 --- a/Private/UserProfileTransfer.ps1 +++ b/Private/UserProfileTransfer.ps1 @@ -1,4 +1,5 @@ function Invoke-UserProfileTransfer { + [CmdletBinding()] param($srcPath, $dstPath) <# .SYNOPSIS @@ -30,7 +31,7 @@ for ($i = 0; $i -lt $FoldersToCopy.Length; $i++) { $Folder = $FoldersToCopy[$i] - Write-Progress -Activity "User Profile Transfer from $srcPath to $dstPath" -Status $Folder -PercentComplete ($i / $FoldersToCopy.Length)*100 + Write-Progress -Activity "User Profile Transfer from $srcPath to $dstPath" -Status $Folder -PercentComplete (($i / $FoldersToCopy.Length)*100) $Source = Join-Path -Path $srcPath -ChildPath $Folder $Destination = $dstPath