PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 110 - PowerShell WMI

Guy's Scripting Ezine 110 - PowerShell WMI

This Week's Secret

This week Microsoft announced that Monad had metamorphosized into PowerShell.  (My friend 'Mad' Mick unfairly calls it PowersHell.)  One of the hazards of being a Beta pioneer is having to keep updating to the latest version of the program.  What I did was download PowerShell, uninstall Monad and install the new PowerShell.  The crucial difference is that PowerShell files require a change of extension from .msh to .ps1.  This new extension only affects scripts, called cmdlets, which we will investigate next week.

This Week's Mission

With any new system you get hype.  In the case of PowerShell much of the talk is about what it may (or may not) do in Longhorn.  What I want to do is give you a killer reason to use PowerShell, now in 2006, with Windows 2003 (or XP).  That killer reason is to create WMI scripts.  I urge you to switch to PowerShell rather than use VBScript for accessing WMI.  If you have never explored WMI, then it's like an Aladdin's cave, so start now with PowerShell.  Here is an example, which discovers your computer's name, model, manufactures and memory, type:

get-WmiObject win32_computersystem

If there is another machine on your subnet try these two commands:
help WmiObject  (I mention help for you to see how I knew it was -computername)
get-WmiObject -computername  othermachine win32_computersystem

Calling for Windows Management Instrumentation (WMI) is rather like switching on a microscope to observe what the operating system is doing.  WMI permits you to discover what the operating system knows about disks, memory processor or any other Win32 component.  My prediction is not the end of the road for WMI, far from it, what I foresee is using PowerShell commands rather than VBScript to access the WMI classes.


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


Guy's Twin Goals for PowerShell and WMI

1) To explore WMI's Win32_* classes type; get-WmiObject.  If at least one of these examples does not give you an idea for a production PowerShell scripts then I will be amazed.

2) I want to introduce you to a few useful PowerShell commands that you learn once then apply to a wide range of verb-noun pairs. For example, format-table, -autosize and -list.

Assumptions and Preparation

You download PowerShell (or Monad) and .NET Framework 2.0.  Next, you install the .msi file, finally you run powershell (or for Monad, MSH).  Now you are at a black screen with a PS Prompt >

Note:  When you type on the PowerShell command line, the only tricky symbol is this pipe |.  If in doubt activate your keyboard's number lock, hold down the alt key, type 124 and then let go of alt.  If necessary, investigate which key on your keyboard corresponds to this pipe symbol (|). 

Here are commands to try at the PS Prompt >

i a) get-WmiObject Win32_NetworkAdapterConfiguration

i b) get-WmiObject Win32_NetworkAdapterConfiguration | format-table

i c) get-WmiObject Win32_NetworkAdapterConfiguration | format-table Ipaddress, Description, DHCPEnabled -autosize

ii a) In many ways we should have started by researching a list of properties, try:
get-WmiObject Win32_NetworkAdapterConfiguration | get-member

ii b) Now amend this command choosing different properties:
get-WmiObject Win32_NetworkAdapterConfiguration | format-table IPAddress, Description, DHCPEnabled -autosize   For example: DefaultIPGateway.

iii a) get-WmiObject Win32_product

iii b) get-WmiObject Win32_product |sort-object vendor |format-table name, vendor -groupby vendor

iv) get-WmiObject Win32_LogicalDisk | format-table

v) get-WmiObject Win32_environment

vi a) Research more objects with: get-WmiObject -list

vi b) Refine your search with: get-WmiObject -list | where {$_.name -like "Win32_n*"}

vi c) Try Win32 objects beginning with other letters, for example, substitute 'p' "Win32_p*.

vii) As you make your selections try appending at least some of these,
| format-table name, others
| format-table name, others, -autosize
-groupby
| sort-object
| where {$_.name -like "Win32_n*"}  Note the braces type brackets.
Also remember; WmiObject Win32_xyz  | get-member

ˆ

Mad Mick and Mapnetworkdrive.

While 'Mad' Mick was rude about PowerShell, he is a genius when it comes to scripting.  His first question to me was 'Guy can PowersHell map network drives'.  I tried for half an hour then said, 'It does not seem possible Mick'.  Then he bet me a curry that in ten minutes he could get it to map a local drive letter to a UNC path.  I lost the bet, but learned a valuable lesson.

Before we try the code that Mick produced, please carry out a 'before' experiment, simply type:
get-psdrive  (Note the singular noun, also note 'PSdrive', it was plain 'drive' in monad).  One more 'before' experiment try
get-WmiObject win32_networkconnection

The main event.  Here is the code Mick created, please alter the UNC path otherwise it will not work.  Also, please place the commands on two lines with a carriage return, no worries if you just copy and paste!

$net = $(New-Object -Com WScript.Network)
$net.MapNetworkDrive("s:", "\\grand\ezine")

If you get an error, read the message!  Check the UNC path.  If it works confirm with
get-WmiObject win32_networkconnection.

Summary of PowerShell and WMI

I have two goals for PowerShell, my main goal is to show you what PowerShell can do for you here and now with WMI.  My secondary goal is to introduce useful general PowerShell commands, for example, append instructions with a | (pipe) then format-table,-list or -groupby.

See more Microsoft PowerShell examples of WMI

PowerShell Home   • WMI   • Service   • WMI File   • WMI Query   • DNS   • Disk

Classes   • Network Adapter   • PowerShell WMI examples

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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


^


Google
Web  This website

Solarwinds IpMonitorIs Your Server Running Slowly?

Check with SolarWinds ipMonitor

Analyze your network with ipMonitor.  Get a free evaluation copy, and monitor the performance of the servers on your network.
Free Download of SolarWinds ipMonitor