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
- Copy and paste the example script below into notepad or a VBScript editor.
- Decide the name of the machine on line 8.
- Save the file with a .vbs extension, for example: Simple1.vbs.
- 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.
- WMI (Windows Management Instrumentation) - Secrets
- Windows Management Instrumentation (WMI)
- Windows Management Instrumentation(WMI)
- WMI 编程 (Windows Management Instrumentation)
- Windows Management Instrumentation(WMI) 之 Win32 类
- Windows Management Instrumentation(WMI) 之 Win32 类
- Windows Management Instrumentation(WMI) 之 Win32 类
- Developing WMI Solutions: A Guide to Windows Management Instrumentation
- Windows Management Instrumentation (WMI)信息可能损坏错误修复
- 安装SQL Server2014 :规则”Windows Management Instrumentation (WMI)服务 “失败
- 解决【Windows Management Instrumentation(WMI)信息可能损坏】
- Windows Management Instrumentation
- 8. Windows Management Instrumentation
- WMI003-WMI学习笔记(三)——Windows Management Instrumentation(Windows管理工具)
- windows不能显示此连接属性。windows management instrumentation (WMI) 信息可能损坏
- Windows Management Instrumentation无法启动
- Windows Management Instrumentation (Windows 管理规范)
- Windows Management Instrumentation 命令行(MSDN)(WMIC)
- ASP中FSO组件的属性和方法(六)-
- IE 和 Firefox 都兼容的文字滚动效果
- OLE程序开发利用(开发EXCEL) 之二
- FileSystemObject对象的属性及说明
- Spring+Hibernate+Struts
- WMI (Windows Management Instrumentation) - Secrets
- JSON概括
- OLE程序开发利用(开发EXCEL) 之三
- 用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)
- 开发保留标准浏览器功能的AJAX应用程序
- 程序员中有多少是党员
- 一个 超 超 超经典的短篇小说
- xp下IIS连接数限制修改方法
- 开张大吉