如何枚举操作系统部署任务序列中的步骤

Configuration Manager,通过使用递归方法扫描任务序列步骤和组,枚举操作系统部署任务序列。

枚举任务序列中的步骤

  1. 设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识

  2. SMS_TaskSequence 对象获取有效的任务序列。 有关详细信息,请参阅 如何创建操作系统部署任务序列

  3. 枚举步骤以显示任何操作 (SMS_TaskSequence_Action) 名称。 使用递归访问找到并显示其操作的任何组 (SMS_TaskSequence_Group)

示例

以下示例显示任务序列中的操作和组。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段

Sub RecurseTaskSequenceSteps(taskSequence, indent)  

    Dim osdStep   
    Dim i  

    ' Indent each new group.  
    for each osdStep in taskSequence.Steps  

        for i=0 to indent  
            WScript.StdOut.Write " "  
        next  

        If osdStep.SystemProperties_("__CLASS")="SMS_TaskSequence_Group" Then  
            wscript.StdOut.Write "Group: "   
        End If  

        WScript.Echo osdStep.Name  

        ' Recurse into each group found.  
        If osdStep.SystemProperties_("__CLASS")="SMS_TaskSequence_Group" Then  
            If IsNull(osdStep.Steps) Then  
                Wscript.Echo "No steps"  
            Else  
                Call RecurseTaskSequenceSteps (osdStep, indent+3)  
            End If      
        End If  
     Next     
End Sub          
public void RecurseTaskSequenceSteps(  
    IResultObject taskSequence,  
    int indent)  
{  
    try  
    {  
        // The array of SMS_TaskSequence_Steps.  
        List<IResultObject> steps = taskSequence.GetArrayItems("Steps");  

        foreach (IResultObject ro in steps)  
        {  
            for (int i = 0; i < indent; i++)  
            {  
                Console.Write(" ");  
            }  

            if (ro["__CLASS"].StringValue == "SMS_TaskSequence_Group")  
            {  
                Console.Write("Group: ");  
            }  

            Console.WriteLine(ro["Name"].StringValue);  

            // Child groups that are found. Use recursion to view them.  
            if (ro["__CLASS"].StringValue == "SMS_TaskSequence_Group")  
            {  
                this.RecurseTaskSequenceSteps(ro, indent + 3);  
            }  
        }  
    }  
    catch (SmsException e)  
    {  
        Console.WriteLine("Failed To enumerate task sequence items: " + e.Message);  
        throw;  
    }  
}  

示例方法具有以下参数:

参数 类型 说明
taskSequence -管理: IResultObject
- VBScript: SWbemObject
有效的任务序列 (SMS_TaskSequence) 。 组将添加到此任务序列。
indent -管理: Integer
- VBScript: Integer
缩进用于为子组设置控制台输出的空间。

编译代码

此 C# 示例需要:

命名空间

System

System.Collections.Generic

System.Text

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

可靠编程

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

.NET Framework 安全性

有关保护Configuration Manager应用程序的详细信息,请参阅Configuration Manager基于角色的管理

另请参阅

对象概述如何添加操作系统部署任务序列操作
如何使用托管代码连接到 Configuration Manager 中的 SMS 提供程序
如何使用 WMI 连接到 Configuration Manager 中的短信提供程序
任务序列概述