PowerShell WMI

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



Use PowerShell to Enable / Disable a NIC

Employ PowerShell to Enable / Disable a NIC

Win32_NetworkAdapter has not only properties, but also 'dot' methods such as .disable() and .enable().

Topics for PowerShell Enable / Disable Network Adapters

 ♣

Preliminary PowerShell Script  To Check Network Connections

To gain a better understanding of your network card's properties let us run a preliminary script to enumerate the connections.  In particular we wish to know the values for NetConnectionStatus (2,4, or 7).  We also need the values for DeviceID.

# PowerShell script to check your active NICs
# Author: Guy Thomas
# Version 4.5 February 2010 tested on PowerShell v 1.0 and 2.0

Get-WmiObject -Class Win32_NetworkAdapter | Format-Table `
Name, NetEnabled, NetConnectionStatus, DeviceId -auto

Note 1:  The key property is NetEnabled.  In the output we are looking for values of 'True'.  Also the NetConnectionStatus of active NICs will be 2 and not 7.

Note 2:  PowerShell has no word-wrap, thus we use the backtick ` to tell the command to continue on the next line. 

Trap: With the backtick there should be no space after the `.

Note 3:  Observe PowerShell's trademark the (|) pipe symbol, this means that the output of the main command is pumped into Format-Table.  I chose, NetEnabled, NetConnectionStatus, DeviceId from the  dozens of possible Win32_NetworkAdapter properties of to display.

Note 4:  Improvement, we could add a -computerName parameter, with a value of a computer on your network.  For example: Get-WmiObject -Class Win32_NetworkAdapter -computerName BIGSERVER.

How To Disable a Network Card with PowerShell

Preliminaries - Vital for Success.  Decide which machine you are configuring, this script is set for LocalHost.  Important: Your DeviceId is unlikely to be 17, so please research and amend for your computer.

# PowerShell script to disable a NIC with a DeviceId of 17.
# Author: Guy Thomas
## Version 2.3 February 2010 tested on PowerShell v 1.0 and 2.0

Clear-host
$Nic = Get-WmiObject win32_networkadapter -computerName LocalHost `
-filter "DeviceId = 17"
$Nic.disable()

Note 1:  If the script did not work, then change DeviceId =17.  You could also check which machine you wish to disable.  Consider adjusting the value of -ComputerName ???

Note 2:  To reverse the script and ENABLE a nic the command is $Nic.enable().

Note 3:  Check your results by running the above script.

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Filter the Output for 'Real' Network Cards

This example also employs -computerName LocalHost, which you could change to the name, or IP address of a machine on your network.

# PowerShell script to list your 'Real' network cards.
# Author: Guy Thomas
## Version 1.5 February 2010 tested on PowerShell v 1.0 and 2.0

Clear-Host
Get-WmiObject -Class win32_networkadapter -computerName LocalHost `
-filter "NetConnectionStatus = 2" | `
Format-Table Name, NetEnabled, NetConnectionStatus, DeviceId -auto

Note 1:  The filtering is achieved through this clause:
-filter "NetConnectionStatus = 2"   Actually, you could a 'Where' clause thus:
Where-Object {$_.NetConnectionStatus -eq '2'}

Note 2:  PowerShell has no word-wrap, thus the backtick ` means continue on the next line.  Beware, there should be no space after the `.

Challenge: Change -computerName LocalHost to the value of a machine on your network.

Next Step:  To discover the IP address try the sister class Win32_NetworkAdapterConfiguration

 How to List More WMI Network Classes

The additional feature of this script is that refines the search from the broad 'Win32', to the narrower 'Win32_Network'.  The result is a list of network WMI classes.

# PowerShell example to list every WMI class matching Win32_network
# Author: Guy Thomas
# Version 1.5 February 2010 tested on PowerShell v 1.0 and 2.0

clear-Host
$i=0
$Type = "Win32_network"
$WMI = get-WmiObject -list | Where-Object {$_.name -match $Type}
Foreach ($Class in $WMI) {$Class.name; $i++}
Write-Host 'There are' $i' types of '$Type

Learning Points

Note 1:  In practical terms, most of the 7 network classes are disappointing.  However, the class Win32_NetworkAdapterConfiguration has the useful property of DefaultIPGateway and IpAddress.

Summary of Use PowerShell to Enable / Disable a NIC

The key concept is that you need a .disable() method.  But first you need to get the Win32_NetworkAdapter class.  I have also included a separate script so that you can check the status of the NetConnectionStatus before and after you attempt to enable or disable the NIC.

See More Microsoft PowerShell WMI Examples:

Home   • PowerShell WMI   • WMI Services   • Memory   • Disk   • DNS   • Win32_pingstatus

WMI Class   • Win32_NetworkAdapter   • Win32_NetworkAdapterConfiguration   • Disable NIC

Examples   • Win32_product   • PowerShell -Query   • PowerShell -Filter

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.