如何使用 WMI 连接到 Configuration Manager 中的短信提供程序

在连接到本地或远程Configuration Manager站点服务器的 SMS 提供程序之前,首先需要找到站点服务器的 SMS 提供程序。 SMS 提供程序可以是使用Configuration Manager站点服务器的本地或远程。 Windows Management Instrumentation (WMI) 类SMS_ProviderLocation存在于所有Configuration Manager站点服务器上,一个实例将包含你正在使用Configuration Manager站点服务器的位置。

可以使用 WMI SWbemLocator 对象或使用 Windows 脚本主机GetObject方法连接到Configuration Manager站点服务器上的 SMS 提供程序。 这两种方法在本地或远程连接上效果同样出色,但存在以下限制:

  • 如果需要将用户凭据传递给远程计算机,则必须使用 SWbemLocator

  • 不能使用 SWbemLocator 将用户凭据显式传递到本地计算机。

    可以使用多种不同的语法进行连接,具体取决于连接是本地连接还是远程连接。 连接到 SMS 提供程序后,你将拥有一个 SWbemServices 对象,用于访问Configuration Manager对象。

注意

如果需要为连接添加上下文限定符,请参阅如何使用 WMI 添加Configuration Manager上下文限定符

连接到 SMS 提供程序

  1. 获取 WbemScripting.SWbemLocator 对象。

  2. 将身份验证级别设置为数据包隐私。

  3. 使用 SWbemLocator 对象 ConnectServer 方法设置与 SMS 提供程序的连接。 仅当它是远程计算机时才提供凭据。

  4. 使用 SMS_ProviderLocation 对象 ProviderForLocalSite 属性,连接到本地计算机的 SMS 提供程序并接收 SWbemServices 对象

  5. 使用 SWbemServices 对象访问提供程序对象。 有关详细信息,请参阅 对象概述

示例

以下示例连接到服务器。 然后,它会尝试连接到该服务器的 SMS 提供程序。 通常,这与同一台计算机相同。 否则, SMS_ProviderLocation 提供正确的计算机名称。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段

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

以下示例使用 PowerShell 连接到远程服务器,并尝试短信连接。

$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)

编译代码

此 C# 示例需要:

Comments

示例方法具有以下参数:

参数 类型 说明
connection -管理: WqlConnectionManager
- VBScript: SWbemServices
与 SMS 提供程序的有效连接。
taskSequence -管理: IResultObject
- VBScript: SWbemObject
有效的任务序列 (SMS_TaskSequence) 。
taskSequenceXML -管理: String
- VBScript: String
有效的任务序列 XML。

可靠编程

有关错误处理的详细信息,请参阅关于Configuration Manager错误

.NET Framework 安全性

使用脚本传递用户名和密码存在安全风险,应尽可能避免。

前面的示例将身份验证设置为数据包隐私。 这是相同的托管 SMS 提供程序。

有关保护Configuration Manager应用程序的详细信息,请参阅Configuration Manager基于角色的管理

另请参阅

SMS 提供程序基础知识如何使用 WMI Windows Management Instrumentation添加Configuration Manager上下文限定符