Get-hostname reorder

This commit is contained in:
Zachary Gorman 2024-09-13 14:57:17 -07:00
parent 2ec04bc03b
commit 649ed498b5

View file

@ -3,11 +3,9 @@ function Get-Hostname ([string]$name) {
if ($name.Length -eq 5) { if ($name.Length -eq 5) {
$res = Get-AssetConversion $name $res = Get-AssetConversion $name
if ($res) { return $res,'' } if ($res) { return $res,'' }
try { else { $errMsg += "$name Asset Tag not in SMBIOS`n" }
$cmdbData = Search-ISMBO -bo cis -filter "AssetTag eq '$name'" -RawFilter # We don't check CMDB asset tags here because they often resolve to
} catch { $cmdbData = $null } # something other than the hostname, like the asset tag itself
if ( $cmdbData ) { return $cmdbData.Name, '' }
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 # 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}$") { if ($name -match "^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$") {
@ -41,11 +39,33 @@ function Get-Hostname ([string]$name) {
} elseif (!$SCCMMatches) { $errMsg += "No SCCM match found`n" } } elseif (!$SCCMMatches) { $errMsg += "No SCCM match found`n" }
elseif ($SCCMMatches.Count -ge 2) { $errMsg += "Many SCCM matches found`n" } elseif ($SCCMMatches.Count -ge 2) { $errMsg += "Many SCCM matches found`n" }
# CMDB check should be absolute last resort
$cmdbMatches = Find-ISMBO -bo cis -SearchQuery $name $cmdbMatches = Find-ISMBO -bo cis -SearchQuery $name
if ($cmdbMatches -and $cmdbMatches.Count -lt 2) { if ($cmdbMatches -and $cmdbMatches.Count -lt 2) {
return $cmdbMatches.Name,'' return $cmdbMatches.Name,''
} elseif (!$cmdbMatches) { $errMsg += "No CMDB match found`n" } } elseif (!$cmdbMatches) { $errMsg += "No CMDB match found`n" }
elseif ($cmdbMatches.Count -ge 2) { $errMsg += "Many CMDB matches found`n" } elseif ($cmdbMatches.Count -ge 2) {
# Try more specific queries
# Asset tag
if ($name.Length -eq 5) {
try {
$cmdbData = Search-ISMBO -bo cis -Filter "AssetTag eq '$name'" -RawFilter
} catch { $cmdbData = $null }
if ( $cmdbData ) {
if (Resolve-DnsName $cmdbData.Name) { 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" }
}
# Serial number
try {
$cmdbData = Search-ISMBO -bo cis -Filter "SerialNumber eq '$name'" -RawFilter
} catch { $cmdbData = $null }
if ( $cmdbData ) {
if (Resolve-DnsName $cmdbData.Name) { 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" }
$errMsg += "Many CMDB matches found`n"
}
return $name, $errMsg return $name, $errMsg
} }