Redaguoti

Bendrinti naudojant


How to Connect to an SMS Provider in Configuration Manager by Using WMI

Before connecting to the SMS Provider for a local or remote Configuration Manager site server, you first need to locate the SMS Provider for the site server. The SMS Provider can be either local or remote to the Configuration Manager site server you're using. The Windows Management Instrumentation (WMI) class SMS_ProviderLocation is present on all Configuration Manager site servers, and one instance will contain the location for the Configuration Manager site server you're using.

You can connect to the SMS Provider on a Configuration Manager site server by using the WMI SWbemLocator object or by using the Windows Script Host GetObject method. Both approaches work equally well on local or remote connections, with the following limitations:

  • You must use SWbemLocator if you need to pass user credentials to a remote computer.

  • You can't use SWbemLocator to explicitly pass user credentials to a local computer.

    There are several different syntaxes that you can use to make the connection, depending on whether the connection is local or remote. After you're connected to the SMS Provider, you'll have an SWbemServices object that you use to access Configuration Manager objects.

Note

If you need to add context qualifiers for the connection, see How to Add a Configuration Manager Context Qualifier by Using WMI.

To connect to an SMS provider

  1. Get a WbemScripting.SWbemLocator object.

  2. Set the authentication level to packet privacy.

  3. Set up a connection to the SMS Provider by using the SWbemLocator object ConnectServer method. Supply credentials only if it's a remote computer.

  4. Using the SMS_ProviderLocation object ProviderForLocalSite property, connect to the SMS Provider for the local computer and receive a SWbemServices object.

  5. Use the SWbemServices object to access provider objects. For more information, see Objects overview.

Examples

The following example connects to the server. It then attempts to connect to the SMS Provider for that server. Typically this will be the same computer. If it isn't, SMS_ProviderLocation provides the correct computer name.

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

Function Connect(server, userName, userPassword)  

    On Error Resume Next  

    Dim net  
    Dim localConnection  
    Dim swbemLocator  
    Dim swbemServices  
    Dim providerLoc  
    Dim location  

    Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")  

    swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy.  

    ' If the server is local, do not supply credentials.  
    Set net = CreateObject("WScript.NetWork")   
    If UCase(net.ComputerName) = UCase(server) Then  
        localConnection = true  
        userName = ""  
        userPassword = ""  
        server = "."  
    End If  

    ' Connect to the server.  
    Set swbemServices= swbemLocator.ConnectServer _  
            (server, "root\sms",userName,userPassword)  
    If Err.Number<>0 Then  
        Wscript.Echo "Couldn't connect: " + Err.Description  
        Connect = null  
        Exit Function  
    End If  

    ' Determine where the provider is and connect.  
    Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")  

        For Each location In providerLoc  
            If location.ProviderForLocalSite = True Then  
                Set swbemServices = swbemLocator.ConnectServer _  
                 (location.Machine, "root\sms\site_" + _  
                    location.SiteCode,userName,userPassword)  
                If Err.Number<>0 Then  
                    Wscript.Echo "Couldn't connect:" + Err.Description  
                    Connect = Null  
                    Exit Function  
                End If  
                Set Connect = swbemServices  
                Exit Function  
            End If  
        Next  
    Set Connect = null ' Failed to connect.  
End Function  

The following sample connects to the remote server using PowerShell, and attempts an SMS connection.

$siteCode = ''
$siteServer = 'server.domain'

$credentials = Get-Credential
$username = $credentials.UserName

# The connector does not understand a PSCredential. The following command will pull your PSCredential password into a string.
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($credentials.Password))

$NameSpace = "root\sms\site_$siteCode"
$SWbemLocator = New-Object -ComObject "WbemScripting.SWbemLocator"
$SWbemLocator.Security_.AuthenticationLevel = 6
$connection = $SWbemLocator.ConnectServer($siteServer,$Namespace,$username,$password)

Compiling the Code

This C# example requires:

Comments

The sample method has the following parameters:

Parameter Type Description
connection - Managed: WqlConnectionManager
- VBScript: SWbemServices
A valid connection to the SMS Provider.
taskSequence - Managed: IResultObject
- VBScript: SWbemObject
A valid task sequence (SMS_TaskSequence).
taskSequenceXML - Managed: String
- VBScript: String
A valid task sequence XML.

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

.NET Framework Security

Using script to pass the user name and password is a security risk and should be avoided where possible.

The preceding example sets the authentication to packet privacy. This is the same managed SMS Provider.

For more information about securing Configuration Manager applications, see Configuration Manager role-based administration.

See Also

SMS Provider fundamentals
How to Add a Configuration Manager Context Qualifier by Using WMI
Windows Management Instrumentation