如何在 Configuration Manager 中建立計算機變數
您可以為執行 Configuration Manager 的電腦建立計算機變數,方法是將 SMS_MachineVariable 的實例新增至 SMS_MachineSettings 類別 MachineVariables
數位屬性。
若要建立計算機變數
設定與SMS提供者的連線。 如需詳細資訊,請 參閱SMS提供者基本概念。
取得的實
SMS_MachineSettings
例。針對要加入的每個變數,將內嵌物件
SMS_MachineVariable
的實例新增至MachineVariables
數位屬性。將變更認可至類別實
SMS_MachineSettings
例。
範例
下列範例方法會建立集合變數,並將其新增至所提供標識碼所識別的集合。
在此範例中 LocaleID
, 屬性會硬式編碼為英文 (美國 ) 。 如果您需要非美國地區設定安裝時,您可以從 SMS_Identification Server WMI ClassLocaleID
屬性取得它。
如需呼叫範例程式代碼的相關信息,請 參閱呼叫 Configuration Manager 代碼段。
Sub CreateComputerVariable(connection, siteCode, name, value, mask, computerId)
Dim computerSettings
Dim computerVariables
Dim computerVariable
Dim Settings
' See if the computer settings object already exists. if it does not, create it.
Set settings = connection.ExecQuery _
("Select * From SMS_MachineSettings Where ResourceID = '" & computerID & "'")
If settings.Count = 0 Then
Wscript.Echo "Creating computer settings object"
Set computerSettings = connection.Get("SMS_MachineSettings").SpawnInstance_
computerSettings.ResourceID = computerId
computerSettings.SourceSite = siteCode
computerSettings.LocaleID = 1033
computerSettings.Put_
End If
' Get the computer settings object.
Set computerSettings = connection.Get("SMS_MachineSettings.ResourceID='" & computerId &"'" )
' Get the computer variables.
computerVariables=computerSettings.MachineVariables
' Create and populate a new computer variable.
Set computerVariable = connection.Get("SMS_MachineVariable").SpawnInstance_
computerVariable.Name = name
computerVariable.Value = value
computerVariable.IsMasked = mask
' Add the new computer variable.
ReDim Preserve computerVariables (UBound (computerVariables)+1)
Set computerVariables(UBound(computerVariables)) = computerVariable
computerSettings.MachineVariables=computerVariables
computerSettings.Put_
End Sub
public void CreateComputerVariable(
WqlConnectionManager connection,
string siteCode,
string name,
string value,
bool mask,
int computerId)
{
try
{
// Get the computer settings.
IResultObject computerSettings=null;
IResultObject computerSettingsQuery = connection.QueryProcessor.ExecuteQuery(
"Select * from SMS_MachineSettings where ResourceId = '" + computerId + "'");
foreach (IResultObject settings in computerSettingsQuery)
{
computerSettings = settings;
}
if (computerSettings == null) // It does not exist, so create it.
{
computerSettings = connection.CreateInstance(@"SMS_MachineSettings");
computerSettings["ResourceID"].IntegerValue = computerId;
computerSettings["SourceSite"].StringValue = siteCode;
computerSettings["LocaleID"].IntegerValue = 1033;
computerSettings.Put();
computerSettings.Get();
}
// Create the computer variable.
List<IResultObject> computerVariables = computerSettings.GetArrayItems("MachineVariables");
IResultObject computerVariable = connection.CreateEmbeddedObjectInstance("SMS_MachineVariable");
computerVariable["Name"].StringValue = name;
computerVariable["Value"].StringValue = value;
computerVariable["IsMasked"].BooleanValue = mask;
// Add the computer variable to the computer settings.
computerVariables.Add(computerVariable);
computerSettings.SetArrayItems("MachineVariables", computerVariables);
computerSettings.Put();
}
catch (SmsException e)
{
Console.WriteLine("Failed to create computer variable: " + e.Message);
throw;
}
}
範例方法具有下列參數:
參數 | Type | 描述 |
---|---|---|
connection |
-管理: WqlConnectionManager - VBScript: SWbemServices |
SMS 提供者的有效連線。 |
siteCode |
-管理: String - VBScript: String |
來源月臺的月臺碼。 |
name |
-管理: String - VBScript: String |
要建立的變數名稱。 |
value |
-管理: String - VBScript: String |
變數值。 |
mask |
-管理: Boolean - VBScript: Boolean |
指定值是否顯示在 Configuration Manager 控制台中。true - 不會顯示變數值。false - 顯示變數值。 |
computerID |
-管理: Integer - VBScript: Integer |
計算機標識碼。 這通常是 SMS_R_System 類別 ResourceID 屬性。 |
正在編譯程式碼
C# 範例具有下列編譯需求:
命名空間
系統
System.Collections.Generic
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
組件
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
健全的程式設計
如需錯誤處理的詳細資訊,請 參閱關於 Configuration Manager 錯誤。
.NET Framework 安全性
如需保護 Configuration Manager 應用程式的詳細資訊,請參閱 Configuration Manager 角色型系統管理。