How to Connect to an SMS Provider in Configuration Manager by Using Managed Code

To connect to a SMS Provider, use WqlConnectionManager.Connect. After it is connected, WqlConnectionManager.Connect has methods to query, create, delete, and otherwise use Configuration Manager Windows Management Instrumentation (WMI) objects.


WqlConnectionManager.Connect is a WMI-specific derivation of ConnectionManagerBase.

If you are connecting to a local SMS Provider, you do not supply user credentials. If you are connecting to a remote SMS Provider, you do not need to supply user credentials if the if the current user/computer context has permissions on the remote SMS Provider.

If you do not have access privileges on the remote SMS Provider, or if you want to use a different user account, then you must supply user credentials for a user account that has access privileges.

WQLConnectionManager.Connection requires a SmsNamedValuesDictionary object. This can be used to store cached information such as the computer name.

It is pre-populated with a number of values that can be used in your application.

Value Description.
ProviderLocation The provider location. For example,

ProviderMachineName The provider computer. For example, \\ComputerName.
Connection The connection path. For example, \\ComputerName\root\sms\site_XXX.
ConnectedSiteCode The site code for the Configuration Manager site that the connection is connected to. For example, XXX.
ServerName The computer name, for example, COMPUTERNAME.
SiteName The Configuration Manager site code. For example, Central Site.
ConnectedServerVersion Ther version for the connected server. For example, 4.00.5830.0000
BuildNumber The Configuration Manager installation build number. For example, 5830.


The SmsNamedValuesDictionary object is not the context qualifier information passed to the provider. For more information, see How to Add a Configuration Manager Context Qualifier by Using Managed Code.

To connect to the SMS Provider

  1. Create a SmsNamedValuesDictionaryObject.

  2. Create an instance of the WqlConnectionManager class and call the [Connect] method passing the server name, and if the server name is remote, the user name and password.

  3. Use the WqlConnectionManager object to connect to the provider.


The following example method connects to the SMS Provider on a local or remote computer. If servername is remote, the method uses the supplied user name and password to connect to the remote computer. If you want to use the current user context, for the remote connection, change the code so that it does not pass the user name and password. If the connection is successful, a WqlConnectionManager object is returned.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

public WqlConnectionManager Connect(string serverName, string userName, string userPassword)  
        SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();  
        WqlConnectionManager connection = new WqlConnectionManager(namedValues);  

        if (System.Net.Dns.GetHostName().ToUpper() == serverName.ToUpper())  
            // Connect to local computer.  
            // Connect to remote computer.  
            connection.Connect(serverName, userName, userPassword);  

        return connection;  
    catch (SmsException e)  
        Console.WriteLine("Failed to Connect. Error: " + e.Message);  
        return null;  
    catch (UnauthorizedAccessException e)  
        Console.WriteLine("Failed to authenticate. Error:" + e.Message);  
        return null;  

Compiling the Code












Robust Programming

The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.

.NET Framework Security

UnauthorizedAccessException is raised when the wrong credentials are passed to WqlConnectionManager.Connect.

See Also

SMS Provider fundamentals How to Add a Configuration Manager Context Qualifier Using Managed Code
Objects overview