簡體   English   中英

如果IP地址或主機名是本地主機,如何簽入Powershell? 沒有域名DNS

[英]How to check in Powershell if IP address or hostname is a localhost? Without domain DNS

我有一個powershell腳本,它在本地或遠程計算機上運行一些命令。 當計算機處於遠程狀態時,將通過Invoke-Command調用該命令,並提示用戶輸入其他憑據。

用戶可以輸入腳本參數,該參數可以是:hostname,IP,127.0.0.1,來自hosts文件的別名。 我需要檢查該參數是否適用於本地或遠程計算機,以便調用本地命令或Invoke-Command。 我曾經這樣做過:

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
}

但它只適用於域DNS。 我們的計算機位於工作組或獨立計算機上,我們只能通過IP或來自hosts文件的別名連接。

因此,如果主機不在域DNS中,如果給定主機是本地的,如何檢查powershell(或c#代碼)。 如果它是本地的,我想有真/假,如果輸入了主機名或主機別名,它的真實IP地址。

你可以試試這個:

function Is-LoopBackAddress([string] $Address)
{
    $addressIPs = [Net.Dns]::GetHostAddresses($Address).IPAddressToString

    $netInterfaces = [Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()

    foreach ($netInterface in $netInterfaces)
    {
        $ipProperties = $netInterface.GetIPProperties()

        foreach ($ip in $ipProperties.UnicastAddresses)
        {
            if ($addressIPs -contains $ip.Address.IPAddressToString)
            {
                return $true
            }
        }
    }

    return $false
}

Is-LoopBackAddress 'localhost'

你可以生成一個本地IP數組,將localhost添加到數組中,然后查看你的參數是否在數組中。 這是ipv4的ps2.0兼容版本。

$LocalArray = @()
$LocalArray += (gwmi Win32_NetworkAdapterConfiguration | ? {$_.IPAddress}) | select -expand ipaddress | select-string -notmatch ":"
$LocalArray += 'localhost'
$LocalArray += '127.0.0.1'
$LocalArray += hostname
$LocalArray -contains $IP

我將@Noah的代碼與其他示例相結合,如何讀取hosts文件,這是最終代碼:

Function IsLocalhost {
    Param([string] $srvname)

    $script:AVLserverHost = $srvname.Split('\')[0]

    #get list of localhost addresses
    $localAddresses = @()
    $localAddresses += (gwmi Win32_NetworkAdapterConfiguration | ? {$_.IPAddress}) | select -expand ipaddress | select-string -notmatch ":"
    $localAddresses += 'localhost'
    $localAddresses += '127.0.0.1'
    $localAddresses += hostname

    $hosts = "$env:windir\System32\drivers\etc\hosts"

    [regex]$r="\S"   #define a regex to return first NON-whitespace character

    #strip out any lines beginning with # and blank lines
    $HostsData = Get-Content $hosts | where {
        (($r.Match($_)).value -ne "#") -and ($_ -notmatch "^\s+$") -and ($_.Length -gt 0)
    }

    $HostsData | foreach {
        $_ -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 ($localAddresses -contains $ip) {
            $localAddresses += $hostname
        }
    }

    if ($localAddresses -contains $script:AVLserverHost) {
        return $true;
    } else {
        $script:HostIp = (Test-Connection $script:AVLserverHost | Select-Object IPV4Address)[0].IPV4Address.IPAddressToString
    }
    return $false
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM