检查多个服务器的IP地址多个nics powershell

时间:2012-03-14 15:38:27

标签: powershell powershell-v2.0 ping

我管理了大量的服务器,我们设置了服务器名称(DNS记录)ping的地方。我的一些服务器也有多个服务器。所以我需要的是一个脚本,我可以ping多个服务器,它将返回该服务器的每个nic的ip地址。如果它可以导出到csv那也很好。

2 个答案:

答案 0 :(得分:0)

这可以帮到你:

function PingServer($serverName)
{
   (Test-Connection $serverName -Count 1 -ErrorAction SilentlyContinue).IPV4Address.IPAddressToString
}

$servers = "server1.com", "server2.com", "server3.com"

$hash = @{}
foreach($server in $servers)
{
   $hash.Add($server, (PingServer $server))
}
$hash.GetEnumerator() | ForEach-Object { New-Object PSObject -Property @{ ServerName = $_.Key; IPAddress = $_.Value } | Select-Object -Property ServerName, IPAddress } | Export-CSV -Path c:\PST\1.csv -NoTypeInformation

您可能需要为多个网络接口稍微更改一下

答案 1 :(得分:0)

在这里,这适用于我,我用它从服务器检索所有IPv4地址。请记住,这个测试可以在尝试获取信息之前先ping它,并使用WMI来获取所需的数据。

#Get NIC IP Addresses
Function Get-NICIPAddresses
{
    param
    (
        $ComputerName
    )

    Try
    {
        $NICIPInformation = gwmi Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE" -ComputerName $ComputerName -ErrorAction "Stop"
    }
    Catch
    {
        Write-Host "$($_.exception)" -ForegroundColor Yellow
        return $Null
    }

    return $NICIPInformation
}

$Computer = "127.0.0.1"

if (Test-Connection -Quiet $Computer)
{
    #WMI IP Addresses
    [array]$IPInformation = Get-NICIPAddresses $Computer

    #IP Addresses
    if ($IPInformation.Count -gt 0)
    {
        #Array to hold all IP Addresses
        [array]$IPAddresses = @()

        #Loop through the IP Addresses, look for all IP addresses that match the AddressFamily "InterNetwork" - AKA IPv4 - http://msdn.microsoft.com/en-us/library/system.net.sockets.addressfamily.aspx
        foreach ($IPAddressInformation in $IPInformation)
        {
            #Use System.Net.IPAddress array to match the IP address values to acutal IP Addresses
            $IPAddresses += [System.Net.IPAddress[]]$IPAddressInformation.IPAddress | ? {$_.AddressFamily -eq "InterNetwork"}
        }

        #Sort the values from smallest to largest IP addresses
        $IPAddresses = $IPAddresses | % {$_.IPAddressToString} | Sort {"{0:d3}.{1:d3}.{2:d3}.{3:d3}" -f @([int[]]$_.split("."))}

        #Return string results, or $Null depending upon input.
        if ($IPAddresses.Count -gt 0)
        {
            $IPAddresses = $IPAddresses -Join " ; "
        }
        else
        {
            $IPAddresses = $Null
        }
    }
    else
    {
        $IPAddresses = $Null
    }

    #Return NIC info
    foreach ($NIC in $IPInformation)
    {
        $NIC
    }

    #Current IPv4 Addresses
    $IPAddresses
}