Partager via


How to: Connect to the WMI Service

To use the Windows Management Instrumentation (WMI) Provider, you must first connect to the WMI service on the server. By default, you can use the current user credentials to connect to the service.

After connecting to the service, you can retrieve the object that was created by the WMI service. This object is based on one of the classes in the WMI Provider.

This topic contains examples that illustrate how to connect to the WMI service.

Note

For clarity, the examples in this topic use the default Windows authentication settings for security. Consider the implication of using the default authentication settings before using these scripts in your environment. For more information, see How to: Enable Security in Scripts.

Connecting to a Local Server

The following Microsoft Visual Basic Scripting Edition (VBScript) example shows you how to connect to the WMI service on a local server and retrieve the object based on the MIIS_ManagementAgent Class class. This example uses the current user credentials.

Option Explicit
Dim Service
Dim ManagementAgent
Set Service = GetObject("winmgmts:root\MicrosoftIdentityIntegrationServer")
Set ManagementAgent = Service.Get("MIIS_ManagementAgent.Name='Fabrikam HR MA'")
' * Start your script here.

Connecting to a Remote Server By Using the Current User Credentials

The following VBScript example shows how to connect to a remote server by using the current user credentials.

Option Explicit
Dim Locator
Dim Service
Dim ManagementAgent
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set Service = Locator.ConnectServer("MyServer", "root\MicrosoftIdentityIntegrationServer")
Set ManagementAgent = Service.Get("MIIS_ManagementAgent.Name='Fabrikam HR MA'")
' * Start your script here. 

Replace MyServer with the name of the remote server.

Connecting to a Remote Server by Using Alternative Credentials

The following VBScript example shows how to connect to a remote server by using alternative credentials.

Option Explicit
Dim Locator
Dim Service
Dim ManagementAgent
Set Service = Locator.ConnectServer("MyServer", "root\MicrosoftIdentityIntegrationServer", "Domain\Me", "MyPassword")
Set ManagementAgent = Service.Get("MIIS_ManagementAgent.Name='Fabrikam HR MA'")
' * Start your script here.

Replace the following:

  • MyServer with the name of the remote server.

  • Domain\Me with the name of the alternative user.

  • MyPassword with the password of the alternative user.

Because this script contains a user name and password, store the script in a secure folder that has the same security level as the FIM Synchronization Service folder. Limit access to this folder.

Note

We do not recommend storing user credentials in a script. If you have to run a script under a different user context, we recommend that you use the runas command to run the script. For more information, see How to: Create Scripts to Run Management Agents.

Note

Do not use this script to connect to a local server. If you specify the user name and password when you connect to a local server, the connection could fail. For more information, see the strUser parameter in the SWbemLocator.ConnectServer topic in the Windows Management Instrumentation SDK.

Remarks

You can use the Synchronization Service Manager to generate a VBScript script that can run a management agent with a specified run profile. For more information, see "Create a Script for a Management Agent Run Profile" in the Microsoft Forefront Identity Management help on TechNet. For more information about WMI, see the following topics in the Platform SDK:

See Also

Concepts

Creating Scripts
Using the WMI Provider
WMI Provider Overview
How to: Enable Security in Scripts
How to: Create Scripts to Run Management Agents