30 lines
975 B
PowerShell

function GetDigestRealm($hostName, [bool]$tls)
{
# Ping device to get HTTP headers.
$url = if($tls -eq $true) { "https://" + $hostName + ":16993/wsman" } else { "http://" + $hostName + ":16992/wsman" }
$webReq = [System.Net.HttpWebRequest]::Create($url)
$webReq.Method = "GET"
$webReq.ContentLength = 0
$response = $webReq.GetResponse()
trap [System.Net.WebException]
{
# Find authenticate header.
for ($index = 0; $index -lt $_.Exception.Response.Headers.Count; $index++)
{
$headers = $_.Exception.Response.Headers
if($headers.GetKey($index).Equals("WWW-Authenticate"))
{
# Get the discovered digest realm string.
$realm = 'Digest realm="'
$value = $headers.Get($index)
# Extract the digest realm.
$start = $value.IndexOf($realm)
$end = $value.IndexOf('"', $start + $realm.Length + 1)
return $value.Substring($start + $realm.Length, $end - $start - $realm.Length)
}
}
}
}