清單、建立、修改和刪除的簡單範例
下列範例示範一組非常基本的方法, SMS_Package
使用 類別來示範使用 SMS 提供者的 List、Create、Modify 和 Delete 作業。 這是基本Configuration Manager程式的結構 , SDK 的其他區域中有更實用的方法程式碼片段可完成特定工作。
重要事項
為了簡化程式碼範例,某些方法會加上批註,因為它們需要 (現有套件識別碼) 的其他資訊。
ListPackages
使用 方法來取得套件識別碼,以便與 ModifyPackage
和 DeletePackage
方法搭配使用。
若要列出套件
設定與 SMS 提供者的連線。
執行查詢,此查詢會在變數中填入類別實例的
SMS_Package
集合。列舉集合,並列出查詢所傳回的封裝。
建立集合
設定與 SMS 提供者的連線。
使用
SMS_Package
類別建立新的封裝物件。填入新的套件屬性。
儲存封裝。
修改套件
設定與 SMS 提供者的連線。
使用
SMS_Package
類別載入現有的封裝物件。修改封裝屬性。
儲存封裝。
若要刪除套件
設定與 SMS 提供者的連線。
使用
SMS_Package
類別載入現有的封裝物件。使用 delete 方法刪除封裝。
範例
下列範例方法顯示一組非常基本的方法,使用 SMS_Package
類別來示範使用 SMS 提供者的 List、Create、Modify 和 Delete 作業。
如需呼叫範例程式碼的相關資訊,請參閱呼叫Configuration Manager程式碼片段。
注意事項
下列範例會在程式碼中內嵌呼叫程式碼。 SDK 中的大多數其他範例只會顯示具有參數的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Added the below Configuration Manager DLL references to support basic SMS Provider operations:
// C:\Program Files (x86)\Microsoft Endpoint Manager\AdminConsole\bin\Microsoft.ConfigurationManagement.ManagementProvider.dll
// C:\Program Files (x86)\Microsoft Endpoint Manager\AdminConsole\bin\AdminUI.WqlQueryEngine.dll
// Added the below Configuration Manager namespaces to support basic SMS Provider operations:
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
//
// A set of very basic methods using the SMS_Package class to demonstrate List, Create, Modify and Delete operations using the SMS Provider.
//
// Note: To simplify the code example, some methods are commented out, as they need additional information (an existing package identifier).
// Use the ListPackages method to obtain the package identifier for use with the ModifyPackage and DeletePackage methods.
//
namespace BasicApp
{
class Program
{
static void Main(string[] args)
{
// Setup Objects
SnippetClass BasicCMAppSnippets = new SnippetClass();
// Setup a connection to the SMS Provider.
// Passing in <server name>, <domain\\account>, <password>.
WqlConnectionManager WMIConnection = BasicCMAppSnippets.Connect("CMLABSERVER", "CMLABSERVER\\cmlabuser", "password");
// List all packages (instances of SMS_Package).
BasicCMAppSnippets.ListPackages(WMIConnection);
// Create a new package.
// Note: This is not a useful package (too few properties), just a demonstration of creating a Configuration Manager object.
BasicCMAppSnippets.CreatePackage(WMIConnection, "New Package", "This is the new package.");
// Modifies a specific package (instance of SMS_Package).
// A valid PackageID needs to be passed to the ModifyPackage method - replace "ABC00000".
//BasicCMAppSnippets.ModifyPackage(WMIConnection, "ABC00000");
// Deletes a specific package (instance of SMS_Package).
// A valid PackageID needs to be passed to the DeletePackage method - replace "ABC00000".
//BasicCMAppSnippets.DeletePackage(WMIConnection, "ABC00000");
// Delay to keep the console output visible.
Console.ReadLine();
}
}
class SnippetClass
{
public WqlConnectionManager Connect(string serverName, string userName, string userPassword)
{
try
{
SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
WqlConnectionManager connection = new WqlConnectionManager(namedValues);
if (System.Net.Dns.GetHostName().ToUpper() == serverName.ToUpper())
{
connection.Connect(serverName);
}
else
{
connection.Connect(serverName, userName, userPassword);
}
return connection;
}
catch (SmsException ex)
{
Console.WriteLine("Failed to connect. Error: " + ex.Message);
return null;
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Failed to authenticate. Error:" + ex.Message);
throw;
}
}
public void ListPackages(WqlConnectionManager connection)
{
try
{
// This query selects all packages (instances of SMS_Package).
string query = "SELECT * FROM SMS_Package";
// Run query, which populates 'listOfPackages' with a collection of package objects.
IResultObject listOfPackages = connection.QueryProcessor.ExecuteQuery(query);
// Output header for list of distribution points.
Console.WriteLine(" ");
Console.WriteLine("List of packages: ");
Console.WriteLine("-------------------");
// Enumerate through the collection of objects returned by the query.
foreach (IResultObject package in listOfPackages)
{
// Output the package name for each package object.
Console.WriteLine("Package ID: {0} Package Name: {1}", package["PackageID"].StringValue, package["Name"].StringValue);
}
}
catch (SmsException ex)
{
Console.WriteLine("Failed to list packages. Error: " + ex.Message);
throw;
}
}
public void CreatePackage(WqlConnectionManager connection, string newPackageName, string newPackageDescription)
{
try
{
// Create new package object.
IResultObject newPackage = connection.CreateInstance("SMS_Package");
// Populate new package properties.
newPackage["Name"].StringValue = newPackageName;
newPackage["Description"].StringValue = newPackageDescription;
// Save the new package and the new package properties.
newPackage.Put();
// The key value 'PackageID' is created on the put, so getting the package object to output the unique 'PackageID' ('Name' is not guaranteed to be unique).
newPackage.Get();
// Output new package name.
Console.WriteLine("Created Package ID: {0} Package Name: {1}", newPackage["PackageID"].StringValue, newPackage["Name"].StringValue);
}
catch (SmsException ex)
{
Console.WriteLine("Failed to create package. Error: " + ex.Message);
throw;
}
}
public void ModifyPackage(WqlConnectionManager connection, string existingPackageID)
{
try
{
// Get the specific package instance to modify (PackageID is a key value).
IResultObject packageToModify = connection.GetInstance(@"SMS_Package.PackageID='" + existingPackageID + "'");
// Modify a package properties (in this case description).
packageToModify["Description"].StringValue = "This package has been modified. " + packageToModify["Description"].StringValue;
// Save the new package and the new package properties.
packageToModify.Put();
}
catch (SmsException ex)
{
Console.WriteLine("Failed to delete package. Error: " + ex.Message);
throw;
}
}
public void DeletePackage(WqlConnectionManager connection, string existingPackageID)
{
try
{
// Get the specific package instance to delete (PackageID is a key value).
IResultObject packageToDelete = connection.GetInstance(@"SMS_Package.PackageID='" + existingPackageID + "'");
// Output package ID and name being deleted.
Console.WriteLine("Deleting Package ID: {0} Package Name: {1}", packageToDelete["PackageID"].StringValue, packageToDelete["Name"].StringValue);
// Delete the package.
packageToDelete.Delete();
}
catch (SmsException ex)
{
Console.WriteLine("Failed to delete package. Error: " + ex.Message);
throw;
}
}
}
}
範例方法具有下列參數:
參數 | Type | 描述 |
---|---|---|
connection |
-管理: WqlConnectionManager |
SMS 提供者的有效連線。 |
newPackageName |
-管理: String |
新封裝的名稱。 |
newPackageDescription |
-管理: String |
新套件的描述。 |
existingPackageID |
-管理: String |
現有的封裝識別碼。 這是 類別的索引鍵值, SMS_Package 可用來傳回 類別的 SMS_Package 特定實例。 上述範例中的 ListPackages 方法會傳回目前封裝實例的名稱和 PackageID。 |
正在編譯程式碼
C# 範例需要:
命名空間
系統
Microsoft。ConfigurationManagement.ManagementProvider
Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine
組件
adminui.wqlqueryengine
microsoft.configurationmanagement.managementprovider
mscorlib
健全的程式設計
如需錯誤處理的詳細資訊,請參閱關於Configuration Manager錯誤。