Updated hostname resolution

This commit is contained in:
Zachary Gorman 2024-09-11 14:02:15 -07:00
parent f950cd11a7
commit ee682348fe
4 changed files with 23 additions and 7 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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