Guy's Scripting Ezine 141 - PowerShell: More Flexible than IpconfigPowerShell: More Flexible than IpconfigScripting 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 MissionThis 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) 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. Method 1 (Quick)
Method 2 (Best)
Objective 1) - List WMI ObjectsHere is a cmdlet which identifies all the Network WMI objects.
$colItems = get-wmiobject -list | where {$_.name -match "network"} Learning PointsNote 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: 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" Learning PointsNote 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 IpconfigHere is the main script illustrating PowerShell's ability to display information about your TCP/IP properties.
$strComputer = "." 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 IpconfigThe 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.
*
| ||||||