80 lines
3.6 KiB
PowerShell
80 lines
3.6 KiB
PowerShell
function Get-Hostname ([string]$name) {
|
|
$errMsg = ''
|
|
if ($name.Length -eq 5) {
|
|
$res = Get-AssetConversion $name
|
|
if ($res) { return $res,'' }
|
|
else { $errMsg += "$name Asset Tag not in SMBIOS`n" }
|
|
# We don't check CMDB asset tags here because they often resolve to
|
|
# something other than the hostname, like the asset tag itself
|
|
}
|
|
# 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}$") {
|
|
$res = Resolve-DnsName $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`n"
|
|
}
|
|
}
|
|
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 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`n"}
|
|
}
|
|
|
|
# Last resort checks
|
|
$sccmMatches = Get-SCCM_HostnameMatch $name
|
|
if ($SCCMMatches -and $SCCMMatches.Count -lt 2) {
|
|
return $SCCMMatches,''
|
|
} elseif (!$SCCMMatches) { $errMsg += "No SCCM match 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
|
|
if ($cmdbMatches -and $cmdbMatches.Count -lt 2) {
|
|
return $cmdbMatches.Name,''
|
|
} elseif (!$cmdbMatches) { $errMsg += "No CMDB match 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" }
|
|
}
|
|
if ($name -match "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$") {
|
|
try {
|
|
$cmdbData = Search-ISMBO -bo cis -Filter "MACAddress eq '$name'" -RawFilter
|
|
} catch { $cmdbData = $null }
|
|
if ( $cmdbData ) {
|
|
if (Resolve-DnsName $cmdbData.Name) { return $cmdbData.Name,'' }
|
|
else { $errMsg += "CMDB name associated with MAC Address $name doesn't resolve to IP Address"}
|
|
} else { $errMsg += "MAC Address 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
|
|
} |