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. Guy Recommends:
The Free IP Address Tracker (IPAT)
| |||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |