Export (0) Print
Expand All

WMI Tasks: Connecting to the WMI Service

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 http://www.microsoft.com/technet.

Remote connections require proper settings for the Windows Firewall and DCOM. For more information, see Connecting to WMI on a Remote Computer and Connecting Through Windows Firewall. Starting with Windows Vista, User Account Control (UAC) can affect WMI access. For more information, see User Account Control and WMI.

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.

Aa394589.wedge(en-us,VS.85).gifTo run a script

  1. Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript scriptfile.vbs at the command prompt.

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 scriptfile.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.

    Windows Server 2003 and Windows XP:  You can use only IPv4 addresses.

For more information, see Connecting to WMI on a Remote Computer and IPv6 and IPv4 Support in WMI.
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

...run a WMI script under alternate credentials?Use the SWbemLocator.ConnectServer method, or IWbemLocator::ConnectServer in C++, and include the appropriate user name and password. You cannot change credentials when connecting to the local computer. For more information, see Creating a WMI Script and Connecting to WMI on a Remote Computer.
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

 

Related topics

WMI Tasks for Scripts and Applications
WMI C++ Application Examples
TechNet ScriptCenter

 

 

Show:
© 2014 Microsoft