Using PowerCli to gather NIC settings

We had some changes recently in the environment, and the old DNS and WINS servers were decommissioned.  So I was tasked with getting all of the NIC settings of the virtual machines so we can see how many were using the old settings.  So I started with a simple WMI call to gather the settings from the NICs.

WMI Method
$computerlist = (Get-Cluster "ClusterName" | Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Sort -Property Name).name

Foreach ($computer in $computerlist){
   $networksettings = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $computer -EA Stop | ? {$_.IPEnabled}
   Foreach ($network in $networksettings){
       $Line = "" | Select "ComputerName    ","IPAddress       ","SubnetMask      ","DefaultGateway  ","PrimaryDNS      ","SecondaryDNS    ","PrimaryWINS     ","SecondaryWINS   "
       $Line."ComputerName    "    = $computer
       $Line."IPAddress       "    = $network.IPAddress[0]
       $Line."SubnetMask      "    = $network.IPSubnet[0]
       $Line."DefaultGateway  "    = $network.DefaultIPGateway[0]
       $Line."PrimaryDNS      "    = $network.DNSServerSearchOrder[0]
       $Line."SecondaryDNS    "    = $network.DNSServerSearchOrder[1]
       $Line."PrimaryWINS     "    = $network.WINSPrimaryServer
       $Line."SecondaryWINS   "    = $network.WINSSecondaryServer
       $NetworkInformation        += $Line
   }
}
$NetworkInformation

This approach seem good in theory, but it missed about a third of the servers in the VM cluster that I targeted. So I need to find a more reliable way to gather the settings from the servers without leaving any out. I started to mess around with a script that would gather the data via PowerCLI. I remembered that I was able to get the IPs from the VMs using PowerCLI, so I started to dig into the ExtendedData properties under the Get-VM cmdlet.

POWERCLI Method
$NICSettingsReport = @()
#Get WINS and DNS from VMs via PowerCLI
$VMs = Get-Cluster "ClusterName" | Get-VM 
    Foreach ($VM in $VMs){
        $Line               = "" | Select Name, DNS1, DNS2, WINS1, WINS2
        $Line.Name          = (Get-VMGuest $VM).HostName #VM.Name
        $Line.DNS1          = $VM.ExtensionData.Guest.net.dnsconfig.IpAddress[0]
        $Line.DNS2          = $VM.ExtensionData.Guest.net.dnsconfig.IpAddress[1]
        $Line.WINS1         = $VM.ExtensionData.Guest.net.netbiosconfig.primarywins
        $Line.WINS2         = $VM.ExtensionData.Guest.net.netbiosconfig.SecondaryWINS
        $NICSettingsReport += $line
     }
$NICSettingsReport

So the latter option is the best option for my environment, as we are very diligent to keep the VMware Tools installed and up to date on all of our virtual servers. So this was the most complete information that I was able to get all of the NIC settings.

2 thoughts on “Using PowerCli to gather NIC settings”

  1. You could save yourself some time by using get-view, we have clusters with quite a few VMs:

    $vmOBJs = @()
    $VMs = get-view -viewtype VirtualMachine -searchroot (get-cluster ).id
    foreach ($computer in $VMs){
    $props = [ordered]@{vmName = $computer.name
    priDNS = $computer.guest.net.dnsconfig.ipaddress[0]
    secDNS = $computer.guest.net.dnsconfig.ipaddress[1]
    priWINs = $computer.guest.net.netbiosconfig.primarywins
    secWINs = $computer.guest.net.netbiosconfig.secondarywins
    }
    $vmOBJ = new-object -typename PSObject -property $props
    $vmOBJs += $vmOBJ
    }
    $vmOBJs

    1. Thanks for your comment Vincent, and it does prove to be a better solution than what I presented. I need to familiarize myself more with the Get-View cmdlet when possible.

Leave a Reply

Your email address will not be published. Required fields are marked *