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.

Guy Recommends: Tools4ever's UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information on UMRA.

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers.  Download the Free IP Address Tracker

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.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell v 3.0

PowerShell 3.0  • What's New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Home  • PowerShell 3.0 Get-ChildItem  • PowerShell 3 -NotIn  • PowerShell 3.0 Where


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information.  Take the guess work out of which WMI counters to use for scripts.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: