Home > Networking Tips > Network Management > Return-all-values script: Managing Windows networks using scripts, Part 13
Networking Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

NETWORK MANAGEMENT

Return-all-values script: Managing Windows networks using scripts, Part 13


Mitch Tulloch
11.18.2008
Rating: --- (out of 5)


Network management news, advice and technical information
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


This article originally appeared on WindowsNetworking.com.
You can manage your Windows Networks using scripts. In this tip, learn how to modify a workhorse or "return all values" script to list the names and values of any property of a Windows Management Instrumentation (WMI) class. This tip originally appeared on WindowsNetworking.com.

In the previous article of this series, Properties of Windows Management Instrumentation, we came up with a script called DisplayClassProperties.vbs that displays the names of all the properties of a WMI class. Here's what this script looks like, using Win32_BootConfiguration as the class we're connecting to in our WMI moniker:

Option Explicit
On Error Resume Next
Dim strComputer
Dim strWMINamespace
Dim strWMIQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
strWMINamespace = "\root\CIMV2"
strWMIQuery = ":Win32_BootConfiguration"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery)
WScript.Echo "Number of properties of " & strWMIQuery & " class is " & objWMIService.Properties_.count

For Each objItem in objWMIService.Properties_
    Wscript.Echo "Property: " & objItem.name
Next

When I run this script (using local admin credentials!) on my Windows XP workstation (with Cscript.exe pre-configured as the default Windows script host) I get the following result:

C:\scripts>DisplayClassProperties.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Number of properties of :Win32_BootConfiguration class is 9
Property: BootDirectory
Property: Caption
Property: ConfigurationPath
Property: Description
Property: LastDrive
Property: Name
Property: ScratchDirectory
Property: SettingID
Property: TempDirectory

I also mentioned in the last article that this script could easily be customized to display the names of properties of any WMI class. For example, say we wanted to display all the names of all the properties in the Win32_DiskPartition class. To do this, all we need to do is take the following line:

strWMIQuery = ":Win32_BootConfiguration"

…and change it to this:

strWMIQuery = ":Win32_DiskPartition"

Now when we run our script again, we get the following result:

C:\scripts>DisplayClassProperties.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Number of properties of :Win32_DiskPartition class is 34
Read other 'Managing Windows networks using scripts' tips
Part 1: The basics

Part 2: Cleaning up

Part 3: Understanding WMI

Part 4: Using Win32_NetworkAdapterConfiguration

Part 5: Getting over the hump

Part 6: Remote scripting first steps

Part 7: Troubleshooting the mystery error

Part 8: Troubleshooting remote scripting with Network Monitor 3.0

Part 9: Understand remote scripting

Part 10: Two tricks using WMI scripts

Part 11: More remote scripting tips

Part 12: Properties of WMI class

Part 13: A return-all-values script

Part 14: WMI scripting resources
Property: Access
Property: Availability
Property: BlockSize
Property: Bootable
Property: BootPartition
Property: Caption
Property: ConfigManagerErrorCode
Property: ConfigManagerUserConfig
Property: CreationClassName
Property: Description
Property: DeviceID
Property: DiskIndex
Property: ErrorCleared
Property: ErrorDescription
Property: ErrorMethodology
Property: HiddenSectors
Property: Index
Property: InstallDate
Property: LastErrorCode
Property: Name
Property: NumberOfBlocks
Property: PNPDeviceID
Property: PowerManagementCapabilities
Property: PowerManagementSupported
Property: PrimaryPartition
Property: Purpose
Property: RewritePartition
Property: Size
Property: StartingOffset
Property: Status
Property: StatusInfo
Property: SystemCreationClassName
Property: SystemName
Property: Type

Display the values of each property
Now at this point you may be saying, "So what? All this script does is display the name of each property of a class. What about displaying the value of each property?" Well, that's a good point!

Let's see if we can modify the script (we're back to using Win32_BootConfiguration as our class) so that the script will list not only the names of all the properties but also their values. To do this, let's try changing this line:

Wscript.Echo "Property: " & objItem.name

…to this:

Wscript.Echo "Property: " & objItem.name & vbTab & "Value: " & objItem.value

Here's what we get when we now run the script:

C:\scripts>DisplayClassProperties.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Number of properties of :Win32_BootConfiguration class is 9
Property: BootDirectory Value:
Property: Caption       Value:
Property: ConfigurationPath     Value:
Property: Description   Value:
Property: LastDrive     Value:
Property: Name  Value:
Property: ScratchDirectory      Value:
Property: SettingID     Value:
Property: TempDirectory Value:

Hmm, all the values are null, i.e., blank. Why? To see what's going on. Look at this line:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery)

Plugging in the values of each variable, we could rewrite this line as follows:

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2:Win32_BootConfiguration")

Note that we're connecting to a specific WMI class (Win32_BootConfiguration) in our WMI moniker string so that we can return a collection containing all properties of this class. Then we want to display the name and value of each property. But the values are returned as NULL because we haven't connected to a specific instance of this class. The WMI Glossary says that an instance is "a representation of a real-world managed object that belongs to a specific class" and that "instances contain actual data" and actual data is what we want. So how do we connect to an instance of a class?

To connect to an instance of a class, you need to specify a particular instance by using the key property of the class. Again consulting the WMI Glossary, we see that the key property is "a property that provides a unique identifier for an instance of a class" and that "key properties are marked with the Key qualifier" in MSDN documentation. Let's look at the Win32_BootConfiguration class on MSDN to learn what the key property is for this class. Figure 1 shows the portion of this page that identifies the key property for this class:

Key property for Win32_BootConfiguring class
Figure 1: Key property for Win32_BootConfiguring class

From this MSDN page, we can see that the key property for the Win32_BootConfiguration class is Name. This means that we need to specify a value for this property in our WMI moniker string if we want to connect to a specific instance of this class to retrieve the values of each property for the class. In other words, all we need to do is change this line:

strWMIQuery = ":Win32_BootConfiguration"

…to this:

strWMIQuery = ":Win32_BootConfiguration.Name='SOMETHING'"

...where 'SOMETHING' is the value of the Name property of specific instance of the class.

So how can we find out the value of the key property of a specific instance of this class? One way is to use the Windows Management Instrumentation Tester (wbemtest.exe). Start by typing wbemtest at a command prompt. This opens the following window:

The Windows Management Instrumentation Tester
Figure 2: The Windows Management Instrumentation Tester

Click the Connect button and connect to the root\cimv2 namespace:

Connecting to the Win32_BootConfiguration class
Figure 3: Connecting to the Win32_BootConfiguration class

Click Connect to return to the main window where all the buttons now show as available:

Connected to the class
Figure 4: Connected to the class

Now click the Enum Instances button and type the class name so you can display all the instances of the class:

Displaying the instances of the class
Figure 5: Displaying the instances of the class

Finally, click OK to show all the instances of the class as enumerated by their key property (Name):

Instances of Win32_BootConfiguration
Figure 6: Instances of Win32_BootConfiguration

After all that, it turns out that there is only one instance of this class on our machine and the Name property of this instance has the value "BootConfiguration"! So this means that in order to display the values of all properties of the instance of the Win32_BootConfiguration class on our machine, all we need to do is change this line:

strWMIQuery = ":Win32_BootConfiguration"

...to this:

strWMIQuery = ":Win32_BootConfiguration.Name='BootConfiguration'"

In other words, our revised DisplayClassProperties.vbs script now looks like this:

Option Explicit
On Error Resume Next
Dim strComputer
Dim strWMINamespace
Dim strWMIQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
strWMINamespace = "\root\CIMV2"
strWMIQuery = ":Win32_BootConfiguration.Name='BootConfiguration'"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery)
WScript.Echo "Number of properties of " & strWMIQuery & " class is " & objWMIService.Properties_.count

For Each objItem in objWMIService.Properties_
   Wscript.Echo "Property: " & objItem.name & vbTab & "Value: " & objItem.value
Next

And now, when we run this script it displays not just the names of all the properties but also their values:

C:\scripts>DisplayClassProperties.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Number of properties of :Win32_BootConfiguration.Name='BootConfiguration' class
is 9
Property: BootDirectory Value: \WINDOWS
Property: Caption       Value: \Device\Harddisk0\Partition1
Property: ConfigurationPath     Value: \WINDOWS
Property: Description   Value: \Device\Harddisk0\Partition1
Property: LastDrive     Value: C:
Property: Name  Value: BootConfiguration
Property: ScratchDirectory      Value: C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp
Property: SettingID     Value:
Property: TempDirectory Value: C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp

Putting this info into table form makes it easier to read:

BootDirectory

\WINDOWS

Caption

\Device\Harddisk0\Partition1

ConfigurationPath

\WINDOWS

Description

\Device\Harddisk0\Partition1

LastDrive

C:

Name

BootConfiguration

ScratchDirectory

C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp

SettingID

Value:

TempDirectory

C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp

Conclusion
We can see that this simple "return-all-values" script has given us some useful information about our machine! Now here's an exercise you can try on your own: Instead of connecting an instance of the Win32_BootConfiguration class (there's only one instance of this class), try connecting to an instance of the Win32_DiskPartition class (which has several instances if your machine has more than one partition). To do this, first use wbemtest to display the instances of this class (and to learn the key property that distinguishes these instances) and then modify the DisplayClassProperties.vbs script so it displays the properties and values of the specified instance of this class (i.e., of the disk partition you specify). Good luck!

About the author:
Mitch Tulloch is a writer, trainer and consultant specializing in Windows server operating systems, IIS administration, network troubleshooting, and security. He is the author of 15 books including the Microsoft Encyclopedia of Networking (Microsoft Press), the Microsoft Encyclopedia of Security (Microsoft Press), Windows Server Hacks (O'Reilly), Windows Server 2003 in a Nutshell (O'Reilly), Windows 2000 Administration in a Nutshell (O'Reilly), and IIS 6 Administration (Osborne/McGraw-Hill). Mitch is based in Winnipeg, Canada, and you can find more information about his books at his website: www.mtit.com.

WindowsNetworking.com contains a wealth of networking information for administrators: Featuring information on how to setup and troubleshoot various networks of any size. Also includes a comprehensive archive of hundreds of reviewed networking software and hardware solutions. Frequently updated with articles and tips by a team of leading authors, it remains a favorite within the networking community.


Rate this Tip
To rate tips, you must be a member of SearchNetworking.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
Network Management
Green enterprise: Three networking investments that make a difference
Distributed network management means no more hard NOCs
Green data center networks: Smarter architecture, not expensive devices
Internal cloud computing on the cheap: Free automated provisioning?
With virtual OS and virtual applications, who needs virtual machines?
Application switch testing: An easy RFP guide
Virtualization: The next generation of application delivery challenges
Improving the performance of Web traffic and application delivery
The link between network management and application delivery
How to align network usage information to business processes

Network Monitoring
Meru reinvents wireless LAN troubleshooting and management
Green enterprise: Three networking investments that make a difference
Network device management overload: Engineers managing too many boxes
What preventative maintenance procedures for network devices exist?
WLAN QoS and SLA monitoring with 7/24 Wireless Quality Assurance costs
How important are network infrastructure maps for engineers or admins?
Understand Windows tracert output to troubleshoot network connectivity
Network management and monitoring market remains crowded, fragmented
When do applications suffer from poor network performance?
Xangati help desk 'DVR' feature speeds up trouble ticketing resolution
Network Monitoring Research

Working With Servers and Desktops
What network loss testing tools/methods calculate dropped packets from a PC?
Do I have to disable DHCP on my router to create a DHCP server?
How can I replicate the services of Active Directory (AD) in ADC?
Top 10 reasons why computers do not have network access to each other
Troubleshooting -- 'Network Know-How' Chapter 17
Windows Server 2008 IP routing configuration: Static and dynamic RIPv2
Understand Windows tracert output to troubleshoot network connectivity
Test your TCP/IP protocol stack to troubleshoot network connectivity
Checking IP configuration to troubleshoot Windows network connectivity
Physical network security key to fighting low-tech threats

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
10-high-day busy period  (SearchNetworking.com)
ACK  (SearchNetworking.com)
baseboard management controller  (SearchNetworking.com)
call failure rate  (SearchNetworking.com)
jam  (SearchNetworking.com)
Jini  (SearchNetworking.com)
maximum segment size  (SearchNetworking.com)
maximum transmission unit  (SearchNetworking.com)
netstat  (SearchNetworking.com)
network tracking tool  (SearchNetworking.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Networking Solutions for Business

Alcatel-Lucent Network Business Communications Solutions

About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts