PowerShell: Pass a string and check if the entry exists in Windows Host File
While working on a brand-new Windows 11 Azure Spot VM and as part of setting up Sitecore MVP Site, one of the issues I faced is ERROR CONNECTING TO HTTPS://ID.SC-MVP.LOCALHOST/.WELL-KNOWN/OPENID-CONFIGURATION. The reason is, the host file entries get wiped-off on every restart in case of Azure Spot VMs (since ip is regenerated every time). As a result, I would be presented with the concerned connectivity error every time I execute Start-Environment.ps1. When I checked the Start-Environment.ps1, the Add-HostsEntry is within a if-block that assumes if certs are generated, there is no need to add hosts file entry in Start-Environment.ps1:
So, the solution is, irrespective of certs generated, check the host file for the related domain entries and if not present, add those. Also, I thought a function to check if a given host name exists in the host file would be handy before adding a host name entry. The Check-HostNameExists function I put-together here below does the job of checking if a specific string exists in the host file.I added this function to C:\mvp-site\docker\tools\Init-Env.psm1
The Initialize-HostNames function needs to have the check before doing any addition to host file:
Then, added the check to the Start-Environment.ps1 too as follows:
With these in place, my host name was added automatically, if not present, and helped me forego ERROR CONNECTING TO HTTPS://ID.SC-MVP.LOCALHOST/.WELL-KNOWN/OPENID-CONFIGURATION on an Azure spot VM.
Check-HostNameExists Function:
As separate files:
Check-HostNameCallingScript.ps1
#Reference: https://www.sapien.com/blog/wp-content/uploads/2009/02/parse-hosts.txt
# ==============================================================================================
#
# Microsoft PowerShell Source File
#
# NAME: Check-HostNameEntry.ps1
#
# USAGE: .\Check-HostNameEntry.ps1 -HostNameString "entrytocheck"
#
#Reference: https://www.sapien.com/blog/wp-content/uploads/2009/02/parse-hosts.txt
#
# COMMENT: This script will check if the local computer windows hosts file has a specific entry
#
#returns $true or $false
#can be used in calling script as follows:
#$entryPresent=.\Check-HostNameExists.ps1 "cm.sitecore.local"
#if ($entryPresent -eq $false)
#{write-host "not present"}
#else {write-host "present"}
# ==============================================================================================
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$HostNameString
)
try
{
#write-host "Trying to match : $HostNameString"
$result=$false
#define a regex to return first NON-whitespace character
[regex]$r="\S"
#strip out any lines beginning with # and blank lines
$HostsData = Get-Content $env:windir\system32\drivers\etc\hosts | where {
(($r.Match($_)).value -ne "#") -and ($_ -notmatch "^\s+$") -and ($_.Length -gt 0)
}
#write-host $HostsData
if ($HostsData){
#only process if something was found in HOSTS
$HostsData | foreach {
#created named values
$_ -match "(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?<HOSTNAME>\S+)" | Out-Null
$ip=$matches.ip
$hostname=$matches.hostname
if ($result -eq $false)
{
write-host $hostname
if ($hostname -eq $HostNameString) {
$result=$true
Write-Host "matched"
return $result
#break
}
}
else
{
return $result
#break
}
} #end ForEach
} #end If $HostsData
else {
Write-Host ("{0} has no entries in its HOSTS file." -f $computername.toUpper()) -foreground Magenta
$result=$false
}
return $result
}
catch
{
write-host "error"
$result=$false
return $result
}

Comments
Post a Comment