Scripting WMI objects with
PowerShell is a
particularly productive area. On this page I will illustrate PowerShell's capabilities by creating a script which is more flexible than Ipconfig.
Topics for
PowerShell: More flexible than Ipconfig
On this page our mission is to extract TCP/IP information from a machine's network adapters
using PowerShell. But first a question: 'Is Guy reinventing the wheel? Can Ipconfig, with its numerous switches,
provide the same information?'
To answer this question is easy, all you have to do is compare Ipconfig with my
PowerShell scripts, even better, compare Ipconfig with YOUR scripts. If I could sum up the benefit of
PowerShell over Ipconfig,
in one word, that
word would be flexibility. For example, you can customize which TCP/IP properties to display, and in addition, you can interrogate the network adapters on other machines. Can Ipconfig do that?
I have not found away.
PowerShell Objectives
WMIObject -list (parameter to enumerate
all possible object classes) WMIObject | get-Member (discover which properties suit our mission)
PowerShell: More flexible than Ipconfig. (Use the 'Where' clause to filter the output) Also remember that you could also run Ipconfig itself from the
PowerShell
command line.
Guy's Advice
Either work
through my learning progression by starting with Objective 1 (recommended), or else if you are in a hurry, cut to the chase, and head for Objective 3
PowerShell: More flexible than Ipconfig.
Here is a cmdlet which identifies all the Network WMI objects.
$colItems = get-wmiobject -list | where {$_.name -match "network"} $colItems |ft name
Learning Points
Note 1: Because PowerShell is so simple, yet so efficient, we need fewer commands than for a corresponding VBScript. Indeed, the bare minimum to get
started, and display all the WMI objects is these 8 letters with a dash in the middle: gwmi -list # gwmi is an alias for get-wmiobject
Note 2: Observe the (|) pipe. You will use this
symbol hundreds of times in PowerShell, what
happens is the output of the wmiobject list is pumped into the 'where' clause, which pulls out all the entries that contain the word "network". My thinking was
get-wmiobject -list produces too many objects.
Note 3: I deliberately don't use many aliases in my
scripts, but I make an exception for ft
(format-Table) because it's so useful for controlling the display of
PowerShell's output. Do try the above script with, and without, the last word,
'name'.
This script identifies the properties available for the object: Win32_NetworkAdapterConfiguration. Here below is a classic example of the get-Member construction.
Note 1: Observe how -class homes in on the object that we are interested in, namely: Win32_NetworkAdapterConfiguration.
Note 2: If you wished to manipulate the
TCP/IP settings, you could investigate which methods are available by substituting 'Method' for the last word, 'Property'. Alternatively, you could omit the
'-Membertype Property' phrase altogether.
Guy Recommends: SolarWinds LANSurveyor
LANSurveyor will produce a neat diagram of your network topology. But that's
just the start;
LANSurveyor can
create an inventory of the hardware and software
of your machines and network devices. Other neat features include dynamic
update for when you add new devices to your network. I also love the ability to export
the diagrams
to Microsoft Visio.
Finally, Guy bets that if you take a free trial of LANSurveyor then you will
find a device on your network that you had forgotten about, or someone else
installed without you realizing!
Note 1: The properties that I have chosen are not important. This is just an example to get you started; it would make my day if you substituted properties that you researched from Example 2:
Get-Member, for example, .DefaultGateway or .IPSubnet instead of .MacAddress.
Note 2: My old friend 'Barking' Eddie thought that my printer needed cleaning when he saw ` in my script.
Actually this character (`), found at the top left of the keyboard, is called a backtick. What the backtick does is tell
PowerShell - 'This command continues on the next line'. Just to be clear, this character corresponds to ASCII 096, and is not a
misplaced comma!
Note 3: To loop though all the network adapters, I chose the ForEach construction. In
PowerShell in general, and ForEach in particular, the type of bracket is highly significant; (ellipses for the
condition) and {curly for the block command}.
Note 4: Most of my scripts employ the
'Where' clause to filter the output. In this case I wanted to discard
data concerning any virtual network cards.
Note 5: I almost forgot, $strComputer controls which computer you are analysing. Again, it would make my day if altered "." to the hostname of another machine on your network.
˚
Summary of
PowerShell: More flexible than Ipconfig
The aim of this script is to show you that
PowerShell can not only mimic Ipconfig, but also exceed its capabilities. In particular, I wanted to show how
PowerShell could customise
list of TCP/IP properties in its output. It would also be useful to create a script which can display TCP/IP information from other machines on the network.
If you see an error of any kind, do let me know. 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.
*
Guy
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.