如何在 Configuration Manager 中使用工作順序變數How to use task sequence variables in Configuration Manager

適用於:Configuration Manager (最新分支)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. 此步驟包含一個 [開始位置] 屬性。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 值存取 [開始位置] 屬性值。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.

動作變數的「預設」值會在步驟執行時存在。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.

有些工作順序步驟會將特定動作變數標示為 outputSome 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. 工作順序環境的大小總計限制為 8 KB。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

您的環境中有多個國家/地區的使用者,因此您想要查詢 OS 語言,將其設定為多語言特定 [套用 OS] 步驟的條件。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. 在 [套用 OS] 步驟之前,將 [執行 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-Culture (英文)。For 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. 如需輸出至工作順序變數的選項,請指定 CurrentOSLanguageFor the option to Output to task sequence variable, specify CurrentOSLanguage.

    [執行 PowerShell 指令碼] 步驟範例的螢幕擷取畫面

  4. 在英文版映像的 [套用 OS] 步驟上,建立下列條件: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"

    [套用 OS] 步驟上範例條件的螢幕擷取畫面

    提示

    如需如何在步驟上建立條件的詳細資訊,請參閱如何存取變數 - 步驟條件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. 當英文版映像的 [套用 OS] 步驟在同一部裝置上執行時,條件會評估為 true。When the Apply OS step for the English language image runs on the same device, the condition evaluates to true. 如果您有多個不同語言之 [套用 OS] 步驟的執行個體,則工作順序會以動態方式執行符合 OS 語言的步驟。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. 您將值為 1 的 MyVariable 指派給 ABC 集合。You assign MyVariable to collection ABC with a value of 1. 您同時也將值為 2 的 MyVariable 指派給 XYZ 裝置。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.

提示

工作順序會將套件識別碼和啟動前置命令列寫入到執行 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