/* ]]> */
Mar 192010
 

If you ever need to check DNS, WINS, Network Mask, or any other Network settings on a number of hosts, this is the PowerShell solution for you.

Update (8/22/2011): For a complete ready-to-run WMI query utility that includes this and other useful data, be sure to see my GetHostInfo utility.

Windows Management Instrumentation (WMI) can retrieve a computer’s Network Adapter (hardware) and Configuration setup using the following two classes:

  • win32_NetworkAdapter
  • win32_NetworkAdapterConfiguration

Since there can be multiple physical (and virtual) network adapters, it may become necessary to tie instances from the two classes together in order to relate an adapter to its corresponding configuration.

This relationship can be made using the following properties:

  • win32_NetworkAdapter : DeviceID or index (in all my tests both properties have the same value)
  • win32_NetworkAdapterConfiguration : index

Here’s my technique to collect Network Adapter and Configuration information:

To start, we must query the win32_NetworkAdapter class for instances where there is an active connection using the NetConnectionStatus property (NetConnectionStatus = 2 (connected))

Sample Code:

?View Code POWERSHELL
$hostname =  "localhost"
$adapters = gwmi -query "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2" -computer $hostname

Next, for each instance found in $adapters variable (remember, there can be multiple network adapters) we must query the corresponding NetworkAdapterConfiguration entry. This is done by querying win32_NetworkAdapterConfiguration where the index matches the win32_NetworkAdapter class’ DeviceId

 

Here we will expand the previous sample to use the win32_NetworkAdapter query result to create a corresponding win32_NetworkAdapterConfiguration query. For easier readability we will add some additional code to expand the DNSDomainSuffixSearchOrder in a separate list

Sample Code:

?View Code POWERSHELL
$hostname =  "localhost"
$adapters = gwmi -query "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2" -computer $hostname
 
Foreach ($adapter in $adapters) {
    Write-host "  Active Adapter Found (win32_NetworkAdapter):".PadRight(60," ")  `
        -backgroundcolor cyan -foregroundcolor black
 
    $adapter | fl DeviceId,AdapterType,Description,NetConnectionId,ProductName,ServiceName
 
    $qry = [string]::format("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled='True' and index={0}", $adapter.DeviceId)
 
    $x = gwmi -query $qry -computer $hostname
 
    foreach ($entry in $x) {
        Write-host "  Configuration (win32_NetworkAdapterConfiguration):".PadRight(60," ") `
            -backgroundcolor cyan -foregroundcolor black
 
        $entry | fl Index,DNSHostName,Caption,Description,IPAddress, `
            DHCPEnabled,DefaultIPGateway,DNSServerSearchOrder, `
            WINSPrimaryServer,WINSSecondaryServer,macaddress | out-default
 
        "DNSDomainSuffixSearchOrder:" | out-default
        "===========================" | out-default
        $suffix_cnt = 0
        foreach ($suffix in $entry.DNSDomainSuffixSearchOrder) {
            $suffix_cnt += 1
            "    $suffix_cnt : $suffix" | out-default
            }
        }
    }

Sample Output:

  Active Adapter Found (win32_NetworkAdapter):
DeviceId        : 1
AdapterType     : Ethernet 802.3
Description     : Intel 21140-Based PCI Fast Ethernet Adapter (Generic)
NetConnectionId : Local Area Connection
ProductName     : Intel 21140-Based PCI Fast Ethernet Adapter (Generic)
ServiceName     : DC21x4
  Configuration (win32_NetworkAdapterConfiguration):
Index                : 1
DNSHostName          : Kahuna_laptop
Caption              : [00000001] Intel 21140-Based PCI Fast Ethernet Adapter (Generic)
Description          : Intel 21140-Based PCI Fast Ethernet Adapter (Generic) - Packet Scheduler Miniport
IPAddress            : {192.168.1.121}
DHCPEnabled          : True
DefaultIPGateway     : {192.168.1.1}
DNSServerSearchOrder : {192.168.1.1}
WINSPrimaryServer    :
WINSSecondaryServer  :
macaddress           : 00:03:FF:17:80:B8
DNSDomainSuffixSearchOrder:
===========================
    1 : MyDomain.com
    2 : PoshTips.com

 Posted by at 12:05 pm

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Page optimized by WP Minify WordPress Plugin