列出、创建、修改和删除的简单示例
以下示例演示使用 SMS_Package
类演示使用 SMS 提供程序执行列表、创建、修改和删除操作的非常基本的方法集。 本文介绍基本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;
}
}
}
}
示例方法具有以下参数:
参数 | 类型 | 说明 |
---|---|---|
connection |
-管理: WqlConnectionManager |
与 SMS 提供程序的有效连接。 |
newPackageName |
-管理: String |
新包的名称。 |
newPackageDescription |
-管理: String |
新包的说明。 |
existingPackageID |
-管理: String |
现有的包标识符。 这是 类的 SMS_Package 键值,用于返回类的特定实例 SMS_Package 。 上述示例中的 ListPackages 方法返回当前包实例的名称和 PackageID。 |
编译代码
C# 示例需要:
命名空间
系统警报
Microsoft。ConfigurationManagement.ManagementProvider
Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
adminui.wqlqueryengine
microsoft.configurationmanagement.managementprovider
mscorlib
可靠编程
有关错误处理的详细信息,请参阅关于Configuration Manager错误。