Tuesday, August 02, 2011

PowerShell script to determine if a machine has two or more local interfaces

First, create a txt file (e.g. d:\server.txt) containing a list of servers:

10-2411A0407
10-2411A0408
10-2411A0409

Second, run the following script block:

$Servers = Get-Content D:\Server.txt
foreach ($s in $Servers)
{
  $Interfaces = Invoke-Command -ComputerName $s -Command {Get-WmiObject Win32_NetworkAdapterConfiguration}
  $NumInterfaces = $Interfaces.Length
  $LocalInterfaces = 0
  $LocalIP = ""
  foreach ($If in $Interfaces)
  {
    $IsLocal = $False
    foreach ($ip in $If.IPAddress)
    {     
      if ($ip.StartsWith("10.10."))
      {
        $IsLocal = $True
        break;
      }
    }
    if ($IsLocal)
    {
      $LocalInterfaces++
      $LocalIP += $ip + " "
    }
  }
  if ($LocalInterfaces -ge 2)
  {
    $s
    $LocalIP   
  }
}

Finally, here is what I got out of it:

10-2411A0407
10.10.11.43 10.10.10.87
10-2411A0408
10.10.11.30 10.10.10.83
10-2411A0409
10.10.10.107 10.10.11.28