diff --git a/Private/Get-Hostname.ps1 b/Private/Get-Hostname.ps1 index a0bc18c..b5e834a 100644 --- a/Private/Get-Hostname.ps1 +++ b/Private/Get-Hostname.ps1 @@ -7,7 +7,7 @@ function Get-Hostname ([string]$name) { $cmdbData = Search-ISMBO -bo cis -filter "AssetTag eq '$name'" -RawFilter } catch { $cmdbData = $null } if ( $cmdbData ) { return $cmdbData.Name, '' } - else { $errMsg += "$name Asset Tag not in SMBIOS or CMDB. " } + else { $errMsg += "$name Asset Tag not in SMBIOS nor CMDB`n" } } # Regex to match IP Address brought to you by https://stackoverflow.com/a/36760050 if ($name -match "^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$") { @@ -15,7 +15,7 @@ function Get-Hostname ([string]$name) { if ($res -and $res.NameHost -and ($res.NameHost -match "([^.]*)\.int\.samhealth\.net")) { return $Matches[0],'' } else { - $errMsg += "$name IP Address couldn't be resolved to hostname" + $errMsg += "$name IP Address couldn't be resolved to hostname`n" } } if ($name.Length -eq 7) { @@ -25,20 +25,27 @@ function Get-Hostname ([string]$name) { $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"} + else { $errMsg += "$name Service Tag not found in SCCM nor CMDB`n"} } # Regex to match MAC Address brought to you by https://stackoverflow.com/a/4260512 if ($name -match "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$") { $res = Get-SCCM_PCFromMAC $name if ($res) { return $res,'' } - else { $errMsg += "$name MAC Address not found in SCCM"} + else { $errMsg += "$name MAC Address not found in SCCM`n"} } + # Last resort checks + $sccmMatches = Get-SCCM_HostnameMatch $name + if ($SCCMMatches -and $SCCMMatches.Count -lt 2) { + return $SCCMMatches.Name,'' + } elseif (!$SCCMMatches) { $errMsg += "No SCCM match found`n" } + elseif ($SCCMMatches.Count -ge 2) { $errMsg += "Many SCCM matches found`n" } + $cmdbMatches = Find-ISMBO -bo cis -SearchQuery $name if ($cmdbMatches -and $cmdbMatches.Count -lt 2) { return $cmdbMatches.Name,'' - } elseif (!$cmdbMatches) { $errMsg += "No CMDB match found" } - elseif ($cmdbMatches.Count -ge 2) { $errMsg += "Many CMDB matches found" } + } elseif (!$cmdbMatches) { $errMsg += "No CMDB match found`n" } + elseif ($cmdbMatches.Count -ge 2) { $errMsg += "Many CMDB matches found`n" } return $name, $errMsg } \ No newline at end of file diff --git a/Private/SCCMQuery.ps1 b/Private/SCCMQuery.ps1 index 736e874..d9920b3 100644 --- a/Private/SCCMQuery.ps1 +++ b/Private/SCCMQuery.ps1 @@ -504,4 +504,10 @@ Function Get-SCCM_UserLastLoggedOn($user) { $computerSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMUserQuery | Select-Object -ExpandProperty Name return $computerSCCM +} + +Function Get-SCCM_HostnameMatch($name) { + $SCCMQuery = "select distinct SMS_R_System.Name from SMS_R_System where SMS_R_System.Name is like '%$name%'" + $res = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -ExpandProperty Name + return $res } \ No newline at end of file diff --git a/Public/Get-PC.ps1 b/Public/Get-PC.ps1 index 7d94842..b9426b0 100644 --- a/Public/Get-PC.ps1 +++ b/Public/Get-PC.ps1 @@ -193,7 +193,7 @@ Function Get-PC { $searchResults = Search-CMDB -Location $searchInput | Sort-Object -Property Hostname - if($searchResults -eq $null){ + if($null -eq $searchResults){ Write-Host 'No results for given search' -ForegroundColor Yellow return } diff --git a/patchnotes.txt b/patchnotes.txt index 91fd33e..515b109 100644 --- a/patchnotes.txt +++ b/patchnotes.txt @@ -1,3 +1,6 @@ +Patch: +-Fix Hostnames now check SCCM for a name match as a fallback + Patch: 2024-09-05 -Fix Spark! hangs sometimes, added timeout in BatchInvokes