如何使用脚本部署类型创建应用程序

应用程序是Configuration Manager的新增功能。 在Configuration Manager之前,包是用于安装软件的基本对象。 现在,Configuration Manager中存在一个更灵活、更完整的应用程序模型。 基于此新模型的软件称为应用程序。 包仍存在于Configuration Manager中,但它们的定义和行为方式与Configuration Manager中的包大致相同。

应用程序模型定义一组标准属性和元数据,系统使用这些属性和元数据来管理应用程序的生命周期。 在对应用程序进行建模时,应用程序本身可以是一个构建基块,用于帮助定义系统中的其他应用程序。 例如,可以将.NET Framework定义为应用程序,然后父应用程序可以将它引用为在安装父应用程序之前必须存在或安装的依赖项。

使用脚本部署类型创建应用程序

为了开始创建应用程序,以下部分定义了一个简单的应用程序及其基本属性。 假设从Configuration Manager的角度来看,创建的所有应用程序都是新的,那么将新应用程序添加到Configuration Manager相对简单。 如果应用程序可能已存在,并且可能与其他应用程序有关系,则通过依赖项或取代会更加复杂,并且未在下面的示例中介绍。

下面显示了一个简单的命令行程序,该程序演示如何通过 SMS 提供程序创建模型并保存到数据库。 作为示例,它包含硬编码的字符串,不应被视为用于自动创建应用程序的实际应用程序。 此外,错误处理最少。 但是,此示例应该足以帮助你入门。

其他参考

有关详细信息,请参阅以下博客文章:

使用脚本部署类型创建应用程序
  1. 初始化提供程序连接和 ApplicationFactory。 (应用程序工厂是一个包装器,使创建提供程序类更加容易。)

  2. 创建应用程序和部署类型。

  3. 将应用程序保存到提供程序。

    若要使用此示例,请创建新的命令行 C# 应用程序,并复制并替换显示的代码。 需要添加对以下五个程序集的引用,这些程序集都在 adminconsole\bin 目录中找到:

  • AdminUI.AppManFoundation.dll

    封装用于创建应用程序的Configuration Manager提供程序功能的包装器。

  • AdminUI.WqlQueryEngine.dll

    WqlConnectionManager。

  • Microsoft.ConfigurationManagement.ApplicationManagement.dll

    用于序列化/反序列化应用程序的核心应用程序模型。

  • Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll

    Windows 安装程序和脚本部署类型的实现。

  • Microsoft.ConfigurationManagement.ManagementProvider.dll

    Configuration Manager托管的 WMI 接口。

    编译并运行应用程序后,应用程序的输出将在成功时显示此输出。

C:\sms\AdminConsole\bin>ApplicationCreator.exe

Connecting to the SMS Provider on computer [machinename].
Initializing the ApplicationFactory.Creating application [app].
Creating Script DeploymentType.
Initializing the SMS_Application object with the model.
Saving application, Title: [app], Scope: [ScopeId_D5230FF0-B439-44D9-906E-00A330F2AE06].
Successfully saved application.

示例

以下示例方法使用脚本部署类型创建一个应用程序,并保存到 Configuration Manager 数据库中。

注意

此代码是一个示例。 它不包含所有情况下的错误处理,也不演示依赖项和取代等关系。 它也不演示如何为部署类型创建要求规则。

namespace ApplicationCreator
{
    using System;
    using System.IO;
    using Microsoft.ConfigurationManagement.AdminConsole.AppManFoundation;
    using Microsoft.ConfigurationManagement.ApplicationManagement;
    using Microsoft.ConfigurationManagement.ManagementProvider;
    using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
    class Program
    {
        static void Main(string[] args)
        {
            Initialize(Environment.MachineName);
            Application application = CreateApplication("app", "description", System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
            application.DeploymentTypes.Add(CreateScriptDt(application.Title, application.Description, "notepad.exe", "return 0;", null));
            Store(application);
        }

        private static AppManWrapper wrapper;
        private static ApplicationFactory factory;

        // Initializes the default authoring scope and establishes connection to the SMS Provider.
        // <param name="siteServerName">A string containing the name of the Configuration Manager site.</param>

        public static void Initialize(string siteServerName)
        {
            Validator.CheckForNull(siteServerName, "siteServerName");
            Log("Connecting to the SMS Provider on computer [{0}].", siteServerName);
            // Creates a connection to the SMS Provider.
            WqlConnectionManager connectionManager = new WqlConnectionManager();
            connectionManager.Connect(siteServerName);
            Log("Initializing the ApplicationFactory.");
            // Initialize application wrapper and factory for creating the SMS Provider application object.
            factory = new ApplicationFactory();
            wrapper = AppManWrapper.Create(connectionManager, factory) as AppManWrapper;
        }

        // Inserts the provided application to the provided connected Configuration Manager site.
        // <param name="application">An application object that will be inserted into the Configuration Manager site.</param>

        public static void Store(Application application)
        {
            Validator.CheckForNull(application, "application");
            Validator.CheckForNull(wrapper, "wrapper");
            Exception ex = null;
            try
            {
                // Set the application into the provider object.
                wrapper.InnerAppManObject = application;
                Log("Initializing the SMS_Application object with the model.");
                factory.PrepareResultObject(wrapper);
                Log("Saving application, Title: [{0}], Scope: [{1}].", application.Title, application.Scope);
                // Save to the database.
                wrapper.InnerResultObject.Put();
            }
            catch (SmsException exception)
            {
                ex = exception;
            }
            catch (Exception exception)
            {
                ex = exception;
            }
            if (ex != null)
            {
                Log("ERROR saving application [{0}].", ex.Message);
                Log(ex);
            }
            else
            {
                Log("Successfully saved application.");
            }
        }

        // Creates an Application object.
        // <param name="title">The title of the application that will be visible in the admin console and in the Software Center and Portal.</param>
        // <param name="description">The description for the application.</param>
        // <param name="language">The language of the resources supplied.</param>

        public static Application CreateApplication(string title, string description, string language)
        {
            Validator.CheckForNull(title, "title");
            Validator.CheckForNull(language, "language");
            Log("Creating application [{0}].", title);
            Application app = new Application { Title = title };
            app.DisplayInfo.DefaultLanguage = language;
             app.DisplayInfo.Add(new AppDisplayInfo { Title = title, Description = description, Language = language });
            return app;
        }

        // Creates a Deployment Type with a Script Installer.
        // <param name="title">A string containing the title for the Deployment Type (required).</param>
        // <param name="description"> A string containing the description for the Deployment Type (optional).</param>
        // <param name="installCommandLine">A string containing the installation command line for the installer (required).</param>
        // <param name="detectionScript">A string containing the script for detection, this would most likely be separated out.
        // to a different method to support creating different detection method types such as Windows Installer, EHD, and script. Additionally, in the case
        // of script, the more likely scenario would be to load the script from a file, read the file, and then set the value.</param>
        // <param name="contentFolder">The folder that will contain the set of files that will represent the content for this Deployment Type. Validation
        // should verify that this is a UNC path, otherwise the Configuration Manager system will fail to create the content package correctly.</param>
        // <returns>A deployment type object.</returns>

        public static DeploymentType CreateScriptDt(string title, string description, string installCommandLine, string detectionScript, string contentFolder)
        {
            Validator.CheckForNull(installCommandLine, "installCommandLine");
            Validator.CheckForNull(title, "title");
            Validator.CheckForNull(detectionScript, "detectionScript");
            Log("Creating Script DeploymentType.");
            ScriptInstaller installer = new ScriptInstaller();
            installer.InstallCommandLine = installCommandLine;
            installer.DetectionScript = new Script { Text = detectionScript, Language = ScriptLanguage.JavaScript.ToString() };
            // Only add content if specified and exists.
            if (Directory.Exists(contentFolder) == true)
            {
                Content content = ContentImporter.CreateContentFromFolder(contentFolder);
                if (content != null)
                {
                    installer.Contents.Add(content);
                }
            }
            DeploymentType dt = new DeploymentType(installer, ScriptInstaller.TechnologyId, NativeHostingTechnology.TechnologyId);
            dt.Title = title;
            return dt;
        }

        public static void Log(Exception exception)
        {
            Log("ERROR: [{0}] ", exception.Message);
            Log("Stack: [{0}]", exception.StackTrace);
            if (exception.InnerException != null)
            {
                Log(exception.InnerException);
            }
        }

        public static void Log(string message, params object[] args)
        {
            Console.WriteLine(message, args);
        }
    }
}

命名空间

系统警报

System.IO

Microsoft。ConfigurationManagement.AdminConsole.AppManFoundation

Microsoft。ConfigurationManagement.ApplicationManagement

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

AdminUI.AppManFoundation.dll

AdminUI.WqlQueryEngine.dll

Microsoft.ConfigurationManagement.ApplicationManagement.dll

Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll

Microsoft.ConfigurationManagementProvider.dll

AdminUI.DcmObjectWrapper.dll

DcmObjectModel.dll

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

另请参阅

SMS_Collection服务器 WMI 类