在連線到本機或遠端 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 提供者之後,您會有用來存取 Configuration Manager 物件的 SWbemServices 物件。
注意事項
如果您需要為連線新增內容限定符,請參閱如何使用WMI 新增 Configuration Manager 內容限定符。
線上到SMS提供者
取得 WbemScripting.SWbemLocator 物件。
將驗證層級設定為封包隱私權。
使用 SWbemLocator 物件 ConnectServer 方法來設定與 SMS 提供者的連線。 只有在遠端電腦時才提供認證。
使用 SMS_ProviderLocation 物件 ProviderForLocalSite 屬性,連接到本機計算機的 SMS 提供者,並接收 SWbemServices 物件。
使用 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 連線到遠端伺服器,並嘗試 SMS 連線。
$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
範例方法具有下列參數:
| 參數 | Type | 描述 |
|---|---|---|
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 提供者基本概念如何使用 WMIWindows Management Instrumentation 新增 Configuration Manager 內容限定符