PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 141 - PowerShell: More Flexible than Ipconfig

 PowerShell: More Flexible than Ipconfig

Scripting WMI objects with PowerShell is a particularly fertile area for scripting.  In this edition I will illustrate PowerShell's capabilities by creating a script which is more flexible than Ipconfig.

Topics for PowerShell: More flexible than Ipconfig

This Week's Secret - Passing the baton.

As far as my own scripting career, and that of this ezine, I see the scripting baton passing from VBScript to PowerShell.  Where appropriate, I will try and write scripts in both languages, (as I did for scripts to delete temp files).  At the risk of repeating myself, it is WMI scripting that is the killer reason for learning PowerShell in the Summer of 2007.

This Week's Mission

This week we will put PowerShell to work extracting TCP/IP information from a machine's network adapters.  But first a question: 'Is Guy reinventing the wheel?  Can Ipconfig, with its numerous switches, do it all?' 

The answer is easy: Prove or disprove my belief by comparing 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 possible objects)
WMIObject | get-member (discover which properties suit our mission)
PowerShell: More flexible than Ipconfig.  (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), else if you are in a hurry, cut to the chase and head for Objective 3 PowerShell: More flexible than Ipconfig.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


Pre-requisites and Checklist

Download PowerShell from Microsoft's site.  One interesting point is that there are different versions of PowerShell for XP, Windows Server 2003 and Vista.PowerShell copy and paste

Method 1 (Quick)

  • Copy the code into memory
  • Launch PowerShell
  • Right-click the PowerShell symbol
  • Edit --> Paste.
  • See screenshot to the right

 

Method 2 (Best)

  • Prepare to run cmdlets with this PowerShell command:
    set-ExecutionPolicy RemoteSigned
  • Copy the code below into a text file.
  • Save the file with a .ps1 extension, for example network.ps1
  • In PowerShell, navigate to where you saved network.ps1
  • Issue this command:
    .\network 
    (dot backslash filename)

 

Objective 1) - List WMI Objects

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, you need far fewer commands than for a corresponding VBScript.  Indeed, the bare minimum to get started, and display all the WMI object is these 8 letters with a dash in the middle:
gwmi -list     (gwmi is an alias for get-wmiobject)

Note 2: Observe the (|) pipe symbol.  You will use this hundreds of times in PowerShell, what happens is the output of the wmiobject list is pumped into the where clause, which filters the entries for those that contain the word network.  My thinking was gwmi-list produces too many objects.

Note 3: I deliberately don't use many aliases in my scripts, but ft (format-table) is useful for controlling the display of PowerShell's output.  Do try the script with and without the last word, name.

Objective 2) - WMIObject | get-member (Discover which properties to use in our mission)

This script identifies the properties available for the object: Win32_NetworkAdapterConfiguration.  Here below is a classic example of the get-member construction. 

$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration"
$colItems | get-member -membertype Property

Learning Points

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, then you could see what methods are available; substitute 'Method' for the last word, 'Property'.  Alternatively, you could omit the -membertype phrase altogether.

ˆ

Objective 3) - PowerShell: More flexible than Ipconfig

Here is the main script illustrating PowerShell's ability to display information about your TCP/IP properties.

$strComputer = "."
$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
-computername $strComputer | Where{$_.IpEnabled -Match "True"}
foreach ($objItem in $colItems) {
write-host "MACAddress : " $objItem.MACAddress
write-host "IPAddress : " $objItem.IPAddress
write-host "IPAddress : " $objItem.IPEnabled
write-host "DNS Servers : " $objItem.DNSServerSearchOrder
Write-host ""
}

Note 1: The properties that I have chosen are not important.  This is just a case in point; 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 comma!

Note 3: I chose the ForEach construction, to loop though all the network adapters.  In PowerShell in general, and ForEach in particular, the type of brackets are significant; (ellipses for the condition) {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 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 that PowerShell can not only mimic Ipconfig, but exceed its capabilities.  In particular that PowerShell could customise the TCP/IP properties in the output, and also interrogate other machines on the network.

See more Windows PowerShell real life tasks

PowerShell Home  • Real life tasks  • IpConfig  • Exchange  • Com objects  • Services  • Syntax

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-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.