2024-07-15 15:54:31 +00:00
|
|
|
function Get-Hostname ([string]$name) {
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'SCCM Asset Tag Lookup' -PercentComplete 10 -ParentId 1
|
2024-07-24 22:37:27 +00:00
|
|
|
$errMsg = ''
|
2024-07-11 18:33:30 +00:00
|
|
|
if ($name.Length -eq 5) {
|
|
|
|
|
$res = Get-AssetConversion $name
|
2024-09-18 18:15:13 +00:00
|
|
|
if ($res) {
|
|
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
|
|
|
|
return $res,''
|
|
|
|
|
} else { $errMsg += "$name Asset Tag not in SMBIOS`n" }
|
2024-09-13 21:57:17 +00:00
|
|
|
# We don't check CMDB asset tags here because they often resolve to
|
|
|
|
|
# something other than the hostname, like the asset tag itself
|
2024-07-11 18:33:30 +00:00
|
|
|
}
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'IP Address resolution' -PercentComplete 20 -ParentId 1
|
2024-07-11 18:33:30 +00:00
|
|
|
# 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")) {
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
2024-08-29 16:44:47 +00:00
|
|
|
return $Matches[0],''
|
2024-07-24 22:37:27 +00:00
|
|
|
} else {
|
2024-09-11 21:02:15 +00:00
|
|
|
$errMsg += "$name IP Address couldn't be resolved to hostname`n"
|
2024-07-11 18:33:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'SCCM Serial Number lookup' -PercentComplete 30 -ParentId 1
|
2024-07-12 20:50:15 +00:00
|
|
|
if ($name.Length -eq 7) {
|
|
|
|
|
$res = Get-ServiceTagConversion $name
|
2024-09-18 18:15:13 +00:00
|
|
|
if ($res) {
|
|
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
|
|
|
|
return $res,''
|
|
|
|
|
} else { $errMsg += "$name Service Tag not found in SCCM`n"}
|
2024-07-12 20:50:15 +00:00
|
|
|
}
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'SCCM MAC Address lookup' -PercentComplete 40 -ParentId 1
|
2024-07-11 18:33:30 +00:00
|
|
|
# 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})$") {
|
2024-07-12 20:50:15 +00:00
|
|
|
$res = Get-SCCM_PCFromMAC $name
|
2024-09-18 18:15:13 +00:00
|
|
|
if ($res) {
|
|
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
|
|
|
|
return $res,''
|
|
|
|
|
} else { $errMsg += "$name MAC Address not found in SCCM`n"}
|
2024-07-11 18:33:30 +00:00
|
|
|
}
|
2024-08-29 16:44:47 +00:00
|
|
|
|
2024-09-11 21:02:15 +00:00
|
|
|
# Last resort checks
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'SCCM Hostname match' -PercentComplete 50 -ParentId 1
|
|
|
|
|
$SCCMMatches = Get-SCCM_HostnameMatch $name
|
2024-09-11 21:02:15 +00:00
|
|
|
if ($SCCMMatches -and $SCCMMatches.Count -lt 2) {
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
2024-09-12 23:46:24 +00:00
|
|
|
return $SCCMMatches,''
|
2024-09-13 23:55:52 +00:00
|
|
|
} elseif (!$SCCMMatches) { $errMsg += "No SCCM name match found`n" }
|
2024-09-16 20:32:28 +00:00
|
|
|
elseif ($SCCMMatches.Count -ge 2) { $errMsg += "Many SCCM name matches found`n" }
|
2024-09-11 21:02:15 +00:00
|
|
|
|
2024-09-13 21:57:17 +00:00
|
|
|
# CMDB check should be absolute last resort
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB General search' -PercentComplete 60 -ParentId 1
|
2024-08-29 16:44:47 +00:00
|
|
|
$cmdbMatches = Find-ISMBO -bo cis -SearchQuery $name
|
|
|
|
|
if ($cmdbMatches -and $cmdbMatches.Count -lt 2) {
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
2024-08-29 16:44:47 +00:00
|
|
|
return $cmdbMatches.Name,''
|
2024-09-13 23:55:52 +00:00
|
|
|
} elseif (!$cmdbMatches) { $errMsg += "No CMDB name match found`n" }
|
2024-09-13 21:57:17 +00:00
|
|
|
elseif ($cmdbMatches.Count -ge 2) {
|
|
|
|
|
# Try more specific queries
|
|
|
|
|
# Asset tag
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB Asset Tag search' -PercentComplete 70 -ParentId 1
|
2024-09-13 21:57:17 +00:00
|
|
|
if ($name.Length -eq 5) {
|
|
|
|
|
try {
|
|
|
|
|
$cmdbData = Search-ISMBO -bo cis -Filter "AssetTag eq '$name'" -RawFilter
|
|
|
|
|
} catch { $cmdbData = $null }
|
|
|
|
|
if ( $cmdbData ) {
|
2024-09-18 18:15:13 +00:00
|
|
|
if (Resolve-DnsName $cmdbData.Name) {
|
|
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
|
|
|
|
return $cmdbData.Name,''
|
|
|
|
|
} else { $errMsg += "CMDB name associated with Asset tag $name doesn't resolve to IP Address"}
|
2024-09-13 21:57:17 +00:00
|
|
|
} else { $errMsg += "Asset tag not in CMDB" }
|
|
|
|
|
}
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB MAC Address search' -PercentComplete 80 -ParentId 1
|
2024-09-13 23:32:38 +00:00
|
|
|
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 ) {
|
2024-09-18 18:15:13 +00:00
|
|
|
if (Resolve-DnsName $cmdbData.Name) {
|
|
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
|
|
|
|
return $cmdbData.Name,''
|
|
|
|
|
} else { $errMsg += "CMDB name associated with MAC Address $name doesn't resolve to IP Address"}
|
2024-09-13 23:32:38 +00:00
|
|
|
} else { $errMsg += "MAC Address not in CMDB" }
|
|
|
|
|
}
|
2024-09-13 21:57:17 +00:00
|
|
|
# Serial number
|
2024-09-18 18:15:13 +00:00
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Status 'CMDB Serial Number search' -PercentComplete 80 -ParentId 1
|
2024-09-13 21:57:17 +00:00
|
|
|
try {
|
|
|
|
|
$cmdbData = Search-ISMBO -bo cis -Filter "SerialNumber eq '$name'" -RawFilter
|
|
|
|
|
} catch { $cmdbData = $null }
|
|
|
|
|
if ( $cmdbData ) {
|
2024-09-18 18:15:13 +00:00
|
|
|
if (Resolve-DnsName $cmdbData.Name) {
|
|
|
|
|
Write-Progress -Activity "Resolving hostname for $name" -Completed -ParentId 1
|
|
|
|
|
return $cmdbData.Name,''
|
|
|
|
|
} else { $errMsg += "CMDB name associated with Serial Number $name doesn't resolve to IP Address"}
|
2024-09-13 21:57:17 +00:00
|
|
|
} else { $errMsg += "Serial Number not in CMDB" }
|
|
|
|
|
$errMsg += "Many CMDB matches found`n"
|
|
|
|
|
}
|
2024-08-29 16:44:47 +00:00
|
|
|
|
2024-07-24 22:37:27 +00:00
|
|
|
return $name, $errMsg
|
2024-07-11 18:33:30 +00:00
|
|
|
}
|