I have a powershell script which runs some command on a local or remote computer. When the computer is remote then the command is called via Invoke-Command and a user is prompted for additional credentials.
A user can enter a script parameter which can be: hostname, IP, 127.0.0.1, alias from hosts file. I need to check if that parameter is for local or remote machine in order to call local command or Invoke-Command. I used to do it this way:
Function IsLocalhost {
Param([string] $srvname)
$script:AVLserverHost = $srvname.Split('\')[0]
#Write-Host ("HOST: " + $script:AVLserverHost)
if ((get-content env:computername) -eq $script:AVLserverHost) {
return $true;
} else {
$AddressList = @(([net.dns]::GetHostEntry($script:AVLserverHost)).AddressList)
$script:HostIp = $AddressList.IpAddressToString
$name = [System.Net.Dns]::gethostentry($script:HostIp)
if ((get-content env:computername) -eq $name.HostName) {
return $true
}
}
return $false
}
But it only works on a domain DNS. Our computers are on a workgroup or a standalone computers that we can connect only by IP or alias from hosts file.
So how to check in powershell (or c# code) if given host is local or not, if the host is not in a domain DNS. I would like to have true/false if it is local or not and its real IP address if hostname or host alias was entered.