WMI (Windows Management Instrumentation) - Secrets

来源:互联网 发布:软件试用情况总结报告 编辑:程序博客网 时间:2024/05/01 10:14

http://www.computerperformance.co.uk/vbscript/wmi_secrets.htm

http://www.computerperformance.co.uk/vbscript/wmi_technique.htm

Introduction to WMI Secrets

Here are my tips on mastering WMI.  Start with a simple script.  My advice is always aim to build on success.  The knack of scripting is to divide your task into sections, in our case, pure VBScript, WMI and CIMv2.  Get each command working then bolt the sections to make your production WMI script.

Topics for WMI Secrets

  • Getting started with WMI
  • Example 1: To Echo the Name Set by the strComputer Variable
  • VBScript Learning Points
  • Example 2 - WMI Script to Obtain Information about the Operating System
  • WMI Tutorial Learning Points
  • Summary of WMI Secrets

Getting started with WMI

We have a simple script to get started, our mission is to master basic VBScript commands.  Following the plan I outlined early, let us break the script down into sections.  Firstly the pure VBScript, secondly to add in the WMI components.

Example 1a: To Echo the Name Set by the strComputer Variable

This is a very basic script, which fits with my aim to start simply.

Prerequisites

This script is suitable for most clients, XP, W2K, even NT 4.0 and Win 9x.

Instructions for Creating your WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide the name of the machine on line 8.
  3. Save the file with a .vbs extension, for example: Simple1.vbs.
  4. Double click Simple1.vbs and check the UserName.

Script to Demonstrate the Basics of VBScript.

 

' Simple1.vbs
' Sample VBScript to display the ComputerName
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.5 - November 2005
' --------------------------------------------------'
Option Explicit
Dim strComputer
strComputer = "LocalHost"
WScript.Echo "Computer: " _
& strComputer
WScript.Quit
' End of VBScript example.

VBScript Learning Points in Preparation for WMI

1) This script deliberately has no WMI commands.  In Example 1, I just want to focus on the minimal VBScript commands which provide the wrappers for the WMI section.  WMI itself, is added in Example 2.

2) WScript is a built in executable for Windows 2000 and later machines.  Although WScript operates through 20 or more methods, in this example we employ just 2 WScript methods; the .Echo and .Quit.

3) Even though this is a short script, I still like to create a header.  In this top section, or header, I set out the purpose and then declare the one variable with:
Dim strComputer

4) Although not strictly necessary, this script introduces the underscore (_) in line 11.  My message is learn this basic VBScript syntax in a simple script, then apply the syntax smoothly to the production versions.  Underscore says to VBScript, 'here is where we word-wrap'.  Almost every WMI script requires an _.   You could just accept that without the underscore you get an error.  Better still you could appreciate that where commands are too long to fit on one line, we need a marker to tell VBScript to carryon reading the command on the next line.

5) To be honest, this first script does not do much, but it gives a grounding in VBScript.  My idea was to prepare you for the WMI in Example 2.  If you were slightly disappointed with the output, I have a more advanced script to echo the machine's actual hostname.

Example 1b: To Discover the ComputerName

While this script is more satisfying, Example 1b is a digression on our main task of mastering WMI.

 

' ComputerName.vbs
' Sample VBScript to display the ComputerName
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - November 2005
' ----------------------------------------------------'
Option Explicit
Dim strComputer, objNetwork

Set objNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Computer = " & objNetwork.ComputerName
WScript.Quit

' End of VBScript example.

 

Example 2 - WMI Script to Obtain Information about the Operating System

This script not only demonstrates the basics of WMI, but it also has a practical goal of querying a machine for information about the operating system.

Script to Query the Computer for Operating System Details

 

' OperatingSystem.vbs
' Purpose VBScript to document your Operating System
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.4 - November 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems
Dim strComputer, strList

On Error Resume Next
strComputer = "."

' WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts://" _
& strComputer & "/root/cimv2")

' WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

' For Each... In Loop (Next at the very end)
For Each objItem in colItems
WScript.Echo "Machine Name: " & objItem.CSName & VbCr & _
"===================================" & vbCr & _
"Processor: " & objItem.Description & VbCr & _
"Manufacturer: " & objItem.Manufacturer & VbCr & _
"Operating System: " & objItem.Caption & VbCr & _
"Version: " & objItem.Version & VbCr & _
"Service Pack: " & objItem.CSDVersion & VbCr & _
"CodeSet: " & objItem.CodeSet & VbCr & _
"CountryCode: " & objItem.CountryCode & VbCr & _
"OSLanguage: " & objItem.OSLanguage & VbCr & _
"CurrentTimeZone: " & objItem.CurrentTimeZone & VbCr & _
"Locale: " & objItem.Locale & VbCr & _
"SerialNumber: " & objItem.SerialNumber & VbCr & _
"SystemDrive: " & objItem.SystemDrive & VbCr & _
"WindowsDirectory: " & objItem.WindowsDirectory & VbCr & _
""
Next
WSCript.Quit

' End of WMI Win32_OperatingSystem VBScript

WMI Tutorial Learning Points

1) Let us concentrate on the WMI commands on lines 13-19.

From example 1a we learnt that the _ (underscore) is solely for word-wrap.  If there is room on the line, you could edit to:
Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")

Next, I would like to dissect the above command into 4 parts.

a) The goal of this command is to get a handle on the CIM namespace /root/cimv2. 

b) GetObject("winmgmts: means call for the winmgmts service to connect to the CIM namespace.

c) strComputer is the variable that we declared earlier.  We could experiment and set strComputer = "othermachine" on line 11.

d) Set objWMIService creates a new variable, or place holder so that we can reuse the /root/cimv2 object later in the script.

2) Set colItems = objWMIService.ExecQuery
("Select * from Win32_OperatingSystem")

a) If you remember we just created the variable objWMIService.  Now we are going to put it the ExecQuery method use and execute a WQL command.

b) ("Select * from Win32_OperatingSystem") is a classic database SQL or WQL command.  The key feature is Win32_OperatingSystem.  As you get to know WMI scripts, so you will use many different Win32_objects, for example Win32_Process, Win32_PhysicalDisk.  Well, in this instance it is the operating system that we are interested in.

c) See how all the above information is transferred to a new variable colItems.

3) Here is the section where WMI commands link to VBScript commands.  VBScript provides the loop with.  'For Each ... In.....Next', and WMI provides the objects to interrogate with objItem and colItems.

4) Each line (apart from the last) ends with & VbCr & _.  What this VBScript command does is tell the script that there are more instructions on the next line, the _ really comes into its own here.  The vbCr provides a line break in the output, not in the script.  See in the next section tips on how to add & VbCr & _  to your scripts.

Bonus Technique - Make the script easier to read.

Summary of WMI Secrets

The biggest problem that people have when they write to me is that their scripts are too complex.  Learn from this and create short scripts.  Even just write snippets.  Separate out the pure WMI from the VBScript wrapper, get each part working, then copy and paste into the final script.