To get data from WMI, either on the local computer or from a remote computer, you must connect to the WMI service by connecting to a specific namespace. In most cases, use either the shorthand moniker connection or the Locator connection. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.
The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.
The following procedure describes how to run a script.
To run a script
Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
Open a command prompt window and navigate to the directory where you saved the file.
Type cscript filename.vbs at the command prompt.
If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).
Note
By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.
The following table lists script examples that can be used to obtain various types of data from the local computer.
How do I...
WMI classes or methods
...connect to a remote computer using WMI?
Specify one of the following as part of your moniker connection string:
A NetBIOS computer name, such as "atl-dc-01"
A fully qualified domain name, such as "atl-dc-01.fabrikam.com"
An IPv4 address, such as "192.168.1.1"
Starting with Windows Vista, you can specify an IPv6 address if the target computer and the computer from which you are making the connection both run IPv6.
strComputer = "atl-dc-01"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process")
For Each objProcess in colProcessList
Wscript.Echo "Process Name: " & objProcess.Name
Next
strComputer = "atl-dc-01"
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer (strComputer, "root\cimv2", "fabrikam\administrator", "password")
Set colProcessList = objSWbemServices.ExecQuery("Select * From Win32_Process")
For Each objProcess in colProcessList
Wscript.Echo "Process Name: " & objProcess.Name
Next
Describes how to find the correct WMI class and procedures to use in scripts and applications that perform common computer and network administration tasks.
WMI tasks for services obtain information about services, including dependent or antecedent services. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.
WMI tasks for networking manage and obtain information about connections and IP or MAC addresses. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.
WMI tasks for the registry create and modify registry keys and values. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.
WMI tasks for computer software obtain information such as which software is installed by the Microsoft Windows Installer (MSI) and software versions. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.
You can use Win32\_Process.Create to execute a script or application on a remote computer. However, for security reasons, the process cannot be interactive. When Win32\_Process.Create is called on the local computer, the process can be interactive.
This module explains the structure of the namespaces that contain classes and also how to query instances of a class. It covers how to query remote computers by using ad-hoc connections and CIM sessions.