vents are added to a log.
Registry Provider. Allows Registry keys to be created, read, and written. WMI events can be generated when specified Registry keys are modified.
Performance Counter Provider. Exposes the raw performance counter information used to compute the performance values shown in the System Monitor tool. Any performance counters installed on a system will automatically be visible through this provider. Only supported on Windows 2000.
Active Directory® Provider. Acts as a gateway to all information stored in the Microsoft Active Directory services. Allows information from both WMI and Active Directory to be accessed using a single API.
Windows Installer Provider. Allows complete control of the Windows Installer and installation of software through WMI. Also supplies information about any application installed with the Windows Installer.
SNMP Provider. Acts as a gateway to systems and devices that use the Simple Network Management Protocol (SNMP) for management. SNMP MIB object variables can be read and written. SNMP traps can be automatically mapped to WMI events.
View Provider. Allows new aggregated classes to be built up from existing classes. Source classes can be filtered for only the information of interest, information from multiple classes can be combined into a single class, and data from multiple machines can be aggregated into a single view.
See the WMI system schema chart at http://www.msdn.microsoft.com/voices/news/wmichart.asp , which shows some of the more important WMI system and device classes that are available in Windows 2000 and the properties, methods, and associations they define. The entire schema for the providers alone just listed is just too large and rich in information to fully publish here, but this chart will be a useful reference tool as you explore what WMI has to offer and can serve as a starting point to learn more about what is available. WMI providers are not just limited to core operating system functionality, however. Numerous providers have been developed both by Microsoft and third parties for other key applications and services that run on Windows platforms. See the sidebar called "How others are making use of WMI" at http://www.msdn.microsoft.com/voices/news/wmisidebar1.asp .
Now that we''''ve defined what WMI is and why you will find it useful in managing your systems, we''''ll take a look at how you can do that with its scripting support. As is usual with scripting on Windows platforms, you have a range of script language choices available to you through the Windows Scripting Host (WSH). These include Microsoft Visual Basic® Scripting Edition (VBScript), Microsoft JScript®, and any script language that supports Microsoft ActiveScripting technology, such as Perl. WMI classes can be just as easily accessed via C++, Visual Basic, Active Server Pages (ASP), and scripts within standard HTML pages.
There are so many things that can be done with WMI instrumentation; there is no way to give a representative example of every major subsystem in this article. Fortunately, once you understand how to access a class, read or write a property, execute a method, or receive an event in one class, it is exactly the same for every other WMI class. To illustrate some of the capabilities of WMI, I have selected several examples, written in VBScript. These examples include:
- Enumerate a list of all installed services and then identify only those services that are stopped but are set to "Automatic" start mode.
- List all the logical disk partitions on my computer and determine if any have less that 20 percent free space left.
- Change the time delay before the default selected operating system is booted.
- Execute a backup on my "Application" event log and then clear the log file.
- Reboot my coworker''''s desktop system. (Don''''t worry, he had to give me security permission to do this and it gives me a chance to talk about security.)
- Launch the Notepad utility through a WMI method.
- Set up an event consumer to watch for any new events that are added to the event log.
- Reconfigure my event consumer script to request an event whenever my CPU utilization exceeds 50 percent.
List All Services on the System
In this example we use the Win32_Service class to output a list of all the services that are installed on the system, regardless of whether they are running:
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_Service") for each Service in ServiceSet WScript.Echo Service.Description Next
Let''''s briefly walk through this example and understand what is happening at each stage. First, we request an object in the GetObject() call from the WMI service (WinMgmt). We do this by submitting a request for all instances of the Win32_Service class through the InstancesOf() method. The result of this call is a list of instances of the Win32_Service class.
After getting the instances back in the ServiceSet variable, all that is left to do is iterate through them and display the property of interest, the Description field, which contains the friendly name of the service. It is important to note here that the Description property is being handled as an automation property of the Service object, allowing for much more intuitive scripting. Alternately, the exact same results can be obtained by using an SQL query and submitting it to the ExecQuery() method:
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}") .ExecQuery("select * from Win32_Service")
The SQL query asks for all instances (and all properties of those instances) to be selected and returned. The resulting set of objects that are returned are exactly equal using this form, but as will be clear in the examples to follow, the query form can be used to refine the number of properties, instances, or both returned to only those of interest. Most importantly, either mechanism can be used for any class supplied through WMI that supports being enumerated.
One final note about this example and those that follow involves the security model being used. You may have seen the statement "{impersonationLevel=impersonate}" as part of the GetObject() call just shown. In essence, this tells the system to use your current login credentials as those to be presented when asking for data or executing methods. It is the same as though you were calling the native APIs directly. This applies for any requests made either on your local machine or a remote machine. In any case, the user the system sees is the one defined by your current login. It is possible, of course, to specify different credentials for requests if desired, but this is outside the scope of this article. Also, on Windows 2000 the "{impersonationLevel=impersonate}" statement can be omitted from the script, as "impersonate" is the default impersonation level configured for COM. It is retained in the samples so that they will work equally well across previous platforms, such as Windows NT 4.0.
List Automatic Services That Are Stopped
If we now want to see just those services that are set to run automatically but for some reason are not running (for example, a user manually stopped one), we really only need to make one change to the query version of the script, as shown in bold here:
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}") .ExecQuery("select * from Win32_Service where State=''''Stopped'''' and StartMode=''''Auto''''") for each Service in ServiceSet WScript.Echo Service.Description next if ServiceSet.Count = 0 Then WScript.echo "No services found meeting query criteria."
Just as if I were requesting more specific information from an SQL database, all that was needed to get a list of services that are stopped, even though they are configured to run automatically at boot time, was to add a WHERE clause to the query. The specified query narrows the results I get to only those instances that have a State property with the value "Stopped" and a StartMode property set to "Auto."
Queries can be very simple or quite complex, depending on how specific a set of information you require. Note that I added one more line to the script at the end to catch the case where no instances that match the criteria were returned, which would almost always be the case for this scenario. In order to see an instance get returned by this script, I recommend temporarily stopping the Print Spooler service on your system and then running the script. Before turning off the Print Spooler, you should get the "no matching instances" message, and after you should get a single instance for the Print Spooler service being turned off. Don''''t forget to restart the Print Spooler service when you are done with the test and check that it disappears from the instances returned by the script.
Listing All Drive Partitions with Less Than 20 Percent Free Space
In order to determine if a drive partition is low on space, two properties from the Win32_LogicalDisk class need to be retrieved: FreeSpace and Size. With these two pieces of information, the percentage of free space can be determined and those disk partitions below t 上一页 [1] [2] [3] [4] 下一页 [系统软件]windows下Apache+php+mysql的安装与配置图解 [办公软件]excel中的VBA中的With语句的使用介绍及实例 [操作系统]在Windows中玩转Linux操作系统 [操作系统]死马还当活马医:6种方法挽救Windows系统 [聊天工具]四大更新 Windows Live Msn 8.1评测 [聊天工具]Windows Live Messenger最新0683版亮相_联络工具_… [聊天工具]Windows Live Mail招人爱的N个理由_联络工具_Wind… [聊天工具]Windows Live Mail Desktop多图欣赏_联络工具_Win… [聊天工具]OE老了 微软开发新邮件客户端取而代之_联络工具 [聊天工具]Windows Live Messenger中文版试用报告(一)__天极…
|