如何使用 Configuration Manager 中的任务序列变量How to use task sequence variables in Configuration Manager

适用范围:Configuration Manager (Current Branch)Applies to: Configuration Manager (current branch)

Configuration Manager 的 OS 部署功能中的任务序列引擎使用多个变量来控制其行为。The task sequence engine in the OS deployment feature of Configuration Manager uses many variables to control its behaviors. 使用这些变量:Use these variables to:

  • 设置步骤条件Set conditions on steps
  • 更改特定步骤的行为Change behaviors for specific steps
  • 在脚本中使用以执行更复杂的操作Use in scripts for more complex actions

有关所有可用的任务序列变量的引用,请参阅任务序列变量For a reference of all available task sequence variables, see Task sequence variables.

变量类型Types of variables

有多种类型的变量:There are several types of variables:

内置变量Built-in variables

内置变量提供有关任务序列运行环境的信息。Built-in variables provide information about the environment where the task sequence runs. 其值可在整个任务序列中使用。Their values are available throughout the whole task sequence. 通常情况下,任务序列引擎会在运行任何步骤之前初始化内置变量。Typically, the task sequence engine initializes built-in variables before it runs any steps.

例如,_SMSTSLogPath 是一个环境变量,指定 Configuration Manager 组件用于写入日志文件的路径。For example, _SMSTSLogPath is an environment variable that specifies the path to which Configuration Manager components write log files. 任何任务序列步骤均可访问此环境变量。Any task sequence step can access this environment variable.

任务序列会在每个步骤前计算一些变量。The task sequence evaluates some variables before each step. 例如,_SMSTSCurrentActionName 列出当前步骤的名称。For example, _SMSTSCurrentActionName lists the name of the current step.

操作变量Action variables

任务序列操作变量指定单个任务序列步骤使用的配置设置。Task sequence action variables specify configuration settings that a single task sequence step uses. 默认情况下,该步骤在运行之前会先初始化其设置。By default, the step initializes its settings before it runs. 仅当相关联的任务序列步骤运行时,这些设置才可用。These settings are available only while the associated task sequence step runs. 运行步骤前,任务序列会将操作变量值添加到环境。The task sequence adds the action variable value to the environment before it runs the step. 该步骤运行之后,任务序列会从环境中删除该值。It then removes the value from the environment after the step runs.

例如,将“运行命令行”步骤添加到任务序列。For example, you add the Run Command Line step to a task sequence. 此步骤包括 Start In 属性。This step includes a Start In property. 任务序列将此属性的默认值存储为 WorkingDirectory 变量。The task sequence stores a default value for this property as the WorkingDirectory variable. 在运行“运行命令行”步骤之前,任务序列会初始化该值。The task sequence initializes this value before it runs the Run Command Line step. 在运行此步骤时,从 WorkingDirectory 值访问 Start In 属性值。While this step is running, access the Start In property value from the WorkingDirectory value. 该步骤完成之后,任务序列从环境中删除 WorkingDirectory 变量值。After the step completes, the task sequence removes the value of the WorkingDirectory variable from the environment. 如果任务序列包含另一个“运行命令行”步骤,它将初始化一个新的 WorkingDirectory 变量。If the task sequence includes another Run Command Line step, it initializes a new WorkingDirectory variable. 此时,任务序列会将变量设置为当前步骤的起始值。At that time, the task sequence sets the variable to the starting value for the current step. 有关详细信息,请参阅 WorkingDirectoryFor more information, see WorkingDirectory.

步骤运行时,操作变量的默认值为“present”。The default value for an action variable is present when the step runs. 如果设置为新值,该值可供任务序列中的多个步骤使用。If you set a new value, it's available to multiple steps in the task sequence. 如果重写默认值,新值将保留在环境中。If you override a default value, the new value stays in the environment. 此新值将覆盖任务序列中其他步骤的默认值。This new value overrides the default value for other steps in the task sequence. 例如,添加“设置任务序列变量”步骤作为任务序列的第一步。For example, you add a Set Task Sequence Variable step as the first step of the task sequence. 此步骤将 WorkingDirectory 变量设置为 C:\This step sets the WorkingDirectory variable to C:\. 任务序列中的任何“运行命令行”步骤均使用新的起始目录值。Any Run Command Line step in the task sequence uses the new starting directory value.

某些任务序列步骤将特定操作变量标记为“输出”。Some task sequence steps mark certain action variables as output. 任务序列中的后续步骤会读取这些输出变量。Steps later in the task sequence read these output variables.

备注

并非所有任务序列步骤都具有操作变量。Not all task sequence steps have action variables. 例如,虽然有与“启用 BitLocker”操作相关联的变量,但没有与“禁用 BitLocker”操作相关联的变量。For example, although there are variables associated with the Enable BitLocker action, there are no variables associated with the Disable BitLocker action.

自定义变量Custom variables

这些变量是 Configuration Manager 不会创建的任何变量。These variables are any that Configuration Manager doesn't create. 将自己的变量初始化为条件,以便在命令行或脚本中使用。Initialize your own variables to use as conditions, in command lines, or in scripts.

为新任务序列变量指定名称时,请遵循以下准则:When you specify a name for a new task sequence variable, follow these guidelines:

  • 任务序列变量名称可以包含字母、数字、下划线字符 (_) 和连字符 (-)。The task sequence variable name can include letters, numbers, the underscore character (_), and a hyphen (-).

  • 任务序列变量名称长度至少为 1 个字符,最多为 256 个字符。Task sequence variable names have a minimum length of one character and a maximum length of 256 characters.

  • 用户定义的变量必须以字母(A-Za-z)开头。User-defined variables must begin with a letter (A-Z or a-z).

  • 用户定义的变量名称不能以下划线字符开头。User-defined variable names can't begin with the underscore character. 仅只读任务序列变量的前面使用下划线字符。Only read-only task sequence variables are preceded by the underscore character.

  • 任务序列变量名称不区分大小写。Task sequence variable names aren't case-sensitive. 例如,OSDVARosdvar 是相同的任务序列变量。For example, OSDVAR and osdvar are the same task sequence variable.

  • 任务序列变量名称不能以空格开头或结尾。Task sequence variable names can't begin or end with a space. 此外还不能有嵌入空格。They also can't have embedded spaces. 变量名称开头或结尾的任何空格会被任务序列忽略。The task sequence ignores any spaces at the beginning or the end of a variable name.

对于可以创建的任务序列变量数量没有设定限制。There's no set limit to how many task sequence variables you can create. 但是,变量数受任务序列环境大小的限制。However, the number of variables is limited by the size of the task sequence environment. 任务序列环境的总大小上限为 8KB。The total size limit for the task sequence environment is 8 KB. 有关详细信息,请参阅减少任务序列策略的大小For more information, see Reduce the size of task sequence policy.

只读变量Read-only variables

不能更改一些只读变量的值。You can't change the value of some variables, which are read-only. 通常该名称以下划线字符(_) 开头。Usually the name begins with an underscore character (_). 任务序列将这些变量用于操作。The task sequence uses them for its operations. 只读变量在任务序列环境中可见。Read-only variables are visible in the task sequence environment.

这些变量在脚本或命令行中很有用。These variables are useful in scripts or command-lines. 例如,运行命令行,并将输出结果传输到 _SMSTSLogPath 中的日志文件,与其他日志文件结合使用。For example, running a command line and piping the output to a log file in _SMSTSLogPath with the other log files.

备注

只读任务序列变量可由任务序列中的步骤读取,但它们无法被设置。Read-only task sequence variables can be read by steps in a task sequence but they can't be set. 例如,使用只读变量作为“运行命令行”步骤的命令行的一部分。For example, use a read-only variable as part of the command line for a Run Command Line step. 不能使用“设置任务序列变量”步骤设置只读变量。You can't set a read-only variable by using the Set Task Sequence Variable step.

数组变量Array variables

任务序列将一些变量存储为数组。The task sequence stores some variables as an array. 数组中的每个元素都代表单个对象设置。Each element in the array represents the settings for a single object. 当设备具有多个要配置的对象时,请使用这些变量。Use these variables when a device has more than one object to configure. 下列任务序列步骤使用数组变量:The following task sequence steps use array variables:

如何设置变量How to set variables

对于自定义变量或并非只读的变量,可以通过以下几种方法来初始化并设置变量的值:For custom variables or variables that aren't read-only, there are several methods to initialize and set the value of the variable:

从环境中删除变量的方法与创建变量相同。Delete a variable from the environment by using the same methods as creating a variable. 要删除一个变量,请将变量值设置为空字符串。To delete a variable, set the variable value to an empty string.

可以组合多个方法以将任务序列变量设置为相同序列的不同值。You can combine methods to set a task sequence variable to different values for the same sequence. 例如,使用任务序列编辑器设置默认值,然后使用脚本设置自定义值。For example, set the default values using the task sequence editor, and then set custom values using a script.

如果通过不同方法设置相同变量,任务序列引擎将使用以下顺序:If you set the same variable by different methods, the task sequence engine uses the following order:

  1. 它首先会计算集合变量。It evaluates collection variables first.

  2. 特定于设备的变量会重写在集合上设置的相同变量。Device-specific variables override the same variable set on a collection.

  3. 由任务序列过程中的任何方法设置的变量优先于集合或设备变量。Variables set by any method during the task sequence take precedence over collection or device variables.

任务序列变量值的一般限制General limitations for task sequence variable values

  • 任务序列变量值不能超过 4,000 个字符。Task sequence variable values can't be more than 4,000 characters.

  • 不能更改只读任务序列变量。You can't change a read-only task sequence variable. 只读变量使用以下划线 (_) 字符开头的名称。Read-only variables have names that start with an underscore character (_).

  • 任务序列变量值可能区分大小写,具体情况视值的使用情况而定。Task sequence variable values can be case-sensitive depending on the usage of the value. 大多数情况下,任务序列变量值不区分大小写。In most cases, task sequence variable values aren't case-sensitive. 包含密码的变量区分大小写。A variable that includes a password is case-sensitive.

设置任务序列变量Set Task Sequence Variable

在任务序列中使用此步骤将单个变量设置为单个值。Use this step in the task sequence to set a single variable to a single value.

有关详细信息,请参阅设置任务序列变量For more information, see Set Task Sequence Variable.

设置动态变量Set Dynamic Variables

在任务序列中使用此步骤设置一个或多个任务序列变量。Use this step in the task sequence to set one or more task sequence variables. 在此步骤中定义规则,以确定要使用哪些变量和值。You define rules in this step to determine which variables and values to use.

有关详细信息,请参阅设置动态变量For more information, see Set Dynamic Variables.

运行 PowerShell 脚本Run PowerShell Script

在任务序列中使用此步骤,可以使用 PowerShell 脚本来设置任务序列变量。Use this step in the task sequence to use a PowerShell script to set a task sequence variable.

可以从包中指定脚本名称,也可以直接在步骤中输入 PowerShell 脚本。You can specify a script name from a package, or directly enter a PowerShell script in the step. 然后,使用“输出到任务序列变量”步骤属性,将脚本输出保存到自定义任务序列变量。Then use the step property to Output to task sequence variable to save the script output to a custom task sequence variable.

若要详细了解此步骤,请参阅运行 PowerShell 脚本For more information on this step, see Run PowerShell Script.

备注

还可以结合使用 PowerShell 脚本和 TSEnvironment 对象来设置一个或多个变量。You can also use a PowerShell script to set one or more variables with the TSEnvironment object. 有关详细信息,请参阅 Configuration Manager SDK 中的如何在运行的任务序列中使用变量For more information, see How to use variables in a running task sequence in the Configuration Manager SDK.

“运行 PowerShell 脚本”步骤的示例方案Example scenario with Run PowerShell Script step

由于你的环境中拥有多个国家/地区的用户,因此你想要查询操作系统语言,以设置为多语言特定的“应用操作系统”步骤的条件。Your environment has users in multiple countries/regions, so you want to query the OS language to set as a condition on multiple language-specific Apply OS steps.

  1. 先向任务序列添加“运行 PowerShell 脚本”的实例,再执行“应用操作系统”步骤。Add an instance of the Run PowerShell Script to the task sequence before the Apply OS steps.

  2. 使用“输入 PowerShell 脚本”选项来指定以下命令:Use the option to Enter a PowerShell script to specify the following command:

    (Get-Culture).TwoLetterISOLanguageName
    

    若要详细了解此 cmdlet,请参阅 Get-CultureFor more information on the cmdlet, see Get-Culture. 若要详细了解两字母 ISO 语言名称,请参阅 ISO 639-1 代码列表For more information on the two-letter ISO language names, see List of ISO 639-1 codes.

  3. 对于“输出到任务序列变量”选项,指定“CurrentOSLanguage”。For the option to Output to task sequence variable, specify CurrentOSLanguage.

    “运行 PowerShell 脚本”步骤的示例方案的屏幕截图

  4. 在英语映像的“应用操作系统”步骤中,创建以下条件:Task Sequence Variable CurrentOSLanguage equals "en"On the Apply OS step for the English language image, create the following condition: Task Sequence Variable CurrentOSLanguage equals "en"

    “应用操作系统”步骤的示例条件的屏幕截图

    提示

    若要详细了解如何在步骤中创建条件,请参阅如何访问变量 - 步骤条件For more information on how to create a condition on a step, see How to access variables - Step condition.

  5. 保存和部署任务序列。Save and deploy the task sequence.

如果“运行 PowerShell 脚本”步骤在安装了英语版本 Windows 的设备上运行,此命令返回值 enWhen the Run PowerShell Script step runs on a device with the English language version of Windows, the command returns the value en. 然后,它将此值保存到自定义变量。It then saves that value into the custom variable. 当英语映像的“应用操作系统”步骤在同一设备上运行时,条件的计算结果为 true。When the Apply OS step for the English language image runs on the same device, the condition evaluates to true. 如果你有“应用操作系统”步骤的多个实例对应不同的语言,任务序列会动态地运行与操作系统语言匹配的步骤。If you have multiple instances of the Apply OS step for different languages, the task sequence dynamically runs the step that matches the OS language.

集合和设备变量Collection and device variables

可以为设备和集合定义自定义的任务序列变量。You can define custom task sequence variables for devices and collections. 为设备定义的变量称为“每设备任务序列变量”。Variables that you define for a device are referred to as per-device task sequence variables. 为集合定义的变量称为特定于集合的任务序列变量。Variables defined for a collection are referred to as per-collection task sequence variables. 如有冲突,特定于设备的变量优先于特定于集合的变量。If there's a conflict, per-device variables take precedence over per-collection variables. 此行为意味着,分配到特定设备的任务序列变量会自动获得比分配到包含该设备的集合的变量更高的优先级。This behavior means that task sequence variables that are assigned to a specific device automatically have a higher priority than variables that are assigned to the collection that contains the device.

例如,设备 XYZ 是集合 ABC 的成员。For example, device XYZ is a member of collection ABC. 将 MyVariable 分配到集合 ABC,值为 1。You assign MyVariable to collection ABC with a value of 1. 同时将 MyVariable 分配到设备 XYZ,值为 2。You also assign MyVariable to device XYZ with a value of 2. 分配给 XYZ 的变量优先于分配给集合 ABC 的变量。The variable that's assigned to XYZ has higher priority than the variable that's assigned to collection ABC. 当具有此变量的任务序列在 XYZ 上运行时,MyVariable 的值为 2。When a task sequence with this variable runs on XYZ, MyVariable has a value of 2.

可以隐藏特定于设备和特定于集合的变量,以便它们在 Configuration Manager 控制台中不可见。You can hide per-device and per-collection variables so that they aren't visible in the Configuration Manager console. 使用选项“不要在 Configuration Manager 控制台中显示此值”时,控制台中不显示该变量的值。When you use the option Do not display this value in the Configuration Manager console, the value of the variable isn't displayed in the console. 任务序列日志文件 (smsts.log) 或任务序列调试器也不会显示变量值。The task sequence log file (smsts.log) or the task sequence debugger won't show the variable value either. 但在变量运行时,任务序列仍可使用该变量。The variable can still be used by the task sequence when it runs. 如果不想再隐藏这些变量,请先删除它们。If you no longer want these variables to be hidden, delete them first. 然后重新定义变量,而无需选择选项来隐藏它们。Then redefine the variables without selecting the option to hide them.

警告

如果你将变量添加在“运行命令行”步骤的命令行中,则任务序列日志文件会显示包含变量值的完整命令行。If you include variables in the Run Command Line step's command line, the task sequence log file displays the full command line including the variable values. 为了防止可能的敏感数据显示在日志文件中,请将任务序列变量“OSDDoNotLogCommand”设置为 TRUETo prevent potentially sensitive data from appearing in the log file, set the task sequence variable OSDDoNotLogCommand to TRUE.

可以在主站点或管理中心站点上管理因设备而异的变量。You can manage per-device variables at a primary site or at a central administration site. Configuration Manager 不支持一台设备具有 1,000 个以上的已分配变量。Configuration Manager doesn't support more than 1,000 assigned variables for a device.

重要

将特定于集合的变量用于任务序列时,请注意以下行为:When you use per-collection variables for task sequences, consider the following behaviors:

  • 对集合的更改始终会复制到整个层次结构。Changes to collections are always replicated throughout the hierarchy. 对集合变量所做的任何更改不仅应用于当前站点的成员,还会应用于整个层次结构中该集合的所有成员。Any changes that you make to collection variables apply not just to members of the current site, but to all members of the collection throughout the hierarchy.

  • 删除集合时,此操作还会删除为集合配置的任务序列变量。When you delete a collection, this action also deletes the task sequence variables that you configured for the collection.

为设备创建任务序列变量Create task sequence variables for a device

  1. 在 Configuration Manager 控制台中,转到“资产和符合性”工作区,并选择“设备”节点 。In the Configuration Manager console, go to the Assets and Compliance workspace, and select the Devices node.

  2. 依次选择目标设备和“属性”。Select the target device and select Properties.

  3. 在“属性”对话框中,切换到“变量”选项卡。In the Properties dialog box, switch to the Variables tab.

  4. 对于要创建的每个变量,请选择“新建”图标。For each variable that you want to create, select the New icon. 指定任务序列变量的名称和值。Specify the Name and Value of the task sequence variable. 若要隐藏变量使其在 Configuration Manager 控制台中不可见,请选择“不在 Configuration Manager 控制台中显示此值”选项。If you want to hide the variable so that it's not visible in the Configuration Manager console, select the option Do not display this value in the Configuration Manager console.

  5. 将所有变量添加到设备属性之后,选择“确定”。After you've added all the variables to the device properties, select OK.

为集合创建任务序列变量Create task sequence variables for a collection

  1. 在 Configuration Manager 控制台中,转到“资产和符合性”工作区,并选择“设备集合”节点。In the Configuration Manager console, go to the Assets and Compliance workspace, and select the Device Collections node. 选择目标集合,然后选择“属性”。Select the target collection and choose Properties.

  2. 在“属性”对话框中,切换到“集合变量”选项卡。In the Properties dialog box, switch to the Collection Variables tab.

  3. 对于要创建的每个变量,请选择“新建”图标。For each variable that you want to create, select the New icon. 指定任务序列变量的名称和值。Specify the Name and Value of the task sequence variable. 若要隐藏变量使其在 Configuration Manager 控制台中不可见,请选择“不在 Configuration Manager 控制台中显示此值”选项。If you want to hide the variable so that it's not visible in the Configuration Manager console, select the option Do not display this value in the Configuration Manager console.

  4. 根据需要为 Configuration Manager 指定要在评估任务序列变量时使用的优先级。Optionally, specify the priority for Configuration Manager to use when the task sequence variables are evaluated.

  5. 将所有变量添加到集合属性之后,选择“确定”。After you've added all the variables to the collection properties, select OK.

TSEnvironment COM 对象TSEnvironment COM object

若要使用脚本中的变量,请使用 TSEnvironment 对象。To work with variables from a script, use the TSEnvironment object.

有关详细信息,请参阅 Configuration Manager SDK 中的如何在运行的任务序列中使用变量For more information, see How to use variables in a running task sequence in the Configuration Manager SDK.

预启动命令Prestart command

预启动命令是在用户选择任务序列之前在 Windows PE 中运行的一个脚本或可执行文件。The prestart command is a script or executable that runs in Windows PE before the user selects the task sequence. 预启动命令可以查询变量或提示用户输入信息,然后将其保存在环境中。The prestart command can query a variable or prompt the user for information, and then save it in the environment. 使用 TSEnvironment COM 对象来读取和写入来自预启动命令的变量。Use the TSEnvironment COM object to read and write variables from the prestart command.

有关详细信息,请参阅任务序列媒体的预启动命令For more information, see Prestart commands for task sequence media.

任务序列向导Task Sequence Wizard

从版本 1906 开始,在“任务序列向导”窗口中选择任务序列后,用于编辑任务序列变量的页面中包含一个“编辑”按钮。Starting in version 1906, after you select a task sequence in the Task Sequence Wizard window, the page to edit task sequence variables includes an Edit button. 可使用可访问的键盘快捷方式编辑变量。You can use accessible keyboard shortcuts to edit the variables. 在鼠标不可用的情况下,这一更改会有所帮助。This change helps in cases where a mouse isn't available.

任务序列媒体向导Task Sequence Media Wizard

指定从媒体运行的任务序列的变量。Specify variables for task sequences that run from media. 使用媒体部署 OS 时,添加任务序列变量,并在创建媒体时指定其值。When using media to deploy the OS, you add the task sequence variables and specify their values when you create the media. 变量及其值存储在媒体上。The variables and their values are stored on the media.

备注

任务序列存储在独立媒体上。Task sequences are stored on stand-alone media. 但是,所有其他类型的媒体,如预留媒体,会从管理点中检索任务序列。However, all other types of media, such as prestaged media, retrieve the task sequence from a management point.

从媒体运行任务序列时,可以在向导的“自定义”页添加一个变量。When you run a task sequence from media, you can add a variable on the Customization page of the wizard.

使用媒体变量替代每集合变量或每计算机变量。Use the media variables in place of per-collection or per-computer variables. 如果正在从媒体运行任务序列,则不应用也不使用每计算机变量和每集合变量。If the task sequence is running from media, per-computer and per-collection variables don't apply and aren't used.

提示

任务序列将包 ID 和预启动命令行写入到运行 Configuration Manager 控制台的计算机上的 CreateTSMedia.log 文件。The task sequence writes the package ID and prestart command line to the CreateTSMedia.log file on the computer that runs the Configuration Manager console. 此日志文件包括任何任务序列变量的值。This log file includes the value for any task sequence variables. 查看此日志文件以验证任务序列变量的值。Review this log file to verify the value for the task sequence variables.

有关详细信息,请参阅创建任务序列媒体For more information, see Create task sequence media.

如何访问变量How to access variables

使用上一部分中所述的方法之一指定变量及其值之后,在任务序列中使用它。After you specify the variable and its value by using one of the methods from the previous section, use it in your task sequences. 例如,访问内置任务序列变量的默认值,或基于变量值设置步骤条件。For example, access default values for built-in task sequence variables, or make a step conditional on the value of a variable.

使用以下方法来访问任务序列环境中的变量值:Use the following methods to access variable values in the task sequence environment:

在步骤中使用Use in a step

在任务序列步骤中指定设置的变量值。Specify a variable value for a setting in a task sequence step. 在任务序列编辑器中编辑步骤,并将变量名称指定为字段值。In the task sequence editor, edit the step, and specify the variable name as the field value. 将变量名称括在百分号 (%) 中。Enclose the variable name in percent signs (%).

例如,使用变量名称作为“运行命令行”步骤的“命令行”字段的一部分。For example, use the variable name as part of the Command Line field of the Run Command Line step. 下面的命令行将计算机名称写入文本文件。The following command line writes the computer name to a text file.

cmd.exe /c %_SMSTSMachineName% > C:\File.txt

步骤条件Step condition

使用内置或自定义任务序列变量作为步骤或组条件的一部分。Use built-in or custom task sequence variables as part of a condition on a step or group. 在运行步骤或组之前,任务序列会计算变量值。The task sequence evaluates the variable value before it runs the step or group.

要添加计算变量值的条件,请执行下列步骤:To add a condition that evaluates a variable value, do the following steps:

  1. 在任务序列编辑器中,选择要添加条件的步骤或组。In the task sequence editor, select the step or group to which you want to add the condition.

  2. 切换到步骤或组的“选项”选项卡。Switch to the Options tab for the step or group. 单击“添加条件”,然后选择“任务序列变量”。Click Add Condition, and select Task Sequence Variable.

  3. 在“任务序列变量”对话框中,指定以下设置:In the Task Sequence Variable dialog box, specify the following settings:

    • 变量:变量的名称。Variable: The name of the variable. 例如,_SMSTSInWinPEFor example, _SMSTSInWinPE.

    • 条件:计算变量值的条件。Condition: The condition to evaluate the variable value. 例如,等于。For example, equals.

    • :要检查的变量的值。Value: The value of the variable to check. 例如,falseFor example, false.

上面三个示例构成了测试的常见条件,即任务是否从 Windows PE 的启动映像运行:The three examples above form a common condition to test whether the task sequence is running from a boot image in Windows PE:

任务序列变量 _SMSTSInWinPE equals "false"Task Sequence Variable _SMSTSInWinPE equals "false"

有关此情况,请参阅默认任务序列模板的“捕获文件和设置”组,以安装现有 OS 映像。See this condition on the Capture Files and Settings group of the default task sequence template to install an existing OS image.

若要详细了解条件,请参阅任务序列编辑器 - 条件For more information about conditions, see Task sequence editor - Conditions.

自定义脚本Custom script

可以在运行任务序列时使用 Microsoft.SMS.TSEnvironment COM 对象读取和写入变量。Read and write variables by using the Microsoft.SMS.TSEnvironment COM object while the task sequence is running.

下面的 Windows PowerShell 示例查询 _SMSTSLogPath 变量以获取当前日志位置。The following Windows PowerShell example queries the _SMSTSLogPath variable to get the current log location. 该脚本还设置自定义变量。The script also sets a custom variable.

# Create an object to access the task sequence environment
$tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment

# Query the environment to get an existing variable
# Set a variable for the task sequence log path
$LogPath = $tsenv.Value("_SMSTSLogPath")

# Or, convert all of the variables currently in the environment to PowerShell variables
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }

# Write a message to a log file
Write-Output "Hello world!" | Out-File -FilePath "$_SMSTSLogPath\mylog.log" -Encoding "Default" -Append

# Set a custom variable "startTime" to the current time
$tsenv.Value("startTime") = (Get-Date -Format HH:mm:ss) + ".000+000"

Windows 安装程序答案文件Windows setup answer file

所提供的 Windows 安装程序答案文件可包含嵌入任务序列变量。The Windows setup answer file that you supply can have embedded task sequence variables. 使用窗体 %varname%,其中 varname 是变量的名称。Use the form %varname%, where varname is the name of the variable. “安装 Windows 和 ConfigMgr”步骤将变量名称字符串替换为实际变量值。The Setup Windows and ConfigMgr step replaces the variable name string for the actual variable value. unattend.xml 答案文件的仅数字字段中不能使用这些嵌入的任务序列变量。These embedded task sequence variables can't be used in numeric-only fields in an unattend.xml answer file.

有关详细信息,请参阅安装 Windows 和 ConfigMgrFor more information, see Setup Windows and ConfigMgr.

另请参阅See also