

• 2026 年 4 月 30 日之後將不再提供 AWS Systems Manager CloudWatch Dashboard。客戶可以繼續使用 Amazon CloudWatch 主控台來檢視、建立和管理其 Amazon CloudWatch 儀表板，就像現在一樣。如需詳細資訊，請參閱 [Amazon CloudWatch Dashboard 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# AWS Systems Manager Maintenance Windows
<a name="maintenance-windows"></a>

Maintenance Windows是 中的工具 AWS Systems Manager，可協助您定義何時對節點執行潛在破壞性動作的排程，例如修補作業系統、更新驅動程式，或安裝軟體或修補程式。

**注意**  
State Manager 和 Maintenance Windows 可以在受管節點上執行某些類似的更新。您選擇哪一項，取決於您是否需要在指定的期間內自動化系統合規，或執行高優先順序、時間敏感的任務。  
如需詳細資訊，請參閱[在 State Manager 與 Maintenance Windows 之間進行選擇](state-manager-vs-maintenance-windows.md)。

使用 Maintenance Windows，您可以在許多其他 AWS 資源類型上排程動作，例如 Amazon Simple Storage Service (Amazon S3) 儲存貯體、Amazon Simple Queue Service (Amazon SQS) 佇列、 AWS Key Management Service (AWS KMS) 金鑰等。

如需您可以在維護時段目標中包含的支援資源類型完整清單，請參閱*AWS Resource Groups 《 使用者指南*》中的[可與 AWS Resource Groups 和標籤編輯器搭配使用的資源](https://docs.aws.amazon.com/ARG/latest/userguide/supported-resources.html#supported-resources-console)。若要開始使用 Maintenance Windows，請開啟 [Systems Manager 主控台](https://console.aws.amazon.com//systems-manager/maintenance-windows)。在導覽窗格中，選擇 **Maintenance Windows**。

每個維護時段都有排程、最長持續時間、一組已註冊目標 （受管節點或其他處理 AWS 的資源），以及一組已註冊任務。當您建立或更新維護時段時，可以將標籤新增到您的維護時段。(標籤為索引鍵，可幫助識別和排序組織內的資源。) 您也可以指定不得在某日期之前或之後執行維護時段，亦可指定維護時段排程所依據的國際時區。

如需維護時段的各種排程相關選項彼此之間有何關聯的說明，請參閱 [維護時段排程與作用期間選項](maintenance-windows-schedule-options.md)。

如需使用 `--schedule` 選項的詳細資訊，請參閱[參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)。

**受支援任務類型**  
透過維護時段，您可以執行四種類型的任務：
+ Run Command (Systems Manager 中的工具) 中的命令

  如需 Run Command 的相關資訊，請參閱 [AWS Systems Manager Run Command](run-command.md)。
+ Automation (Systems Manager 中的工具) 中的工作流程

  如需自動化工作流程的相關詳細資訊，請參閱 [AWS Systems Manager 自動化](systems-manager-automation.md)。
+ 中的 函數 AWS Lambda

  如需有關 Lambda 函數的資訊，請參閱《AWS Lambda 開發人員指南》**中的 [Lambda 入門](https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html)。
+ 中的任務 AWS Step Functions
**注意**  
維護時段任務僅支援 Step Functions 標準狀態機器工作流程。這些任務不支援快速狀態機器工作流程。如需有關狀態機器工作流程類型的資訊，請參閱《AWS Step Functions 開發人員指南》**中的[標準與快速工作流程](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html)。

  如需 Step Functions 的詳細資訊，請參閱《*[AWS Step Functions 開發人員指南](https://docs.aws.amazon.com/step-functions/latest/dg/)*》。

這表示您可以使用維護時段對所選目標執行類似下列任務。
+ 安裝或更新應用程式。
+ 套用修補程式。
+ 安裝或更新 SSM Agent
+ 藉由使用 Systems Manager Run Command 任務執行 PowerShell 命令和 Linux shell 指令碼。
+ 透過使用 Systems Manager Automation 任務以建置 Amazon Machine Images (AMIs)、啟動軟體和設定節點。
+ 執行叫用其他動作的 AWS Lambda 函數，例如掃描節點以取得修補程式更新。
+ 執行 AWS Step Functions 狀態機器來執行任務，例如從 Elastic Load Balancing 環境移除節點、修補節點，然後將節點新增回 Elastic Load Balancing 環境。
+ 透過指定 AWS 資源群組做為目標，將離線的節點設為目標。

**注意**  
必須為維護時段 Run Command 類型任務指定一或多個目標。視任務而定，其他維護時段任務類型 （自動化 AWS Lambda和 AWS Step Functions) 的目標為選用。如需有關執行未指定目標之任務的詳細資訊，請參閱 [註冊不含目標的維護時段任務](maintenance-windows-targetless-tasks.md)。

**支援 EventBridge**  
此 Systems Manager 工具作為 Amazon EventBridge 規則中的*事件*類型受到支援。如需詳細資訊，請參閱 [使用 Amazon EventBridge 監控 Systems Manager](monitoring-eventbridge-events.md) 及 [參考：Systems Manager 的 Amazon EventBridge 事件模式和類型](reference-eventbridge-events.md)。

**Topics**
+ [設定 Maintenance Windows](setting-up-maintenance-windows.md)
+ [使用主控台建立和管理維護時段](sysman-maintenance-working.md)
+ [教學課程](maintenance-windows-tutorials.md)
+ [註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)
+ [維護時段排程與作用期間選項](maintenance-windows-schedule-options.md)
+ [註冊不含目標的維護時段任務](maintenance-windows-targetless-tasks.md)
+ [對維護時段進行故障診斷](troubleshooting-maintenance-windows.md)

# 設定 Maintenance Windows
<a name="setting-up-maintenance-windows"></a>

您 AWS 帳戶中的使用者必須先取得必要的許可，才能使用 Maintenance Windows (AWS Systems Manager 中的工具) 來建立和排程維護時段任務。此外，您必須建立維護時段的 IAM 服務角色，以及要附加至其中的 IAM 政策。

**開始之前**  
除了在本節中設定的許可，IAM 實體 (將與維護時段搭配使用的使用者、角色或群組) 也應具有了一般維護時段許可。您可以透過將 IAM 政策 `AmazonSSMFullAccess` 指派給實體來授予這些許可，或透過指派自訂 IAM 政策來授予這些許可，而自訂政策能為 Systems Manager 提供較小存取許可集 (許可應涵蓋維護時段任務)。

**Topics**
+ [使用主控台控制對維護時段的存取權](configuring-maintenance-window-permissions-console.md)
+ [使用 控制維護時段的存取 AWS CLI](configuring-maintenance-window-permissions-cli.md)

# 使用主控台控制對維護時段的存取權
<a name="configuring-maintenance-window-permissions-console"></a>

下列程序說明如何使用 AWS Systems Manager 主控台建立維護時段所需的許可和角色。

**Topics**
+ [任務 1：使用主控台為維護時段服務角色建立自訂政策](#create-custom-policy-console)
+ [任務 2：使用主控台為維護時段建立自訂服務角色](#create-custom-role-console)
+ [任務 3：使用主控台向指定使用者授予註冊維護時段任務的許可](#allow-maintenance-window-access-console)
+ [任務 4：使用主控台防止指定的使用者註冊維護時段任務](#deny-maintenance-window-access-console)

## 任務 1：使用主控台為維護時段服務角色建立自訂政策
<a name="create-custom-policy-console"></a>

維護時段任務需要 IAM 角色提供在目標資源上執行所需的許可。許可透過連接到角色的 IAM 政策提供。您執行的任務類型及其他的操作要求決定了此政策的內容。我們提供您可以根據需求調整的基本政策。根據維護時段執行的任務及任務類型，您可能不需要此政策中的所有許可，而您可能需要包含其他的許可。您可以將此政策附加至稍後在 [任務 2：使用主控台為維護時段建立自訂服務角色](#create-custom-role-console) 建立的角色。

**使用主控台建立自訂政策**

1. 在 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 中開啟 IAM 主控台。

1. 在導覽窗格中，選擇 **Policies (政策)**，然後選擇 **Create policy (建立政策)**。

1. 在**政策編輯器**區段中，選擇 **JSON**。

1. 將預設內容取代為以下內容：

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "ssm:SendCommand",
                   "ssm:CancelCommand",
                   "ssm:ListCommands",
                   "ssm:ListCommandInvocations",
                   "ssm:GetCommandInvocation",
                   "ssm:GetAutomationExecution",
                   "ssm:StartAutomationExecution",
                   "ssm:ListTagsForResource",
                   "ssm:DescribeInstanceInformation",
                   "ssm:GetParameters"
               ],
               "Resource": "*"
           },
           {
               "Effect": "Allow",
               "Action": [
                   "states:DescribeExecution",
                   "states:StartExecution"
               ],
               "Resource": [
                   "arn:aws:states:*:*:execution:*:*",
                   "arn:aws:states:*:*:stateMachine:*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "lambda:InvokeFunction"
               ],
               "Resource": [
                   "arn:aws:lambda:*:*:function:*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "resource-groups:ListGroups",
                   "resource-groups:ListGroupResources"
               ],
               "Resource": [
                   "*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "tag:GetResources"
               ],
               "Resource": [
                   "*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": "iam:PassRole",
               "Resource": "arn:aws:iam::111122223333:role/maintenance-window-role-name",
               "Condition": {
                   "StringEquals": {
                       "iam:PassedToService": [
                           "ssm.amazonaws.com"
                       ]
                   }
               }
           }
       ]
   }
   ```

------

1. 根據您在帳户中執行的維護任務及其需要來修改 JSON 內容。您所做的變更專為您的操作而規劃。

   例如：
   + 您可以為特定功能及狀態機器提供 Amazon Resource Names (ARNs)，而不是使用萬用字圓 (\$1) 限定詞。
   + 如果您不打算執行 AWS Step Functions 任務，您可以移除`states`許可 和 ARNs)。
   + 如果您不打算執行 AWS Lambda 任務，您可以移除`lambda`許可和 ARNs。
   + 如果您不打算執行自動化任務，您可以移除 `ssm:GetAutomationExecution` 及 `ssm:StartAutomationExecution` 許可。
   + 新增執行任務可能需要的其他許可。例如，有些自動化動作搭配 AWS CloudFormation 堆疊運作。因此，`cloudformation:CreateStack`、`cloudformation:DescribeStacks` 以及 `cloudformation:DeleteStack` 許可是必要的。

     另一個例子：Automation Runbook `AWS-CopySnapshot` 需建立 Amazon Elastic Block Store (Amazon EBS) 快照的權限。因此，服務角色需要 `ec2:CreateSnapshot` 許可。

     如需有關 Automation 執行手冊所需的角色權限的資訊，請參閱 [AWS Systems Manager Automation Runbook Reference](https://docs.aws.amazon.com/systems-manager-automation-runbooks/latest/userguide/automation-runbook-reference.html) 中的執行手冊描述。

1. 完成政策修訂後，選擇**下一步**。

1. 在**政策名稱**中輸入名稱，該名稱將政策標識為連接至所建立之服務角色的政策。例如：**my-maintenance-window-role-policy**。

1. (選用) 在**新增標籤**區段中，新增一個或多個標籤鍵值對來組織、追蹤或控制對此政策的存取。

1. 選擇**建立政策**。

   記下為政策指定的名稱。您可以在接下來的程序 [任務 2：使用主控台為維護時段建立自訂服務角色](#create-custom-role-console) 加以引用。

## 任務 2：使用主控台為維護時段建立自訂服務角色
<a name="create-custom-role-console"></a>

在先前任務中建立的政策會連接至您在此任務中建立的維護時段服務角色。使用者註冊維護時段任務時，他們將此 IAM 角色指定為任務組態的一部分。此角色的權限可讓 Systems Manager 代您在維護時段執行任務。

**重要**  
先前，Systems Manager 主控台可讓您選擇要`AWSServiceRoleForAmazonSSM`用作任務維護角色的 AWS 受管 IAM 服務連結角色。不再建議將此角色及其關聯政策 `AmazonSSMServiceRolePolicy`，用於維護時段任務。如果您現在將此角色用於維護時段任務，我們建議您停止使用。相反地，請建立您自己的 IAM 角色，以便在維護時段任務執行 AWS 服務 時啟用 Systems Manager 與其他 之間的通訊。

請使用下列步驟來為 Maintenance Windows 建立自訂服務角色，讓 Systems Manager 能代表您執行 Maintenance Windows 任務。您需要將在前一任務中建立的政策連接至所建立的自訂服務角色。

**使用主控台為維護時段建立自訂服務角色**

1. 在以下網址開啟 IAM 主控台：[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)。

1. 在導覽窗格中，選擇**角色**，然後選擇**建立角色**。

1. 對於 **Select trusted entity** (選擇信任的實體)，請執行以下選項：

   1. 針對**信任的實體類型**，請選擇 **AWS 服務**。

   1. 對於**使用案例**，選擇 **Systems Manager**

   1. 選擇 **Systems Manager**。

      下圖反白顯示了 Systems Manager 選項的位置。  
![\[Systems Manager 是使用案例的選項之一。\]](http://docs.aws.amazon.com/zh_tw/systems-manager/latest/userguide/images/iam_use_cases_for_MWs.png)

1. 選擇**下一步**。

1. 在**許可政策**區域的搜尋方塊中，輸入在 [任務 1：使用主控台為維護時段服務角色建立自訂政策](#create-custom-policy-console) 中建立的政策名稱，選取其名稱旁邊的方塊，然後選擇**下一步**。

1. 在 **Role name** (角色名稱) 中，輸入識別此角色為 Maintenance Windows 角色的名稱。例如：**my-maintenance-window-role**。

1. (選用) 變更預設的角色描述以反映此角色的用途。例如：**Performs maintenance window tasks on your behalf**。

1. 對於**步驟 1：選取信任的實體**，確認下列政策顯示在**信任的政策**方塊中。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Sid": "",
         "Effect": "Allow",
         "Principal": {
           "Service": "ssm.amazonaws.com"
         },
         "Action": "sts:AssumeRole"
       }
     ]
   }
   ```

------

1. 對於**步驟 2：新增許可**，確認您在 [任務 1：使用主控台為維護時段服務角色建立自訂政策](#create-custom-policy-console) 中建立的政策存在。

1. (選用) 在**步驟 3：新增標籤**中，新增一個或多個標籤鍵值對來組織、追蹤或控制對此角色的存取。

1. 選擇 **Create role** (建立角色)。系統會讓您回到 **Roles (角色)** 頁面。

1. 選擇剛建立之 IAM 角色的名稱。

1. 複製或記下角色名稱及**摘要**區的 **ARN** 值。您帳户的使用者在建立維護時段時指定此訊息。

## 任務 3：使用主控台向指定使用者授予註冊維護時段任務的許可
<a name="allow-maintenance-window-access-console"></a>

為使用者提供存取維護時段自訂服務角色的許可，可讓他們將其用於維護時段任務。這是對您已經授予他們將 Systems Manager API 命令用於 Maintenance Windows 工具的許可的補充。此 IAM 角色傳遞了執行維護時段任務所需的許可。因此，如果無法傳遞這些 IAM 許可，使用者就無法使用您的自訂服務角色向維護時段註冊任務。

在向維護時段註冊任務時，需要指定服務角色來執行實際的任務操作。這是服務代您執行任務時所擔任的角色。在此之前，若要註冊任務本身，請將 IAM `PassRole` 政策指派給 IAM 實體 (例如使用者或群組)。這樣便讓 IAM 實體可以指定執行任務時應使用的角色，而指定角色是向維護時段註冊這些任務的其中一步。如需相關資訊，請參閱《IAM 使用者指南》**中的 [Grant a user permissions to pass a role to an AWS 服務](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html)。

**設定許可以允許使用者註冊維護時段任務**

如果 IAM 實體 (使用者、角色或群組) 設定為具有管理員許可，則該 IAM 使用者或角色可以存取維護時段。對於沒有管理員許可的 IAM 實體，管理員必須將以下許可授予給 IAM 實體。以下是在維護時段中註冊任務所需的最低許可：
+ `AmazonSSMFullAccess` 受管政策或提供相當許可的政策。
+ 以下是 `iam:PassRole` 和 `iam:ListRoles` 許可。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": "iam:PassRole",
              "Resource": "arn:aws:iam::111122223333:role/my-maintenance-window-role"
          },
          {
              "Effect": "Allow",
              "Action": "iam:ListRoles",
              "Resource": "arn:aws:iam::111122223333:role/"
          },
          {
              "Effect": "Allow",
              "Action": "iam:ListRoles",
              "Resource": "arn:aws:iam::111122223333:role/aws-service-role/ssm.amazonaws.com/"
          }
      ]
  }
  ```

------

  *my-maintenance-window-role* 代表您先前建立之自訂維護時段服務角色的名稱。

  *account-id* 代表您的 AWS 帳戶 ID。新增此資源 `arn:aws:iam::account-id:role/` 的許可，可讓使用者在建立維護時段任務時，檢視主控台中的客戶角色，並從中選擇客戶角色。新增 `arn:aws:iam::account-id:role/aws-service-role/ssm.amazonaws.com/` 的許可，可讓使用者在建立維護時段任務時，在主控台中選擇 Systems Manager 服務連結的角色。

  若要提供存取權，請新增權限至您的使用者、群組或角色：
  + 中的使用者和群組 AWS IAM Identity Center：

    建立權限合集。請按照《*AWS IAM Identity Center 使用者指南*》中的[建立權限合集](https://docs.aws.amazon.com//singlesignon/latest/userguide/howtocreatepermissionset.html)說明進行操作。
  + 透過身分提供者在 IAM 中管理的使用者：

    建立聯合身分的角色。遵循《*IAM 使用者指南*》的[為第三方身分提供者 (聯合) 建立角色](https://docs.aws.amazon.com//IAM/latest/UserGuide/id_roles_create_for-idp.html)中的指示。
  + IAM 使用者：
    + 建立您的使用者可擔任的角色。請按照《*IAM 使用者指南*》的[為 IAM 使用者建立角色](https://docs.aws.amazon.com//IAM/latest/UserGuide/id_roles_create_for-user.html)中的指示。
    + (不建議) 將政策直接附加至使用者，或將使用者新增至使用者群組。請遵循《*IAM 使用者指南*》的[新增許可到使用者 (主控台)](https://docs.aws.amazon.com//IAM/latest/UserGuide/id_users_change-permissions.html#users_change_permissions-add-console) 中的指示。

**使用主控台為可以註冊維護時段任務的群組設定許可**

1. 前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 開啟 IAM 主控台。

1. 在導覽窗格中，選擇 **User groups** (使用者群組)。

1. 在群組清單中，選取要為其指派 `iam:PassRole` 許可的群組的名稱，或先建立新的群組 (如有必要) 

1. 在 **Permissions** (許可) 索引標籤上，選擇 **Add permissions, Create inline policy** (新增許可、建立內嵌政策)。

1. 在**政策編輯器**區域中，選擇 **JSON** 並使用列內容取代方塊中的預設內容。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": "iam:PassRole",
               "Resource": "arn:aws:iam::111122223333:role/my-maintenance-window-role"
           },
           {
               "Effect": "Allow",
               "Action": "iam:ListRoles",
               "Resource": "arn:aws:iam::111122223333:role/"
           },
           {
               "Effect": "Allow",
               "Action": "iam:ListRoles",
               "Resource": "arn:aws:iam::111122223333:role/aws-service-role/ssm.amazonaws.com/"
           }
       ]
   }
   ```

------

   *my-maintenance-window-role* 代表您先前建立之自訂維護時段角色的名稱。

   *account-id* 代表您的 AWS 帳戶 ID。新增此資源 `arn:aws:iam::account-id:role/` 的許可，可讓使用者在建立維護時段任務時，檢視主控台中的客戶角色，並從中選擇客戶角色。新增 `arn:aws:iam::account-id:role/aws-service-role/ssm.amazonaws.com/` 的許可，可讓使用者在建立維護時段任務時，在主控台中選擇 Systems Manager 服務連結的角色。

1. 選擇**下一步**。

1. 在**檢閱並建立**頁面上，在**政策名稱**方塊中輸入名稱以識別此 `PassRole` 政策 (例如 **my-group-iam-passrole-policy**)，然後選擇**建立政策**。

## 任務 4：使用主控台防止指定的使用者註冊維護時段任務
<a name="deny-maintenance-window-access-console"></a>

您可以拒絕 AWS 帳戶 您不想向維護時段註冊任務之 中使用者的`ssm:RegisterTaskWithMaintenanceWindow`許可。這針對不應註冊維護時段任務的使用者提供了額外的保護層。

**使用主控台為無法註冊維護時段任務的群組設定許可**

1. 前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 開啟 IAM 主控台。

1. 在導覽窗格中，選擇 **User groups** (使用者群組)。

1. 在群組清單中，選取要為其拒絕 `ssm:RegisterTaskWithMaintenanceWindow` 許可的群組名稱，或先建立新的群組 (如有必要)。

1. 在 **Permissions** (許可) 索引標籤上，選擇 **Add permissions, Create inline policy** (新增許可、建立內嵌政策)。

1. 在**政策編輯器**區域中，選擇 **JSON**，然後使用下列內容取代方塊中的預設內容。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Deny",
               "Action": "ssm:RegisterTaskWithMaintenanceWindow",
               "Resource": "*"
           }
       ]
   }
   ```

------

1. 選擇**下一步**。

1. 在**檢閱和建立**頁面上，在**政策名稱**中輸入名稱以識別此政策 (例如 **my-groups-deny-mw-tasks-policy**)，然後選擇**建立政策**。

# 使用 控制維護時段的存取 AWS CLI
<a name="configuring-maintenance-window-permissions-cli"></a>

下列程序說明如何使用 AWS Command Line Interface (AWS CLI) 為 中的Maintenance Windows工具 建立所需的許可和角色 AWS Systems Manager。

**Topics**
+ [任務 1：建立 JSON 格式的信任政策和客戶管理政策檔案](#create-custom-policy-json-files-cli)
+ [任務 2：使用 建立和驗證維護時段的自訂服務角色 AWS CLI](#create-custom-role-cli)
+ [任務 3：授予指定使用者使用 註冊維護時段任務的許可 AWS CLI](#allow-maintenance-window-access-cli)
+ [任務 4：防止指定的使用者使用 註冊維護時段任務 AWS CLI](#deny-maintenance-window-access-cli)

## 任務 1：建立 JSON 格式的信任政策和客戶管理政策檔案
<a name="create-custom-policy-json-files-cli"></a>

維護時段任務需要 IAM 角色提供在目標資源上執行所需的許可。許可透過連接到角色的 IAM 政策提供。您執行的任務類型及其他的操作要求決定了此政策的內容。我們提供您可以根據需求調整的基本政策。根據維護時段執行的任務及任務類型，您可能不需要此政策中的所有許可，而您可能需要包含其他的許可。

在此任務中，您在一組 JSON 檔案中指定自訂維護時段角色所需的許可。您可以將此政策附加至稍後在 [任務 2：使用 建立和驗證維護時段的自訂服務角色 AWS CLI](#create-custom-role-cli) 建立的角色。

**建立信任政策和客戶管理政策檔案**

1. 將下列信任政策複製並貼入一個文字檔。使用以下名稱和副檔名儲存此檔案：**mw-role-trust-policy.json**。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": {
                   "Service": "ssm.amazonaws.com"
               },
               "Action": "sts:AssumeRole"
           }
       ]
   }
   ```

------

1. 複製下列 JSON 政策並將其貼到另一個文字檔案中。在您建立第一個檔案的相同目錄中，使用下列名稱和副檔名儲存此檔案：**mw-role-custom-policy.json**。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "ssm:SendCommand",
                   "ssm:CancelCommand",
                   "ssm:ListCommands",
                   "ssm:ListCommandInvocations",
                   "ssm:GetCommandInvocation",
                   "ssm:GetAutomationExecution",
                   "ssm:StartAutomationExecution",
                   "ssm:ListTagsForResource",
                   "ssm:GetParameters"
               ],
               "Resource": "*"
           },
           {
               "Effect": "Allow",
               "Action": [
                   "states:DescribeExecution",
                   "states:StartExecution"
               ],
               "Resource": [
                   "arn:aws:states:*:*:execution:*:*",
                   "arn:aws:states:*:*:stateMachine:*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "lambda:InvokeFunction"
               ],
               "Resource": [
                   "arn:aws:lambda:*:*:function:*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "resource-groups:ListGroups",
                   "resource-groups:ListGroupResources"
               ],
               "Resource": [
                   "*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "tag:GetResources"
               ],
               "Resource": [
                   "*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": "iam:PassRole",
               "Resource": "arn:aws:iam::111122223333:role/maintenance-window-role-name",
               "Condition": {
                   "StringEquals": {
                       "iam:PassedToService": [
                           "ssm.amazonaws.com"
                       ]
                   }
               }
           }
       ]
   }
   ```

------

1. 根據您在帳户中執行的維護任務需求來修改 `mw-role-custom-policy.json` 的內容。您所做的變更專為您的操作而規劃。

   例如：
   + 您可以為特定功能及狀態機器提供 Amazon Resource Names (ARNs)，而不是使用萬用字圓 (\$1) 限定詞。
   + 如果您不打算執行 AWS Step Functions 任務，您可以移除`states`許可 和 (ARNs)。
   + 如果您不打算執行 AWS Lambda 任務，您可以移除`lambda`許可和 ARNs。
   + 如果您不打算執行自動化任務，您可以移除 `ssm:GetAutomationExecution` 及 `ssm:StartAutomationExecution` 許可。
   + 新增執行任務可能需要的其他許可。例如，有些自動化動作搭配 AWS CloudFormation 堆疊運作。因此，`cloudformation:CreateStack`、`cloudformation:DescribeStacks` 以及 `cloudformation:DeleteStack` 許可是必要的。

     另一個例子：Automation Runbook `AWS-CopySnapshot` 需建立 Amazon Elastic Block Store (Amazon EBS) 快照的權限。因此，服務角色需要 `ec2:CreateSnapshot` 許可。

     如需有關 Automation 執行手冊所需的角色權限的資訊，請參閱 [AWS Systems Manager Automation Runbook Reference](https://docs.aws.amazon.com/systems-manager-automation-runbooks/latest/userguide/automation-runbook-reference.html) 中的執行手冊描述。

   進行任何所需變更後，再次儲存檔案。

## 任務 2：使用 建立和驗證維護時段的自訂服務角色 AWS CLI
<a name="create-custom-role-cli"></a>

在先前任務中建立的政策會連接至您在此任務中建立的維護時段服務角色。使用者註冊維護時段任務時，他們將此 IAM 角色指定為任務組態的一部分。此角色的權限可讓 Systems Manager 代您在維護時段執行任務。

**重要**  
先前，Systems Manager 主控台可讓您選擇要`AWSServiceRoleForAmazonSSM`用作任務維護角色的 AWS 受管 IAM 服務連結角色。不再建議將此角色及其關聯政策 `AmazonSSMServiceRolePolicy`，用於維護時段任務。如果您現在將此角色用於維護時段任務，我們建議您停止使用。相反地，請建立您自己的 IAM 角色，以便在維護時段任務執行 AWS 服務 時啟用 Systems Manager 與其他 之間的通訊。

在此任務中，您需執行 CLI 命令來建立維護時段服務角色，並從您建立的 JSON 檔案新增政策內容。

**使用 建立維護時段的自訂服務角色 AWS CLI**

1. 開啟 AWS CLI ，並在您放置 `mw-role-custom-policy.json` 和 的目錄中執行下列命令`mw-role-trust-policy.json`。該命令會建立名為 `my-maintenance-window-role` 的維護時段服務角色，並將*信任政策*連接至該角色。

------
#### [ Linux & macOS ]

   ```
   aws iam create-role \
       --role-name "my-maintenance-window-role" \
       --assume-role-policy-document file://mw-role-trust-policy.json
   ```

------
#### [ Windows ]

   ```
   aws iam create-role ^
       --role-name "my-maintenance-window-role" ^
       --assume-role-policy-document file://mw-role-trust-policy.json
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "Role": {
           "AssumeRolePolicyDocument": {
               "Version": "2012-10-17", 		 	 	 		 	 	 
               "Statement": [
                   {
                       "Action": "sts:AssumeRole",
                       "Effect": "Allow",
                       "Principal": {
                           "Service": "ssm.amazonaws.com"
                       }
                   }
               ]
           },
           "RoleId": "AROAIIZKPBKS2LEXAMPLE",
           "CreateDate": "2024-08-19T03:40:17.373Z",
           "RoleName": "my-maintenance-window-role",
           "Path": "/",
           "Arn": "arn:aws:iam::123456789012:role/my-maintenance-window-role"
       }
   }
   ```
**注意**  
記下 `RoleName` 與 `Arn` 值。它們可以包含在下一個命令中。

1. 執行下列命令，將*客戶管理政策*連接至該角色。使用自己的 AWS 帳戶 ID 取代 *account-id* 預留位置

------
#### [ Linux & macOS ]

   ```
   aws iam attach-role-policy \
       --role-name "my-maintenance-window-role" \
       --policy-arn "arn:aws:iam::account-id:policy/mw-role-custom-policy.json"
   ```

------
#### [ Windows ]

   ```
   aws iam attach-role-policy ^
       --role-name "my-maintenance-window-role" ^
       --policy-arn "arn:aws:iam::account-id:policy/mw-role-custom-policy.json"
   ```

------

1. 執行下列命令，確認角色已建立，並且已連接信任政策。

   ```
   aws iam get-role --role-name my-maintenance-window-role
   ```

   此命令會傳回與以下內容相似的資訊：

   ```
   {
       "Role": {
           "Path": "/",
           "RoleName": "my-maintenance-window-role",
           "RoleId": "AROA123456789EXAMPLE",
           "Arn": "arn:aws:iam::123456789012:role/my-maintenance-window-role",
           "CreateDate": "2024-08-19T14:13:32+00:00",
           "AssumeRolePolicyDocument": {
               "Version": "2012-10-17", 		 	 	 		 	 	 
               "Statement": [
                   {
                       "Effect": "Allow",
                       "Principal": {
                           "Service": "ssm.amazonaws.com"
                       },
                       "Action": "sts:AssumeRole"
                   }
               ]
           },
           "MaxSessionDuration": 3600,
           "RoleLastUsed": {
               "LastUsedDate": "2024-08-19T14:30:44+00:00",
               "Region": "us-east-2"
           }
       }
   }
   ```

1. 執行下列命令，確認客戶管理政策已連接至角色。

   ```
   aws iam list-attached-role-policies --role-name my-maintenance-window-role
   ```

   此命令會傳回與以下內容相似的資訊：

   ```
   {
       "AttachedPolicies": [
           {
               "PolicyName": "mw-role-custom-policy",
               "PolicyArn": "arn:aws:iam::123456789012:policy/mw-role-custom-policy"
           }
       ]
   }
   ```

## 任務 3：授予指定使用者使用 註冊維護時段任務的許可 AWS CLI
<a name="allow-maintenance-window-access-cli"></a>

為使用者提供存取維護時段自訂服務角色的許可，可讓他們將其用於維護時段任務。這是對您已經授予他們將 Systems Manager API 命令用於 Maintenance Windows 工具的許可的補充。此 IAM 角色傳遞了執行維護時段任務所需的許可。因此，如果無法傳遞這些 IAM 許可，使用者就無法使用您的自訂服務角色向維護時段註冊任務。

在向維護時段註冊任務時，需要指定服務角色來執行實際的任務操作。這是服務代您執行任務時所擔任的角色。在此之前，若要註冊任務本身，請將 IAM `PassRole` 政策指派給 IAM 實體 (例如使用者或群組)。這樣便讓 IAM 實體可以指定執行任務時應使用的角色，而指定角色是向維護時段註冊這些任務的其中一步。如需相關資訊，請參閱《IAM 使用者指南》**中的 [Grant a user permissions to pass a role to an AWS 服務](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html)。

**為允許使用者使用 註冊維護時段任務的使用者設定許可 AWS CLI**

1. 將下列 AWS Identity and Access Management (IAM) 政策複製並貼到文字編輯器中，並使用下列名稱和副檔名儲存：`mw-passrole-policy.json`。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": "iam:PassRole",
               "Resource": "arn:aws:iam::111122223333:role/my-maintenance-window-role"
           },
           {
               "Effect": "Allow",
               "Action": "iam:ListRoles",
               "Resource": "arn:aws:iam::111122223333:role/"
           },
           {
               "Effect": "Allow",
               "Action": "iam:ListRoles",
               "Resource": "arn:aws:iam::111122223333:role/aws-service-role/ssm.amazonaws.com/"
           }
       ]
   }
   ```

------

   使用您先前建立之自訂維護時段角色的名稱取代 *my-maintenance-window-role*。

   將 *account-id* 取代為 AWS 帳戶的 ID。新增此資源 `arn:aws:iam::account-id:role/` 的許可，可讓群組中的使用者在建立維護時段任務時，檢視主控台中的客戶角色，並從中選擇客戶角色。新增此 `arn:aws:iam::account-id:role/aws-service-role/ssm.amazonaws.com/` 許可，可讓群組中的使用者在建立維護時段任務時，在主控台中選擇 Systems Manager 服務連結的角色。

1. 開啟 AWS CLI。

1. 根據您是否將許可指派給 IAM 實體 (使用者或群組)，執行下列其中一個命令。
   + **對於 IAM 實體：**

------
#### [ Linux & macOS ]

     ```
     aws iam put-user-policy \
         --user-name "user-name" \
         --policy-name "policy-name" \
         --policy-document file://path-to-document
     ```

------
#### [ Windows ]

     ```
     aws iam put-user-policy ^
         --user-name "user-name" ^
         --policy-name "policy-name" ^
         --policy-document file://path-to-document
     ```

------

     對於 *user-name*，指定要將任務指派給維護時段的使用者。對於 *policy-name*，指定您要用於識別該政策的名稱，例如 **my-iam-passrole-policy**。對於 *path-to-document*，指定您儲存在步驟 1 的檔案路徑。例如：`file://C:\Temp\mw-passrole-policy.json`
**注意**  
若要授予使用者使用 Systems Manager 主控台為維護時段註冊任務的存取權，您還必須將 `AmazonSSMFullAccess` 政策指派給使用者 (或 IAM 政策，該政策為涵蓋維護時段任務的 Systems Manager 提供較小的存取許可集)。執行以下命令，將 `AmazonSSMFullAccess` 政策指派給使用者。  

     ```
     aws iam attach-user-policy \
         --policy-arn "arn:aws:iam::aws:policy/AmazonSSMFullAccess" \
         --user-name "user-name"
     ```

     ```
     aws iam attach-user-policy ^
         --policy-arn "arn:aws:iam::aws:policy/AmazonSSMFullAccess" ^
         --user-name "user-name"
     ```
   + **對於 IAM 群組：**

------
#### [ Linux & macOS ]

     ```
     aws iam put-group-policy \
         --group-name "group-name" \
         --policy-name "policy-name" \
         --policy-document file://path-to-document
     ```

------
#### [ Windows ]

     ```
     aws iam put-group-policy ^
         --group-name "group-name" ^
         --policy-name "policy-name" ^
         --policy-document file://path-to-document
     ```

------

     對於 *group-name*，指定其成員會將任務指派給維護時段的群組。對於 *policy-name*，指定您要用於識別該政策的名稱，例如 **my-iam-passrole-policy**。對於 *path-to-document*，指定您儲存在步驟 1 的檔案路徑。例如：`file://C:\Temp\mw-passrole-policy.json`
**注意**  
若要授與群組成員使用 Systems Manager 主控台為維護時段註冊任務的存取權限，您還必須將 `AmazonSSMFullAccess` 政策指派給群組。執行以下命令，將此政策指派到您的群組。  

     ```
     aws iam attach-group-policy \
         --policy-arn "arn:aws:iam::aws:policy/AmazonSSMFullAccess" \
         --group-name "group-name"
     ```

     ```
     aws iam attach-group-policy ^
         --policy-arn "arn:aws:iam::aws:policy/AmazonSSMFullAccess" ^
         --group-name "group-name"
     ```

1. 執行以下命令，確認已經將政策指派到群組。

------
#### [ Linux & macOS ]

   ```
   aws iam list-group-policies \
       --group-name "group-name"
   ```

------
#### [ Windows ]

   ```
   aws iam list-group-policies ^
       --group-name "group-name"
   ```

------

## 任務 4：防止指定的使用者使用 註冊維護時段任務 AWS CLI
<a name="deny-maintenance-window-access-cli"></a>

您可以拒絕 AWS 帳戶 您不想向維護時段註冊任務之 中使用者的`ssm:RegisterTaskWithMaintenanceWindow`許可。這針對不應註冊維護時段任務的使用者提供了額外的保護層。

無論您是根據個別使用者還是群組來拒絕 `ssm:RegisterTaskWithMaintenanceWindow` 許可，請使用下列其中一個處理程序，來阻止使用者向維護時段註冊任務。

**為不允許使用 註冊維護時段任務的使用者設定許可 AWS CLI**

1. 下列 IAM 政策複製並貼到文字編輯器，並以下名稱和副檔名儲存：**deny-mw-tasks-policy.json**。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Deny",
               "Action": "ssm:RegisterTaskWithMaintenanceWindow",
               "Resource": "*"
           }
       ]
   }
   ```

------

1. 開啟 AWS CLI。

1. 根據您是否將許可指派給 IAM 實體 (使用者或群組)，執行下列其中一個命令。
   + **對於使用者：**

------
#### [ Linux & macOS ]

     ```
     aws iam put-user-policy \
         --user-name "user-name" \
         --policy-name "policy-name" \
         --policy-document file://path-to-document
     ```

------
#### [ Windows ]

     ```
     aws iam put-user-policy ^
         --user-name "user-name" ^
         --policy-name "policy-name" ^
         --policy-document file://path-to-document
     ```

------

     對於 *user-name*，指定要防止其將任務指派給維護時段的使用者。對於 *policy-name*，指定您要用於識別該政策的名稱，例如 **my-deny-mw-tasks-policy**。對於 *path-to-document*，指定您儲存在步驟 1 的檔案路徑。例如：`file://C:\Temp\deny-mw-tasks-policy.json`
   + **對於群組：**

------
#### [ Linux & macOS ]

     ```
     aws iam put-group-policy \
         --group-name "group-name" \
         --policy-name "policy-name" \
         --policy-document file://path-to-document
     ```

------
#### [ Windows ]

     ```
     aws iam put-group-policy ^
         --group-name "group-name" ^
         --policy-name "policy-name" ^
         --policy-document file://path-to-document
     ```

------

     對於 *group-name*，指定要防止其成員將任務指派給維護時段的群組。對於 *policy-name*，指定您要用於識別該政策的名稱，例如 **my-deny-mw-tasks-policy**。對於 *path-to-document*，指定您儲存在步驟 1 的檔案路徑。例如：`file://C:\Temp\deny-mw-tasks-policy.json`

1. 執行以下命令，確認已經將政策指派到群組。

------
#### [ Linux & macOS ]

   ```
   aws iam list-group-policies \
       --group-name "group-name"
   ```

------
#### [ Windows ]

   ```
   aws iam list-group-policies ^
       --group-name "group-name"
   ```

------

# 使用主控台建立和管理維護時段
<a name="sysman-maintenance-working"></a>

本節說明如何使用 AWS Systems Manager 主控台建立、設定、更新和刪除維護時段。本節也提供管理維護時段的目標和任務的資訊。

**重要**  
我們建議您一開始先在測試環境中建立和設定維護時段。

**開始之前**  
在建立維護時段之前，您必須設定對 的存取權Maintenance Windows，這是 中的工具 AWS Systems Manager。如需詳細資訊，請參閱[設定 Maintenance Windows](setting-up-maintenance-windows.md)。

**Topics**
+ [使用主控台建立維護時段](sysman-maintenance-create-mw.md)
+ [使用主控台將目標指派至維護時段](sysman-maintenance-assign-targets.md)
+ [使用主控台將任務指派至維護時段](sysman-maintenance-assign-tasks.md)
+ [使用主控台停用或啟用維護時段](sysman-maintenance-disable.md)
+ [使用主控台更新或刪除維護時段資源](sysman-maintenance-update.md)

# 使用主控台建立維護時段
<a name="sysman-maintenance-create-mw"></a>

在此程序中，您會在 Maintenance Windows ( AWS Systems Manager中的工具) 中建立維護時段。您可以指定其基本選項，例如名稱、排程和持續時間。在後續步驟中，您可以選擇要更新的目標或資源，以及在維護時段執行時執行的任務。

**注意**  
如需維護時段的各種排程相關選項彼此之間有何關聯的說明，請參閱 [維護時段排程與作用期間選項](maintenance-windows-schedule-options.md)。  
如需使用 `--schedule` 選項的詳細資訊，請參閱[參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)。

**使用主控台建立維護時段**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 選擇**建立維護時段**。

1. 對於 **Name** (名稱)，輸入描述名稱，以協助您識別此維護時段。

1. (選用) 在 **Description** (描述) 中，輸入描述來確定如何使用此維護時段。

1. (選用) 如果您想允許維護時段任務在受管節點上執行 (即使尚未將這些節點註冊為目標)，請選擇 **Allow unregistered targets** (允許未註冊的目標)。

   如果您選擇此選項，即可在向維護時段註冊任務時選擇未註冊的節點 (依據節點 ID)。

   如果您未選擇此選項，則必須在向維護時段註冊任務時選擇先前註冊過的目標。

1. 使用三個排程選項的其中一個，來為維護時段指定排程。

   如需有關建立 Cron/Rate 運算式的詳細資訊，請參閱[參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)。

1. 針對 **Duration (持續時間)**，輸入維護時段將執行的時數。您指定的值會根據維護時段的開始時間，決定維護時段的特定結束時間。在產生的結束時間減去您在下一個步驟中為 **Stop initiating tasks (停止啟動任務)** 指定的小時數過後，將不允許啟動任何維護時段任務。

   例如，如果維護時段從下午 3 點開始，持續時間為三小時，而 **Stop initiating tasks (停止啟動任務)** 的值為一小時，則在下午 5 點之後無法啟動任何維護時段任務。

1. 針對 **Stop initiating tasks (停止初始任務)**，輸入在維護時段執行結束之前，系統應該停止排程新任務的時數。

1. (選用) 對於 **Window start date** (時段開始日期)，依照 ISO-8601 Extended 格式，指定您希望開始啟用維護時段的日期和時間。這可讓您延遲啟用維護時段，直到指定的未來日期為止。
**注意**  
您無法指定過去的開始日期和時間。

1. (選用) 對於 **Window end date** (時段結束日期)，依照 ISO-8601 Extended 格式，指定您希望停用維護時段的日期和時間。這可讓您設定不再執行維護時段的未來日期和時間點。

1. (選用) 對於**排程時區**，依照網際網路號碼分配局 (IANA) 格式，指定排程的維護時段執行時所依據的時區。例如："America/Los\$1Angeles"、"etc/UTC" 或 "Asia/Seoul"。

   如需有關有效格式的詳細資訊，請參閱 IANA 網站上的[時區資料庫有效格式](https://www.iana.org/time-zones)。

1. (選用) 對於 **Schedule offset** (排程偏移)，請輸入在執行維護時段之前，在 Cron 或 Rate 表達式所指定的日期和時間之後等待的天數。您可以指定一至六天。
**注意**  
僅當您透過手動輸入 Cron 或 Rate 表達式指定排程時，此選項才可用。

1. (選用) 在 **Manage tags (管理標籤)** 區域，將一或多個標籤金鑰名稱/值對套用到維護時段。

   標籤是您指派給資源的選用性中繼資料。標籤允許您以不同的方式 (例如用途、擁有者或環境) 將資源分類。例如，您可能想要標記維護時段來識別其執行的任務類型、目標類型以及其執行所在的環境。在這種情況下，您可以指定以下索引鍵名稱/值對：
   + `Key=TaskType,Value=AgentUpdate`
   + `Key=OS,Value=Windows`
   + `Key=Environment,Value=Production`

1. 選擇**建立維護時段**。系統會帶您回到維護時段頁面。您剛建立的維護時段為 **Enabled (已啟用)** 狀態。

# 使用主控台將目標指派至維護時段
<a name="sysman-maintenance-assign-targets"></a>

在此程序中，您會向維護時段註冊目標。換言之，您會指定維護時段要對哪些資源執行動作。

**注意**  
如果單一維護時段任務已向多個目標註冊，則其任務叫用會依序發生，而非平行發生。如果您的任務必須同時在多個目標上執行，請個別註冊每個目標的任務，並為每個任務指派相同的優先順序層級。

**使用主控台將目標指派至維護時段**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 在維護時段清單中，選擇您要新增目標的維護時段。

1. 選擇 **Actions (動作)**，然後選擇 **Register targets (註冊目標)**。

1. (選用) 在 **Target Name (目標名稱)** 中，輸入目標的名稱。

1. 在**描述**，請輸入描述。

1. (選用) 對於 **Owner information** (擁有者資訊)，指定要在此維護時段為這些目標執行任務時引發的任何 Amazon EventBridge 事件中包含的資訊。

   如需使用 EventBridge 監控 Systems Manager 事件的相關資訊，請參閱 [使用 Amazon EventBridge 監控 Systems Manager](monitoring-eventbridge-events.md)。

1. 在 **Targets (目標)** 區域，選擇下表中所述的其中一個選項。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/systems-manager/latest/userguide/sysman-maintenance-assign-targets.html)

1. 選擇 **Register target (註冊目標)**。

如果您想要將多個目標指派到這個維護時段，選擇 **Targets (目標)** 標籤，然後選擇 **Register target (註冊目標)**。使用此選項，您可以選擇不同設定目標的方法。例如，如果您之前透過節點 ID 來將節點作為目標，您可以透過指定受管節點中套用的標籤，或從資源群組中選擇資源類型來註冊新目標並將節點作為目標。

# 使用主控台將任務指派至維護時段
<a name="sysman-maintenance-assign-tasks"></a>

在此程序中，您會將任務新增到維護時段。任務是在維護時段執行時所執行的動作。

您可將下列四種類型的任務新增到維護時段：
+ AWS Systems Manager Run Command 命令
+ Systems Manager Automation 工作流程
+ AWS Step Functions 任務
+ AWS Lambda 函數
**重要**  
適用於 Maintenance Windows 的 IAM 政策需要您為 Lambda 函數 (或別名) 名稱新增 `SSM` 字首。在您繼續註冊此類任務之前，請在 中更新其名稱 AWS Lambda 以包含 `SSM`。例如，如果 Lambda 函數名稱為 `MyLambdaFunction`，請變更為 `SSMMyLambdaFunction`。

**指派任務至維護時段**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 在維護時段清單中，選擇維護時段。

1. 選擇 **Actions** (動作)，然後選擇您想要向維護時段註冊的任務類型選項：
   + **Register Run command task (註冊執行命令任務)**
   + **Register Automation task (註冊自動化任務)**
   + **Register Lambda task (註冊 Lambda 任務)**
   + **Register Step Functions task (註冊步驟函數任務)**
**注意**  
維護時段任務僅支援 Step Functions 標準狀態機器工作流程。這些任務不支援快速狀態機器工作流程。如需有關狀態機器工作流程類型的資訊，請參閱《AWS Step Functions 開發人員指南》**中的[標準與快速工作流程](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html)。

1. (選用) 對於 **Name** (名稱)，請輸入任務的名稱。

1. 在**描述**，請輸入描述。

1. 對於 **New task invocation cutoff** (新任務叫用截止)，如果您不想在到達維護時段截止時間後啟動任何新的任務叫用，則請選擇 **Enabled** (已啟用)。

   當此選項*未*啟用時，任務會在到達截止時間後繼續執行，並啟動新的任務叫用，直到完成為止。
**注意**  
當您啟用此選項時，未完成的任務狀態為 `TIMED_OUT`。

1. 在此步驟中，選擇所選任務類型的索引標籤。

------
#### [ Run Command ]

   1. 在 **Command 文件**清單中，選擇定義要執行任務的 Systems Manager Command 文件 (SSM 文件)。

   1. 在 **Document Version (文件版本)** 中，選擇要使用的文件版本。

   1. 對於 **Task priority (任務優先順序)**，請指定此任務的優先順序。零 (`0`) 是最高的優先順序。維護時段內的任務都是以優先順序來排程；相同優先順序的任務會平行排程。

------
#### [ Automation ]

   1.  在 **Automation 文件**清單中，選擇定義要執行任務的 Automation 執行手冊。

   1. 在 **Document Version** (文件版本) 中，選擇要使用的 Runbook 版本。

   1. 對於 **Task priority (任務優先順序)**，請指定此任務的優先順序。零 (`0`) 是最高的優先順序。維護時段內的任務都是以優先順序來排程；相同優先順序的任務會平行排程。

------
#### [ Lambda ]

   1. 在 **Lambda 參數**區域中，從清單中選擇 Lambda 函數。

   1. (選用) 提供您想包含之 **Payload** (承載)、**Client Context** (用戶端內容) 或 **Qualifier** (限定詞) 的任何內容。
**注意**  
在某些情況下，您可以使用*虛擬參數*作為 `Payload` 值的一部分。維護時段任務執行時，其會傳遞正確的值 (而不是虛擬參數預留位置)。如需相關資訊，請參閱[註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)。

   1. 對於 **Task priority (任務優先順序)**，請指定此任務的優先順序。零 (`0`) 是最高的優先順序。維護時段內的任務都是以優先順序來排程；相同優先順序的任務會平行排程。

------
#### [ Step Functions ]

   1. 在 **Step Functions 參數**區域中，從清單中選擇狀態機器。

   1. (選用) 提供狀態機器執行的名稱以及您想包含之 **Input** (輸入) 的任何內容。
**注意**  
在某些情況下，您可以使用*虛擬參數*作為 `Input` 值的一部分。維護時段任務執行時，其會傳遞正確的值 (而不是虛擬參數預留位置)。如需相關資訊，請參閱[註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)。

   1. 對於 **Task priority (任務優先順序)**，請指定此任務的優先順序。零 (`0`) 是最高的優先順序。維護時段內的任務都是以優先順序來排程；相同優先順序的任務會平行排程。

------

1. 在 **Targets** (目標) 區域中，選擇以下其中一項：
   + **Selecting registered target groups** (選取已註冊的目標群組)：選取您已在目前維護時段註冊的一或多個維護時段目標。
   + **Selecting unregistered targets** (選取未註冊的目標)：逐一選擇可用的資源作為任務的目標。

     如果您預期看到的受管節點未列出，請參閱 [疑難排解受管節點的可用性](fleet-manager-troubleshooting-managed-nodes.md) 以取得疑難排解秘訣。
   + **Task target not required** (不需要任務目標)：任務的目標可能已經針對除 Run Command 類型以外的所有任務在其他函數中指定。

     為維護時段 Run Command 類型任務指定一或多個目標。視任務而定，其他維護時段任務類型 （自動化 AWS Lambda和 AWS Step Functions) 的目標為選用。如需有關執行未指定目標之任務的詳細資訊，請參閱 [註冊不含目標的維護時段任務](maintenance-windows-targetless-tasks.md)。
**注意**  
在許多情況下，您不需要明確指定自動化任務的目標。例如，假設您正在建立 Automation 類型任務來使用 `AWS-UpdateLinuxAmi` Runbook 更新 Linux 的 Amazon Machine Image (AMI)。當任務執行時，AMI 已更新為可用的最新版本 Linux 發行版本套件和 Amazon 軟體。從 AMI 建立的新執行個體已經安裝這些更新。因為在 Runbook 的輸入參數中指定了要更新的 AMI ID，所以不需要在維護時段任務中再次指定目標。

1. *僅限 Automation 任務：*

   在 **Input parameters** (輸入參數) 區域中，為執行任務所需的任何必要或選用參數提供值。
**注意**  
在某些情況下，您可以針對特定輸入參數值使用*虛擬參數*。維護時段任務執行時，其會傳遞正確的值 (而不是虛擬參數預留位置)。如需相關資訊，請參閱[註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)。

1. 對於**速率控制**：
   + 在**並行**中，指定可同時執行命令的受管節點數目或百分比。
**注意**  
如果您透過指定套用至受管節點的標籤或指定 AWS 資源群組來選取目標，而且不確定目標的受管節點數量，則透過指定百分比來限制可同時執行文件的目標數量。
   + 在 **Error threshold** (錯誤閾值) 中，指定在特定數目或百分比之節點上的命令失敗之後，停止在其他受管節點上執行命令。例如，如果您指定三個錯誤，則 Systems Manager 會在收到第四個錯誤時停止傳送命令。仍在處理命令的受管節點也可能會傳送錯誤。

1. (選用) 對於 **IAM 服務角色**，請選擇一個角色，以便為 Systems Manager 提供執行維護時段任務時承擔的許可。

   如果您未指定服務角色 ARN，則 Systems Manager 會使用帳戶中的服務連結角色。此角色未列在下拉式功能表中。如果帳戶中不存在適當的 Systems Manager 服務連結角色，則會在成功註冊任務時建立該角色。
**注意**  
為了改善安全狀態，強烈建議您建立自訂政策和自訂服務角色，以便執行維護時段任務。您可以制定政策，僅提供特定維護時段任務所需的許可。如需詳細資訊，請參閱[設定 Maintenance Windows](setting-up-maintenance-windows.md)。

1. *僅限 Run Command 任務：*

   (選用) 對於 **Output options** (輸出選項)，執行下列動作：
   + 選取 **Enable writing to S3** (啟用寫入 S3) 核取方塊，將命令輸出儲存成檔案。在方塊中輸入儲存貯體和字首 (資料夾) 名稱。
   + 選取 **CloudWatch output** (CloudWatch 輸出) 核取方塊，將完整的輸出寫入 Amazon CloudWatch Logs。輸入 CloudWatch Logs 日誌群組的名稱。
**注意**  
授予能力以將資料寫入至 S3 儲存貯體或 CloudWatch Logs 的許可，是指派給節點的執行個體設定檔的許可 (而不是執行此任務的 IAM 使用者的許可)。如需詳細資訊，請參閱[設定 Systems Manager 所需的執行個體許可](setup-instance-permissions.md)。此外，如果指定的 S3 儲存貯體或日誌群組位於不同的 中 AWS 帳戶，請確認與節點相關聯的執行個體描述檔具有寫入該儲存貯體的必要許可。

1. *僅限 Run Command 任務：*

   在**SNS 通知**區段中，如果您要傳送有關命令執行狀態的通知，請選取**啟用 SNS 通知**核取方塊。

   如需為 Run Command 設定 Amazon SNS 通知的詳細資訊，請參閱 [使用 Amazon SNS 通知監控 Systems Manager 狀態變更](monitoring-sns-notifications.md)。

1. *僅限 Run Command 任務：*

   在 **Parameters** (參數) 區域中，指定文件的參數。
**注意**  
在某些情況下，您可以針對特定輸入參數值使用*虛擬參數*。維護時段任務執行時，其會傳遞正確的值 (而不是虛擬參數預留位置)。如需相關資訊，請參閱[註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)。

1. *僅限 Run Command 和 Automation 任務：*

   (選用) 在 **CloudWatch 警示**區域中，在**警示名稱**欄位中選擇要套用至任務來進行監控的 CloudWatch 警示。

   如果警示啟用，則會停止任務。
**注意**  
若要將 CloudWatch 警示附加至您的任務，執行任務的 IAM 主體必須具備 `iam:createServiceLinkedRole` 動作的許可。如需有關 CloudWatch 警示的詳細資訊，請參閱[使用 Amazon CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

1. 根據任務類型，選擇下列其中一項：
   + **Register Run command task (註冊執行命令任務)**
   + **Register Automation task (註冊自動化任務)**
   + **Register Lambda task (註冊 Lambda 任務)**
   + **Register Step Functions task (註冊步驟函數任務)**

# 使用主控台停用或啟用維護時段
<a name="sysman-maintenance-disable"></a>

您可以在 Maintenance Windows ( AWS Systems Manager中的工具) 中停用或啟用維護時段。您可以一次選擇一個維護時段，以停用或啟用維護時段。您也可以選取多個或所有維護時段以全部啟用或停用。

本節說明如何使用 Systems Manager 主控台來啟用或停用維護時段。如需如何使用 AWS Command Line Interface (AWS CLI) 執行此操作的範例，請參閱 [教學課程：使用 更新維護時段 AWS CLI](maintenance-windows-cli-tutorials-update.md)。

**Topics**
+ [使用主控台停用維護時段](#sysman-maintenance-disable-mw)
+ [使用主控台啟用維護時段](#sysman-maintenance-enable-mw)

## 使用主控台停用維護時段
<a name="sysman-maintenance-disable-mw"></a>

您可以透過停用維護時段暫停一項任務一段指定的期間，而且您在之後仍然可以再次啟用此維護時段。

**停用維護時段**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 使用您要停用之維護時段旁邊的核取方塊；您可以為一或多個維護時段這麼做。

1. 在**動作**選單上，選擇**停用維護時段**。系統會提示您確認您的動作。

## 使用主控台啟用維護時段
<a name="sysman-maintenance-enable-mw"></a>

您可以透過啟用維護時段來繼續一項任務。

**注意**  
如果維護時段使用速率排程，且開始日期目前設定為過去的日期和時間，則會使用目前的日期和時間作為維護時段的開始日期。您可以在啟用維護時段前後變更其開始日期。如需相關資訊，請參閱[使用主控台更新或刪除維護時段資源](sysman-maintenance-update.md)。

**啟用維護時段**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 選取要啟用的維護時段旁的核取方塊。

1. 選擇**動作，啟用維護時段**。系統會提示您確認您的動作。

# 使用主控台更新或刪除維護時段資源
<a name="sysman-maintenance-update"></a>

您可以在 Maintenance Windows ( AWS Systems Manager中的工具) 中更新或刪除維護時段。您也可以更新或刪除維護時段的目標或任務。如果您編輯維護時段的詳細資訊，即可變更排程、目標和任務。您也可以指定時段的名稱和說明、目標和任務，這可協助您更了解他們的用途，並可讓您更輕鬆地管理您佇列的時段。

本節說明如何使用 Systems Manager 主控台來更新或刪除維護時段、目標和任務。如需有關如何使用 AWS Command Line Interface (AWS CLI) 完成此操作的詳細資訊，請參閱 [教學課程：使用 更新維護時段 AWS CLI](maintenance-windows-cli-tutorials-update.md)。

**Topics**
+ [使用主控台更新或刪除維護時段](#sysman-maintenance-update-mw)
+ [使用主控台更新或取消註冊維護時段目標](#sysman-maintenance-update-target)
+ [使用主控台更新或取消註冊維護時段任務](#sysman-maintenance-update-tasks)

## 使用主控台更新或刪除維護時段
<a name="sysman-maintenance-update-mw"></a>

您可以更新維護時段來變更其名稱、描述和排程，以及維護時段是否應允許未註冊的目標。

**更新或刪除維護時段**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 選取您要更新或刪除之維護時段旁邊的按鈕，然後執行以下其中一項：
   + 選擇 **刪除**。系統會提示您確認您的動作。
   + 選擇**編輯**。在 **Edit maintenance window** (編輯維護時段) 頁面上，變更您想更改的值和選項，然後選擇 **Save changes** (儲存變更)。

     如需您可以選擇的組態的相關資訊，請參閱 [使用主控台建立維護時段](sysman-maintenance-create-mw.md)。

## 使用主控台更新或取消註冊維護時段目標
<a name="sysman-maintenance-update-target"></a>

您可以更新或取消註冊維護時段的目標。如果您選擇更新維護時段目標，您可以指定新的目標名稱、說明和擁有者。您也可以選擇不同的目標。

**更新或刪除維護時段的目標**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 選擇要更新之維護時段的名稱、選擇 **Targets** (目標) 索引標籤，然後執行以下其中一項：
   + 若要更新目標，請選取要更新目標旁邊的按鈕，然後選擇 **Edit** (編輯)。
   + 若要取消註冊目標，請選取要取消註冊目標旁邊的按鈕，然後選擇 **Deregister targets** (取消註冊目標)。在 **Deregister maintenance windows target** (取消註冊維護時段目標) 對話方塊中，選擇 **Deregister** (取消註冊)。

## 使用主控台更新或取消註冊維護時段任務
<a name="sysman-maintenance-update-tasks"></a>

您可以更新或取消註冊維護時段的任務。如果您選擇更新，您可以指定新的任務名稱、說明和擁有者。對於 Run Command 和 Automation 任務，您可以為任務選擇不同的 SSM 文件。不過您無法編輯任務，以變更其類型。例如，如果您建立自動化任務，您不能編輯該任務，並將其變更為 Run Command 任務。

**使用主控台更新或刪除維護時段的任務**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 選擇您要更新的維護時段名稱。

1. 選擇 **Tasks** (任務) 索引標籤，然後選取要更新任務旁邊的按鈕。

1. 執行以下任意一項：
   + 若要取消註冊任務，請選擇 **Deregister task** (取消註冊任務)。
   + 若要編輯任務，請選擇 **Edit** (編輯)。變更您想更改的值和選項，然後選擇 **Edit task** (編輯任務)。

# 教學課程
<a name="maintenance-windows-tutorials"></a>

本節中的教學課程說明如何在使用維護時段時執行常見任務。

**完成事前準備**  
嘗試這些教學之前，請完成以下事前準備。
+ **在本機電腦上設定 AWS CLI** – 您必須先在本機電腦上安裝和設定 CLI，才能執行 AWS CLI 命令。如需相關資訊，請參閱[安裝或更新 AWS CLI 的最新版本](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)和[安裝 AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html)。
+ **驗證維護時段角色和許可** – 您帳戶中的 AWS 管理員必須授予您使用 CLI 管理維護時段時所需的 AWS Identity and Access Management (IAM) 的許可。如需相關資訊，請參閱[設定 Maintenance Windows](setting-up-maintenance-windows.md)。
+ **建立或設定與 Systems Manager 相容的執行個體** – 要完成教學課程，您需要至少一個 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體，該執行個體要設定為可與 Systems Manager 搭配使用。這表示 SSM Agent 會安裝在此執行個體，且 Systems Manager 的 IAM 執行個體設定檔會連接到此執行個體。

  我們建議透過預先安裝的代理程式從 AWS 管理的 Amazon Machine Image (AMI) 來啟動執行個體。如需更多詳細資訊，請參閱 [尋找預先安裝了 SSM Agent的 AMIs](ami-preinstalled-agent.md)。

  如需在執行個體安裝 SSM Agent 的詳細資訊，請參閱下列主題：
  + [在 Windows Server EC2 執行個體上手動安裝和解除安裝 SSM Agent](manually-install-ssm-agent-windows.md)
  + [在 Linux EC2 執行個體上手動安裝和解除安裝 SSM Agent](manually-install-ssm-agent-linux.md)

  如需有關為執行個體設定 Systems Manager IAM 許可的詳細資訊，請參閱[設定 Systems Manager 所需的執行個體許可](setup-instance-permissions.md)。
+ **建立所需的其他資源** – Run Command (Systems Manager 的工具) 包含許多任務，您只需建立先決條件主題中列出的資源。因此，我們提供簡單的 Run Command 任務，供您在第一次演練教學過程中使用。如本主題之前所述，您還需要已設定為可與 Systems Manager 搭配使用的 EC2 執行個體。在設定該執行個體後，您可以註冊簡單的 Run Command 任務。

  Systems Manager Maintenance Windows 工具支援執行下列四種任務：
  + Run Command 命令
  + Systems Manager Automation 工作流程
  + AWS Lambda 函式
  + AWS Step Functions 任務

  一般來說，如果您想要執行的維護時段任務需要其他資源，則應先建立這些資源。例如，如果您想要執行 AWS Lambda 函數的維護時段，請在開始前先建立 Lambda 函數；對於 Run Command 任務，請建立您可以將命令輸出儲存到其中的 S3 儲存貯體 (如果您計劃這麼做)，以此類推。

**Topics**
+ [教學課程：使用 建立和管理維護時段 AWS CLI](maintenance-window-tutorial-cli.md)
+ [教學課程：使用主控台建立修補維護時段](maintenance-window-tutorial-patching.md)

# 教學課程：使用 建立和管理維護時段 AWS CLI
<a name="maintenance-window-tutorial-cli"></a>

這個區段包括的教學，可協助您了解如何使用 AWS Command Line Interface (AWS CLI) 來執行下列動作：
+ 建立和設定維護時段
+ 檢視維護時段的資訊
+ 檢視維護時段任務和任務執行的相關資訊
+ 更新維護時段
+ 刪除維護時段

**追蹤資源 ID**  
當您完成這些 AWS CLI 教學課程中的任務時，請追蹤您執行的命令所產生的資源 IDs。您可以使用這些 ID 做為後續命令的輸入。例如，在建立維護時段時，系統會以下列格式將維護時段的 ID 提供給您：

```
{
   "WindowId":"mw-0c50858d01EXAMPLE"
}
```

請記下以下系統產生的 ID，因為此區段的教學會用到這些資訊：
+ `WindowId`
+ `WindowTargetId`
+ `WindowTaskId`
+ `WindowExecutionId`
+ `TaskExecutionId`
+ `InvocationId`
+ `ExecutionId`

您也需要計劃在此教學課程中使用之 EC2 執行個體的 ID。例如：`i-02573cafcfEXAMPLE`

**Topics**
+ [教學課程：使用 建立和設定維護時段 AWS CLI](maintenance-windows-cli-tutorials-create.md)
+ [教學課程：使用 檢視維護時段的相關資訊 AWS CLI](maintenance-windows-cli-tutorials-describe.md)
+ [教學課程：使用 檢視任務和任務執行的相關資訊 AWS CLI](mw-cli-tutorial-task-info.md)
+ [教學課程：使用 更新維護時段 AWS CLI](maintenance-windows-cli-tutorials-update.md)
+ [教學課程：使用 刪除維護時段 AWS CLI](mw-cli-tutorial-delete-mw.md)

# 教學課程：使用 建立和設定維護時段 AWS CLI
<a name="maintenance-windows-cli-tutorials-create"></a>

本教學課程示範如何使用 AWS Command Line Interface (AWS CLI) 來建立和設定維護時段、其目標及其任務。此教學的主要過程包含幾個簡單的步驟。建立一個維護時段、識別單一目標，並為要執行的維護時段設定簡單的任務。我們會在過程中提供您可用來嘗試更複雜案例的資訊。

當您按照此教學課程中的步驟，使用自己的選項和 ID 來取代斜體*紅色*文字。例如，使用您所建立之資源 ID 取代維護時段 ID *mw-0c50858d01EXAMPLE* 和執行個體 ID *i-02573cafcfEXAMPLE*。

**Topics**
+ [步驟 1：使用 建立維護時段 AWS CLI](mw-cli-tutorial-create-mw.md)
+ [步驟 2：使用 向維護時段註冊目標節點 AWS CLI](mw-cli-tutorial-targets.md)
+ [步驟 3：使用 向維護時段註冊任務 AWS CLI](mw-cli-tutorial-tasks.md)

# 步驟 1：使用 建立維護時段 AWS CLI
<a name="mw-cli-tutorial-create-mw"></a>

在此步驟中，您會建立維護時段並指定其基本選項，例如名稱、排程和持續時間。在後續步驟中，您可以選擇其更新的執行個體和其執行的任務。

在我們的範例中，您將建立每 5 分鐘執行一次的維護時段。一般而言，您無法如此頻繁地執行維護時段。不過，這個速率可讓您快速取得教學結果。我們也將示範如何在任務已成功執行之後變更為較低的頻率速率。

**注意**  
如需維護時段的各種排程相關選項彼此之間有何關聯的說明，請參閱 [維護時段排程與作用期間選項](maintenance-windows-schedule-options.md)。  
如需使用 `--schedule` 選項的詳細資訊，請參閱[參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)。

**使用 建立維護時段 AWS CLI**

1. 開啟 AWS Command Line Interface (AWS CLI) 並在本機電腦上執行下列命令，以建立執行下列動作的維護時段：
   + 每 5 分鐘執行一次，持續時間長達兩個小時 (視需要)。
   + 避免讓新任務在維護時段操作結束的 1 小時內啟動。
   + 允許沒有關聯的目標可讓您尚未註冊 (您未向維護時段註冊的執行個體)。
   + 自訂標籤的使用，表示其建立者想要在教學中使用它。

------
#### [ Linux & macOS ]

   ```
   aws ssm create-maintenance-window \
       --name "My-First-Maintenance-Window" \
       --schedule "rate(5 minutes)" \
       --duration 2 \
       --cutoff 1 \
       --allow-unassociated-targets \
       --tags "Key=Purpose,Value=Tutorial"
   ```

------
#### [ Windows ]

   ```
   aws ssm create-maintenance-window ^
       --name "My-First-Maintenance-Window" ^
       --schedule "rate(5 minutes)" ^
       --duration 2 ^
       --cutoff 1 ^
       --allow-unassociated-targets ^
       --tags "Key"="Purpose","Value"="Tutorial"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
      "WindowId":"mw-0c50858d01EXAMPLE"
   }
   ```

1. 立即執行以下命令來檢視相關詳細資訊，以及您帳戶中已存在的任何其他維護時段。

   ```
   aws ssm describe-maintenance-windows
   ```

   系統會傳回與以下相似的資訊。

   ```
   {
      "WindowIdentities":[
         {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "Name": "My-First-Maintenance-Window",
               "Enabled": true,
               "Duration": 2,
               "Cutoff": 1,
               "NextExecutionTime": "2019-05-11T16:46:16.991Z"
         }
      ]
   }
   ```

繼續進行[步驟 2：使用 向維護時段註冊目標節點 AWS CLI](mw-cli-tutorial-targets.md)。

# 步驟 2：使用 向維護時段註冊目標節點 AWS CLI
<a name="mw-cli-tutorial-targets"></a>

在此步驟中，您會使用新的維護時段來註冊目標。在這個情況下，您會指定維護時段執行時要更新哪個節點。

如需使用節點 ID 一次註冊多個節點的範例、使用標籤來識別多個節點的範例，與將資源群組指定為目標的範例，請參閱 [範例：向維護時段註冊目標](mw-cli-tutorial-targets-examples.md)。

**注意**  
您應已建立此步驟中要使用的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體，如 [Maintenance Windows 教學課程事前準備](maintenance-windows-tutorials.md)所述。

**使用 向維護時段註冊目標節點 AWS CLI**

1. 在本機機器上執行以下命令。將每個*範例資源預留位置*取代為您自己的資訊。

------
#### [ Linux & macOS ]

   ```
   aws ssm register-target-with-maintenance-window \
       --window-id "mw-0c50858d01EXAMPLE" \
       --resource-type "INSTANCE" \
       --target "Key=InstanceIds,Values=i-02573cafcfEXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm register-target-with-maintenance-window ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --resource-type "INSTANCE" ^
       --target "Key=InstanceIds,Values=i-02573cafcfEXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
      "WindowTargetId":"e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
   }
   ```

1. 立即在您的本機機器上執行以下命令，來檢視有關維護時段目標的詳細資訊。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-targets \
       --window-id "mw-0c50858d01EXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-targets ^
       --window-id "mw-0c50858d01EXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "Targets": [
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE",
               "ResourceType": "INSTANCE",
               "Targets": [
                   {
                       "Key": "InstanceIds",
                       "Values": [
                           "i-02573cafcfEXAMPLE"
                       ]
                   }
               ]
           }
       ]
   }
   ```

繼續進行[步驟 3：使用 向維護時段註冊任務 AWS CLI](mw-cli-tutorial-tasks.md)。

# 範例：向維護時段註冊目標
<a name="mw-cli-tutorial-targets-examples"></a>

您可以使用其節點 ID 將單一節點註冊為目標，如 [步驟 2：使用 向維護時段註冊目標節點 AWS CLI](mw-cli-tutorial-targets.md) 中所示範。您也可以使用此頁面上的命令格式來將一或多個節點註冊為目標。

一般而言，有兩種方法可以識別您想要做為維護時段目標的節點：指定個別的節點，並使用資源標籤。資源標籤方法提供多個選項，如範例 2-3 所示。

您也可以將一或多個資源群組指定為維護時段的目標。資源群組可以包含節點和許多其他類型的支援 AWS 資源。接下來的範例 4 和 5 會示範如何將資源群組新增到維護時段目標。

**注意**  
如果單一維護時段任務已向多個目標註冊，則其任務叫用會依序發生，而非平行發生。如果您的任務必須同時在多個目標上執行，請個別註冊每個目標的任務，並為每個任務指派相同的優先順序層級。

如需建立和管理資源群組的詳細資訊，請參閱《*AWS Resource Groups 使用者指南*》中的[什麼是資源群組？](https://docs.aws.amazon.com/ARG/latest/userguide/resource-groups.html)和 *AWS 新聞部落格*中的 [AWS的資源群組和標記](https://aws.amazon.com/blogs/aws/resource-groups-and-tagging/)。

如需有關 中Maintenance Windows工具 配額的資訊 AWS Systems Manager，除了下列範例中指定的配額之外，請參閱《》中的 [Systems Manager 服務配額](https://docs.aws.amazon.com/general/latest/gr/ssm.html#limits_ssm)*Amazon Web Services 一般參考*。

## 範例 1：使用節點 ID 註冊多個目標
<a name="mw-target-example-1"></a>

在本機機器上執行下列命令，以使用其節點 ID 將多個節點註冊為目標。將每個*範例資源預留位置*取代為您自己的資訊。

------
#### [ Linux & macOS ]

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --resource-type "INSTANCE" \
    --target "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE,i-07782c72faEXAMPLE"
```

------
#### [ Windows ]

```
aws ssm register-target-with-maintenance-window ^
    --window-id "mw-0c50858d01EXAMPLE ^
    --resource-type "INSTANCE" ^
    --target "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE,i-07782c72faEXAMPLE
```

------

**建議使用**：在第一次使用任何維護時段註冊唯一一組的節點時非常有用，但它們「不會」**共用常見的節點標籤。

**配額：**您可以為每個維護時段目標指定總計最多 50 個節點。

## 範例 2：使用節點中套用的資源標籤來註冊目標
<a name="mw-target-example-2"></a>

在本機機器上執行下列命令來註冊節點，這些執行個體皆已包含您已指派之索引鍵值對的標籤。將每個*範例資源預留位置*取代為您自己的資訊。

------
#### [ Linux & macOS ]

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --resource-type "INSTANCE" \
    --target "Key=tag:Region,Values=East"
```

------
#### [ Windows ]

```
aws ssm register-target-with-maintenance-window ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --resource-type "INSTANCE" ^
    --target "Key=tag:Region,Values=East"
```

------

**建議使用**：在第一次使用任何維護時段註冊唯一一組的節點時非常有用，但它們「會」**共用常見的節點標籤。

**配額：**您可以為每個目標指定總計最多五個鍵值組。如果您指定了多個鍵值對，則節點必須加上您指定的*所有*標籤鍵和值，才會包含在目標群組中。

**注意**  
您可以使用標籤金鑰 `Patch Group` 或 `PatchGroup` 為一組節點加上標籤，並將共用的金鑰值 (例如 `my-patch-group`) 指派給節點。(如果您已在 [EC2 執行個體中繼資料中允許標籤](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#allow-access-to-tags-in-IMDS)，則必須使用 `PatchGroup`，不留空格。) Patch Manager (Systems Manager 中的工具) 會評估節點上的 `Patch Group` 或 `PatchGroup` 金鑰，以協助判斷要對其套用哪些修補基準。如果您的任務會執行 `AWS-RunPatchBaseline` SSM 文件 (或舊版 `AWS-ApplyPatchBaseline` SSM 文件)，則您可以指定向維護時段註冊目標時的相同 `Patch Group` 或 `PatchGroup` 金鑰/值對。例如：`--target "Key=tag:PatchGroup,Values=my-patch-group`。這樣可允許您使用維護時段為一組節點更新修補程式 (已與相同修補基準建立關聯)。如需詳細資訊，請參閱[修補程式群組](patch-manager-patch-groups.md)。

## 範例 3：使用一組標籤索引鍵來註冊目標 (不含標籤值)
<a name="mw-target-example-3"></a>

在本機機器上執行以下命令來註冊執行個體，這些節點皆已獲指派一或多個標籤索引鍵，無論其索引鍵值為何。將每個*範例資源預留位置*取代為您自己的資訊。

------
#### [ Linux & macOS ]

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --resource-type "INSTANCE" \
    --target "Key=tag-key,Values=Name,Instance-Type,CostCenter"
```

------
#### [ Windows ]

```
aws ssm register-target-with-maintenance-window ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --resource-type "INSTANCE" ^
    --target "Key=tag-key,Values=Name,Instance-Type,CostCenter"
```

------

**建議使用**：當您想要透過指定多個標籤*索引鍵* (不含其值)，而不只是一個標籤索引鍵或標籤索引鍵值對，來鎖定節點時很有用。

**配額：**您可以為每個目標指定總計最多五個標籤鍵。如果您指定多個標籤鍵，則節點必須加上您指定的*所有*標籤鍵，才會包含在目標群組中。

## 範例 4：使用資源群組名稱註冊目標
<a name="mw-target-example-4"></a>

在本機機器上執行以下命令來註冊指定的資源群組，無論其中包含的資源類型為何。將 *mw-0c50858d01EXAMPLE* 取代為您自己的資訊。如果您指派給維護時段的任務沒有對在此資源群組中包含的資源類型執行動作，系統可能會報告錯誤。儘管發生這些錯誤，找到支援資源類型的任務會持續執行。

------
#### [ Linux & macOS ]

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --resource-type "RESOURCE_GROUP" \
    --target "Key=resource-groups:Name,Values=MyResourceGroup"
```

------
#### [ Windows ]

```
aws ssm register-target-with-maintenance-window ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --resource-type "RESOURCE_GROUP" ^
    --target "Key=resource-groups:Name,Values=MyResourceGroup"
```

------

**建議使用**：當您想要快速將資源群組指定為目標，而不評估維護時段是否將其所有資源類型視為目標時，或當您知道資源群組僅包含您的任務對其執行動作的資源類型時很有用。

**配額：**您可以僅將一個資源群組指定為目標。

## 範例 5：在資源群組中篩選資源類型來註冊目標
<a name="mw-target-example-5"></a>

在本機機器上執行以下命令來僅註冊特定資源類型，這些資源類型屬於您指定的資源群組。將 *mw-0c50858d01EXAMPLE* 取代為您自己的資訊。在使用此選項的情況下，即使您為屬於資源群組的資源類型新增任務，如果您還沒有將資源類型明確新增到篩選條件，此任務就不會執行。

------
#### [ Linux & macOS ]

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --resource-type "RESOURCE_GROUP" \
    --target "Key=resource-groups:Name,Values=MyResourceGroup" \
    "Key=resource-groups:ResourceTypeFilters,Values=AWS::EC2::Instance,AWS::ECS::Cluster"
```

------
#### [ Windows ]

```
aws ssm register-target-with-maintenance-window ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --resource-type "RESOURCE_GROUP" ^
    --target "Key=resource-groups:Name,Values=MyResourceGroup" ^
    "Key=resource-groups:ResourceTypeFilters,Values=AWS::EC2::Instance,AWS::ECS::Cluster"
```

------

**建議用途**：當您想要嚴格控制維護時段可以執行動作 AWS 的資源類型，或當資源群組包含大量資源類型，而且您想要避免維護時段日誌中不必要的錯誤報告時，請使用此功能。

**配額：**您可以僅將一個資源群組指定為目標。

# 步驟 3：使用 向維護時段註冊任務 AWS CLI
<a name="mw-cli-tutorial-tasks"></a>

在教學課程的此步驟中，您會註冊在 AWS Systems Manager Run Command Linux 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上執行 `df`命令的任務。此標準 Linux 命令的結果會顯示有多少可用空間，以及執行個體磁碟檔案系統上使用多少空間。

-或-

如果您以 Windows Server 的 Amazon EC2 執行個體為目標 (而不是 Linux)，請在下列命令中以 **ipconfig** 取代 **df**。此命令中的輸出會列出在目標執行個體上適用於轉接器的 IP 地址、子網路遮罩以及預設閘道的詳細資訊。

當您準備好註冊其他任務類型，或使用更多可用 Systems Manager Run Command 選項時，請參閱 [範例：向維護時段註冊任務](mw-cli-register-tasks-examples.md)。目前，我們提供所有四個任務類型的詳細資訊，以及其中一些最重要的選項，以協助您規劃更廣泛的真實世界案例。

**向維護時段註冊任務**

1. 在本機機器上執行以下命令。將每個*範例資源預留位置*取代為您自己的資訊。從本機 Windows 機器執行的版本包含逸出字元 (「/」)，您在透過命令列工具執行命令時會需要這些字元。

------
#### [ Linux & macOS ]

   ```
   aws ssm register-task-with-maintenance-window \
       --window-id mw-0c50858d01EXAMPLE \
       --task-arn "AWS-RunShellScript" \
       --max-concurrency 1 --max-errors 1 \
       --priority 10 \
       --targets "Key=InstanceIds,Values=i-0471e04240EXAMPLE" \
       --task-type "RUN_COMMAND" \
       --task-invocation-parameters '{"RunCommand":{"Parameters":{"commands":["df"]}}}'
   ```

------
#### [ Windows ]

   ```
   aws ssm register-task-with-maintenance-window ^
       --window-id mw-0c50858d01EXAMPLE ^
       --task-arn "AWS-RunShellScript" ^
       --max-concurrency 1 --max-errors 1 ^
       --priority 10 ^
       --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" ^
       --task-type "RUN_COMMAND" ^
       --task-invocation-parameters={\"RunCommand\":{\"Parameters\":{\"commands\":[\"df\"]}}}
   ```

------

   系統會傳回與以下相似的資訊：

   ```
   {
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE"
   }
   ```

1. 立即執行以下命令來檢視與您建立之維護時段任務相關的詳細資訊。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-tasks \
       --window-id mw-0c50858d01EXAMPLE
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-tasks ^
       --window-id mw-0c50858d01EXAMPLE
   ```

------

1. 系統會傳回與以下相似的資訊。

   ```
   {
       "Tasks": [
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
               "TaskArn": "AWS-RunShellScript",
               "Type": "RUN_COMMAND",
               "Targets": [
                   {
                       "Key": "InstanceIds",
                       "Values": [
                           "i-02573cafcfEXAMPLE"
                       ]
                   }
               ],
               "TaskParameters": {},
               "Priority": 10,
               "ServiceRoleArn": "arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole",
               "MaxConcurrency": "1",
               "MaxErrors": "1"
           }
       ]
   }
   ```

1. 根據您在 [步驟 1：使用 建立維護時段 AWS CLI](mw-cli-tutorial-create-mw.md) 指定的排程，等到任務的執行時間。例如，如果您已指定 **--schedule "rate(5 minutes)"**，請等待五分鐘。然後執行以下命令，來檢視與此任務發生的任何執行所相關的資訊。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-executions \
       --window-id mw-0c50858d01EXAMPLE
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-executions ^
       --window-id mw-0c50858d01EXAMPLE
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowExecutions": [
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
               "Status": "SUCCESS",
               "StartTime": 1557593493.096,
               "EndTime": 1557593498.611
           }
       ]
   }
   ```

**提示**  
任務順利執行後，您可以降低維護時段執行的速率。例如，執行以下命令來將頻率降低為一週一次。將 *mw-0c50858d01EXAMPLE* 取代為您自己的資訊。  

```
aws ssm update-maintenance-window \
    --window-id mw-0c50858d01EXAMPLE \
    --schedule "rate(7 days)"
```

```
aws ssm update-maintenance-window ^
    --window-id mw-0c50858d01EXAMPLE ^
    --schedule "rate(7 days)"
```
如需管理維護時段排程的詳細資訊，請參閱[參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)和[維護時段排程與作用期間選項](maintenance-windows-schedule-options.md)。  
如需使用 AWS Command Line Interface (AWS CLI) 修改維護時段的詳細資訊，請參閱 [教學課程：使用 更新維護時段 AWS CLI](maintenance-windows-cli-tutorials-update.md)。

如需練習執行 AWS CLI 命令以檢視維護時段任務及其執行的詳細資訊，請繼續 [教學課程：使用 檢視任務和任務執行的相關資訊 AWS CLI](mw-cli-tutorial-task-info.md)。

**存取教學課程命令輸出**  
超出本教學課程的範圍，您可以使用 AWS CLI 檢視與維護時段任務執行相關聯的Run Command命令*輸出*。

不過，您可以使用 檢視此資料 AWS CLI。(您也可以在 Systems Manager 主控台中或在 Amazon Simple Storage Service (Amazon S3) 儲存貯體中存放的日誌檔檢視輸出 (如果您已將維護時段設定為在前述日誌檔中存放命令輸出)。) 您會發現在 Linux EC2 執行個體上的 **df** 命令輸出與以下內容類似。

```
Filesystem 1K-blocks Used Available Use% Mounted on

devtmpfs 485716 0 485716 0% /dev

tmpfs 503624 0 503624 0% /dev/shm

tmpfs 503624 328 503296 1% /run

tmpfs 503624 0 503624 0% /sys/fs/cgroup

/dev/xvda1 8376300 1464160 6912140 18% /
```

在 Windows Server EC2 執行個體的 **ipconfig** 命令輸出與以下內容類似：

```
Windows IP Configuration


Ethernet adapter Ethernet 2:

   Connection-specific DNS Suffix  . : example.com
   IPv4 Address. . . . . . . . . . . : 10.24.34.0/23
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . : 0.0.0.0

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : abc1.wa.example.net

Wireless LAN adapter Local Area Connection* 1:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::100b:c234:66d6:d24f%4
   IPv4 Address. . . . . . . . . . . : 192.0.2.0
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.0.2.0

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
```

# 範例：向維護時段註冊任務
<a name="mw-cli-register-tasks-examples"></a>

您可以使用 AWS Command Line Interface (AWS CLI) Run Command透過維護時段在 中的工具 中註冊任務 AWS Systems Manager，如[使用維護時段註冊任務](mw-cli-tutorial-tasks.md)所示。您也可以註冊 Systems Manager Automation 工作流程、 AWS Lambda 函數和 AWS Step Functions 任務的任務，如本主題稍後所示。

**注意**  
為維護時段 Run Command 類型任務指定一或多個目標。視任務而定，其他維護時段任務類型 （自動化 AWS Lambda和 AWS Step Functions) 的目標為選用。如需有關執行未指定目標之任務的詳細資訊，請參閱 [註冊不含目標的維護時段任務](maintenance-windows-targetless-tasks.md)。

在本主題中，我們提供使用 AWS Command Line Interface (AWS CLI) 命令`register-task-with-maintenance-window`向維護時段註冊四種支援任務類型的每一個範例。此範例僅用於示範，但您可以進行修改來建立可運作的任務註冊命令。

**使用 --cli-input-json 選項**  
為了更有效地管理您的任務選項，您可以使用命令選項 `--cli-input-json`，內含 JSON 檔案中參考的選項值。

若要使用我們在以下範例中提供的範本 JSON 檔案內容，請在您的本機機器上執行下列動作：

1. 建立包含 `MyRunCommandTask.json`、`MyAutomationTask.json` 這類名稱或您偏好的另一個名稱來建立檔案。

1. 將我們的 JSON 範本內容複製到此檔案中。

1. 將檔案內容修改為任務註冊適用的內容，然後儲存檔案。

1. 在您存放該檔案的相同目錄中執行下列命令。替換 *MyFile.json* 的檔案名稱。

------
#### [ Linux & macOS ]

   ```
   aws ssm register-task-with-maintenance-window \
       --cli-input-json file://MyFile.json
   ```

------
#### [ Windows ]

   ```
   aws ssm register-task-with-maintenance-window ^
       --cli-input-json file://MyFile.json
   ```

------

**維護時段任務中的虛擬參數**  
在某些範例中，我們使用*虛擬參數*做為將 ID 資訊傳遞到任務的方法。例如，`{{TARGET_ID}}` 和 `{{RESOURCE_ID}}` 可以用來將 AWS 資源的 ID 傳遞給 Automation、Lambda 和 Step Functions 任務。如需 `--task-invocation-parameters` 內容中虛擬參數的詳細資訊，請參閱[註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)。

**詳細資訊**  
+ [register-task-with-maintenance-windows 命令的參數選項](mw-cli-task-options.md).
+ 《AWS CLI 命令參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html) 一節
+ *AWS Systems Manager API 參考*中的 [https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_RegisterTaskWithMaintenanceWindow.html](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_RegisterTaskWithMaintenanceWindow.html)

## 任務註冊範例
<a name="task-examples"></a>

下列各節提供範例 AWS CLI 命令，用於註冊支援的任務類型，以及可與 `--cli-input-json`選項搭配使用的 JSON 範例。

### 註冊 Systems Manager Run Command 任務
<a name="register-tasks-tutorial-run-command"></a>

以下範例示範如何使用 AWS CLI向維護時段註冊 Systems Manager Run Command 任務。

------
#### [ Linux & macOS ]

```
aws ssm register-task-with-maintenance-window \
    --window-id mw-0c50858d01EXAMPLE \
    --task-arn "AWS-RunShellScript" \
    --max-concurrency 1 --max-errors 1 --priority 10 \
    --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" \
    --task-type "RUN_COMMAND" \
    --task-invocation-parameters '{"RunCommand":{"Parameters":{"commands":["df"]}}}'
```

------
#### [ Windows ]

```
aws ssm register-task-with-maintenance-window ^
    --window-id mw-0c50858d01EXAMPLE ^
    --task-arn "AWS-RunShellScript" ^
    --max-concurrency 1 --max-errors 1 --priority 10 ^
    --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" ^
    --task-type "RUN_COMMAND" ^
    --task-invocation-parameters "{\"RunCommand\":{\"Parameters\":{\"commands\":[\"df\"]}}}"
```

------

**要與 `--cli-input-json` 檔案選項搭配使用的 JSON 內容：**

```
{
    "TaskType": "RUN_COMMAND",
    "WindowId": "mw-0c50858d01EXAMPLE",
    "Description": "My Run Command task to update SSM Agent on an instance",
    "MaxConcurrency": "1",
    "MaxErrors": "1",
    "Name": "My-Run-Command-Task",
    "Priority": 10,
    "Targets": [
        {
            "Key": "WindowTargetIds",
            "Values": [
                "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
            ]
        }
    ],
    "TaskArn": "AWS-UpdateSSMAgent",
    "TaskInvocationParameters": {
        "RunCommand": {
            "Comment": "A TaskInvocationParameters test comment",
            "NotificationConfig": {
                "NotificationArn": "arn:aws:sns:region:123456789012:my-sns-topic-name",
                "NotificationEvents": [
                    "All"
                ],
                "NotificationType": "Invocation"
            },
            "OutputS3BucketName": "amzn-s3-demo-bucket",
            "OutputS3KeyPrefix": "S3-PREFIX",
            "TimeoutSeconds": 3600
        }
    }
}
```

### 註冊 Systems Manager Automation 任務
<a name="register-tasks-tutorial-automation"></a>

以下範例示範如何使用 AWS CLI向維護時段註冊 Systems Manager Automation 任務：

**AWS CLI 命令：**

------
#### [ Linux & macOS ]

```
aws ssm register-task-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --task-arn "AWS-RestartEC2Instance" \
    --service-role-arn arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole \
    --task-type AUTOMATION \
    --task-invocation-parameters "Automation={DocumentVersion=5,Parameters={InstanceId='{{RESOURCE_ID}}'}}" \
    --priority 0 --name "My-Restart-EC2-Instances-Automation-Task" \
    --description "Automation task to restart EC2 instances"
```

------
#### [ Windows ]

```
aws ssm register-task-with-maintenance-window ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --task-arn "AWS-RestartEC2Instance" ^
    --service-role-arn arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole ^
    --task-type AUTOMATION ^
    --task-invocation-parameters "Automation={DocumentVersion=5,Parameters={InstanceId='{{TARGET_ID}}'}}" ^
    --priority 0 --name "My-Restart-EC2-Instances-Automation-Task" ^
    --description "Automation task to restart EC2 instances"
```

------

**要與 `--cli-input-json` 檔案選項搭配使用的 JSON 內容：**

```
{
    "WindowId": "mw-0c50858d01EXAMPLE",
        "TaskArn": "AWS-PatchInstanceWithRollback",
    "TaskType": "AUTOMATION","TaskInvocationParameters": {
        "Automation": {
            "DocumentVersion": "1",
            "Parameters": {
                "instanceId": [
                    "{{RESOURCE_ID}}"
                ]
            }
        }
    }
}
```

### 註冊 AWS Lambda 任務
<a name="register-tasks-tutorial-lambda"></a>

以下範例示範如何使用 AWS CLI向維護時段註冊 Lambda 函數任務。

對於這些範例，建立 Lambda 函數的使用者會將其命名為 `SSMrestart-my-instances` 並建立名為 `instanceId` 和 `targetType` 的兩個參數。

**重要**  
適用於 Maintenance Windows 的 IAM 政策需要您為 Lambda 函數 (或別名) 名稱新增 `SSM` 字首。在您繼續註冊此類任務之前，請在 中更新其名稱 AWS Lambda 以包含 `SSM`。例如，如果 Lambda 函數名稱為 `MyLambdaFunction`，請變更為 `SSMMyLambdaFunction`。

**AWS CLI 命令：**

------
#### [ Linux & macOS ]

**重要**  
如果您使用的是 第 2 版 AWS CLI，且 Lambda 承載不是 base64 編碼，則必須在下列命令`--cli-binary-format raw-in-base64-out`中包含 選項。`cli_binary_format` 選項僅在版本 2 中可用。如需有關此 AWS CLI `config`和其他檔案設定的資訊，請參閱*AWS Command Line Interface 《 使用者指南*》中的[支援`config`的檔案設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-settings)。

```
aws ssm register-task-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
    --priority 2 --max-concurrency 10 --max-errors 5 --name "My-Lambda-Example" \
    --description "A description for my LAMBDA example task" --task-type "LAMBDA" \
    --task-arn "arn:aws:lambda:region:123456789012:function:serverlessrepo-SSMrestart-my-instances-C4JF9EXAMPLE" \
    --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}","Qualifier": "$LATEST"}}'
```

------
#### [ PowerShell ]

**重要**  
如果您使用的是 第 2 版 AWS CLI，且 Lambda 承載不是 base64 編碼，則必須在下列命令`--cli-binary-format raw-in-base64-out`中包含 選項。`cli_binary_format` 選項僅在版本 2 中可用。如需有關此 AWS CLI `config`和其他檔案設定的資訊，請參閱*AWS Command Line Interface 《 使用者指南*》中的[支援`config`的檔案設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-settings)。

```
aws ssm register-task-with-maintenance-window `
    --window-id "mw-0c50858d01EXAMPLE" `
    --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" `
    --priority 2 --max-concurrency 10 --max-errors 5 --name "My-Lambda-Example" `
    --description "A description for my LAMBDA example task" --task-type "LAMBDA" `
    --task-arn "arn:aws:lambda:region:123456789012:function:serverlessrepo-SSMrestart-my-instances-C4JF9EXAMPLE" `
    --task-invocation-parameters '{\"Lambda\":{\"Payload\":\"{\\\"InstanceId\\\":\\\"{{RESOURCE_ID}}\\\",\\\"targetType\\\":\\\"{{TARGET_TYPE}}\\\"}\",\"Qualifier\": \"$LATEST\"}}'
```

------

**要與 `--cli-input-json` 檔案選項搭配使用的 JSON 內容：**

```
{
    "WindowId": "mw-0c50858d01EXAMPLE",
    "Targets": [
        {
            "Key": "WindowTargetIds",
            "Values": [
                "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
            ]
        }
    ],
    "TaskArn": "SSM_RestartMyInstances",
    "TaskType": "LAMBDA",
    "MaxConcurrency": "10",
    "MaxErrors": "10",
    "TaskInvocationParameters": {
        "Lambda": {
            "ClientContext": "ew0KICAi--truncated--0KIEXAMPLE",
            "Payload": "{ \"instanceId\": \"{{RESOURCE_ID}}\", \"targetType\": \"{{TARGET_TYPE}}\" }",
            "Qualifier": "$LATEST"
        }
    },
    "Name": "My-Lambda-Task",
    "Description": "A description for my LAMBDA task",
    "Priority": 5
}
```

### 註冊 Step Functions 任務
<a name="register-tasks-tutorial-step-functions"></a>

以下範例示範如何使用 AWS CLI向維護時段註冊 Step Functions 狀態機器任務。

**注意**  
維護時段任務僅支援 Step Functions 標準狀態機器工作流程。這些任務不支援快速狀態機器工作流程。如需有關狀態機器工作流程類型的資訊，請參閱《AWS Step Functions 開發人員指南》**中的[標準與快速工作流程](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html)。

對於這些範例，建立步驟函數狀態機器的使用者會使用名為 `instanceId` 的參數建立名為 `SSMMyStateMachine` 的狀態機器。

**重要**  
的 AWS Identity and Access Management (IAM) 政策Maintenance Windows需要您在 Step Functions 狀態機器名稱前面加上 `SSM`。在繼續註冊這類任務前，您必須在 AWS Step Functions 中將其名稱更新為包含 `SSM`。例如，如果狀態機器名稱為 `MyStateMachine`，請變更為 `SSMMyStateMachine`。

**AWS CLI 命令：**

------
#### [ Linux & macOS ]

```
aws ssm register-task-with-maintenance-window \
    --window-id "mw-0c50858d01EXAMPLE" \
    --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
    --task-arn arn:aws:states:region:123456789012:stateMachine:SSMMyStateMachine-MggiqEXAMPLE \
    --task-type STEP_FUNCTIONS \
    --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}", "Name":"{{INVOCATION_ID}}"}}' \
    --priority 0 --max-concurrency 10 --max-errors 5 \
    --name "My-Step-Functions-Task" --description "A description for my Step Functions task"
```

------
#### [ PowerShell ]

```
aws ssm register-task-with-maintenance-window `
    --window-id "mw-0c50858d01EXAMPLE" `
    --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" `
    --task-arn arn:aws:states:region:123456789012:stateMachine:SSMMyStateMachine-MggiqEXAMPLE `
    --task-type STEP_FUNCTIONS `
    --task-invocation-parameters '{\"StepFunctions\":{\"Input\":\"{\\\"InstanceId\\\":\\\"{{RESOURCE_ID}}\\\"}\", \"Name\":\"{{INVOCATION_ID}}\"}}' `
    --priority 0 --max-concurrency 10 --max-errors 5 `
    --name "My-Step-Functions-Task" --description "A description for my Step Functions task"
```

------

**要與 `--cli-input-json` 檔案選項搭配使用的 JSON 內容：**

```
{
    "WindowId": "mw-0c50858d01EXAMPLE",
    "Targets": [
        {
            "Key": "WindowTargetIds",
            "Values": [
                "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
            ]
        }
    ],
    "TaskArn": "SSM_MyStateMachine",
    "TaskType": "STEP_FUNCTIONS",
    "MaxConcurrency": "10",
    "MaxErrors": "10",
    "TaskInvocationParameters": {
        "StepFunctions": {
            "Input": "{ \"instanceId\": \"{{TARGET_ID}}\" }",
            "Name": "{{INVOCATION_ID}}"
        }
    },
    "Name": "My-Step-Functions-Task",
    "Description": "A description for my Step Functions task",
    "Priority": 5
}
```

# register-task-with-maintenance-windows 命令的參數選項
<a name="mw-cli-task-options"></a>

**register-task-with-maintenance-window** 命令會提供多種選項，可供您根據需求來設定任務。有些是必要的，有些是選用的，有些僅適用於單一維護時段任務類型。

本主題提供部分這些選項的相關資訊，以協助您使用此教學區段中的範本。如需其他命令選項的相關資訊，請參閱《*AWS CLI 命令參考*》中的 **[https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html)**。

**命令選項：`--task-arn`**  
選項 `--task-arn` 可用來指定執行任務的資源。您指定的值取決於註冊的任務類型，如下表所述。


**維護時段任務的 TaskArn 格式**  

| 維護時段任務類型 | TaskArn 值 | 
| --- | --- | 
|  **`RUN_COMMAND`** 與 **`AUTOMATION`**  |  `TaskArn` 是 SSM 文件名稱或 Amazon Resource Name (ARN)。例如： `AWS-RunBatchShellScript`  -或- `arn:aws:ssm:region:111122223333:document/My-Document`.  | 
|  **`LAMBDA`**  |  `TaskArn` 是函數名稱或 ARN。例如： `SSMMy-Lambda-Function` -或- `arn:aws:lambda:region:111122223333:function:SSMMyLambdaFunction`.  適用於 Maintenance Windows 的 IAM 政策需要您為 Lambda 函數 (或別名) 名稱新增 `SSM` 字首。在您繼續註冊此類任務之前，請在 中更新其名稱 AWS Lambda 以包含 `SSM`。例如，如果 Lambda 函數名稱為 `MyLambdaFunction`，請變更為 `SSMMyLambdaFunction`。   | 
|  **`STEP_FUNCTIONS`**  |  `TaskArn` 是狀態機器的 ARN。例如： `arn:aws:states:us-east-2:111122223333:stateMachine:SSMMyStateMachine`.  維護時段的 IAM 政策需要您為 Step Functions 狀態機器名稱加上 `SSM` 字首。註冊這類任務前，您必須在 AWS Step Functions 中將其名稱更新為包含 `SSM`。例如，如果狀態機器名稱為 `MyStateMachine`，請變更為 `SSMMyStateMachine`。   | 

**命令選項：`--service-role-arn`**  
 AWS Systems Manager 要在執行維護時段任務時擔任的角色。

如需詳細資訊，請參閱[設定 Maintenance Windows](setting-up-maintenance-windows.md)

**命令選項：`--task-invocation-parameters`**  
`--task-invocation-parameters` 選項可用來指定四種任務類型特有的參數。下表描述四種任務類型支援的參數。

**注意**  
如需在 `--task-invocation-parameters` 內容中使用虛擬參數的資訊，例如 \$1\$1TARGET\$1ID\$1\$1，請參閱 [註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md)。

維護時段任務的任務叫用參數選項


| 維護時段任務類型 | 可用參數  | 範例 | 
| --- | --- | --- | 
|  **`RUN_COMMAND`**  |  `Comment` `DocumentHash` `DocumentHashType` `NotificationConfig` `OutputS3BucketName` `OutPutS3KeyPrefix` `Parameters` `ServiceRoleArn` `TimeoutSeconds`  |  <pre>"TaskInvocationParameters": {<br />        "RunCommand": {<br />            "Comment": "My Run Command task comment",<br />            "DocumentHash": "6554ed3d--truncated--5EXAMPLE",<br />            "DocumentHashType": "Sha256",<br />            "NotificationConfig": {<br />                "NotificationArn": "arn:aws:sns:region:123456789012:my-sns-topic-name",<br />                "NotificationEvents": [<br />                    "FAILURE"<br />                ],<br />                "NotificationType": "Invocation"<br />            },<br />            "OutputS3BucketName": "amzn-s3-demo-bucket",<br />            "OutputS3KeyPrefix": "S3-PREFIX",<br />            "Parameters": {<br />                "commands": [<br />                    "Get-ChildItem$env: temp-Recurse|Remove-Item-Recurse-force"<br />                ]<br />            },<br />            "ServiceRoleArn": "arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole",<br />            "TimeoutSeconds": 3600<br />        }<br />    }</pre>  | 
|  **`AUTOMATION`**  |  `DocumentVersion` `Parameters`  |  <pre>"TaskInvocationParameters": {<br />        "Automation": {<br />            "DocumentVersion": "3",<br />            "Parameters": {<br />                "instanceid": [<br />                    "{{TARGET_ID}}"<br />                ]<br />            }<br />        }<br />    }</pre>  | 
|  **`LAMBDA`**  |  `ClientContext` `Payload` `Qualifier`  |  <pre>"TaskInvocationParameters": {<br />        "Lambda": {<br />            "ClientContext": "ew0KICAi--truncated--0KIEXAMPLE",<br />            "Payload": "{ \"targetId\": \"{{TARGET_ID}}\", \"targetType\": \"{{TARGET_TYPE}}\" }",<br />            "Qualifier": "$LATEST"<br />        }<br />    }</pre>  | 
|  **`STEP_FUNCTIONS`**  |  `Input` `Name`  |  <pre>"TaskInvocationParameters": {<br />        "StepFunctions": {<br />            "Input": "{ \"targetId\": \"{{TARGET_ID}}\" }",<br />            "Name": "{{INVOCATION_ID}}"<br />        }<br />    }</pre>  | 

# 教學課程：使用 檢視維護時段的相關資訊 AWS CLI
<a name="maintenance-windows-cli-tutorials-describe"></a>

此教學中包含的命令可協助您更新或取得維護時段、任務、執行和呼叫的相關資訊。此範例是依命令來示範如何使用命令選項來篩選您要查看的類型的詳細資訊。

當您按照此教學課程中的步驟，使用自己的選項和 ID 來取代斜體*紅色*文字。例如，使用您所建立之資源 ID 取代維護時段 ID *mw-0c50858d01EXAMPLE* 和執行個體 ID *i-02573cafcfEXAMPLE*。

如需設定 AWS Command Line Interface (AWS CLI) 的詳細資訊[，請參閱安裝、更新和解除安裝 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)和[設定 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。

**Topics**
+ ['describe-maintenance-windows' 的範例](#mw-cli-tutorials-describe-maintenance-windows)
+ ['describe-maintenance-window-targets' 的範例](#mw-cli-tutorials-describe-maintenance-window-targets)
+ ['describe-maintenance-window-tasks' 的範例](#mw-cli-tutorials-describe-maintenance-window-tasks)
+ ['describe-maintenance-windows-for-target' 的範例](#mw-cli-tutorials-describe-maintenance-windows-for-target)
+ ['describe-maintenance-window-executions' 的範例](#mw-cli-tutorials-describe-maintenance-window-executions)
+ ['describe-maintenance-window-schedule' 的範例](#mw-cli-tutorials-describe-maintenance-window-schedule)

## 'describe-maintenance-windows' 的範例
<a name="mw-cli-tutorials-describe-maintenance-windows"></a>

**列出 中的所有維護時段 AWS 帳戶**  
執行下列命令。

```
aws ssm describe-maintenance-windows
```

系統會傳回與以下相似的資訊。

```
{
   "WindowIdentities":[
      {
         "WindowId":"mw-0c50858d01EXAMPLE",
         "Name":"My-First-Maintenance-Window",
         "Enabled":true,
         "Duration":2,
         "Cutoff":0,
         "NextExecutionTime": "2019-05-18T17:01:01.137Z"        
      },
      {
         "WindowId":"mw-9a8b7c6d5eEXAMPLE",
         "Name":"My-Second-Maintenance-Window",
         "Enabled":true,
         "Duration":4,
         "Cutoff":1,
         "NextExecutionTime": "2019-05-30T03:30:00.137Z"        
      },
   ]
}
```

**列出所有啟用的維護時段**  
執行下列命令。

```
aws ssm describe-maintenance-windows --filters "Key=Enabled,Values=true"
```

系統會傳回與以下相似的資訊。

```
{
   "WindowIdentities":[
      {
         "WindowId":"mw-0c50858d01EXAMPLE",
         "Name":"My-First-Maintenance-Window",
         "Enabled":true,
         "Duration":2,
         "Cutoff":0,
         "NextExecutionTime": "2019-05-18T17:01:01.137Z"        
      },
      {
         "WindowId":"mw-9a8b7c6d5eEXAMPLE",
         "Name":"My-Second-Maintenance-Window",
         "Enabled":true,
         "Duration":4,
         "Cutoff":1,
         "NextExecutionTime": "2019-05-30T03:30:00.137Z"        
      },
   ]
}
```

**列出所有停用的維護時段**  
執行下列命令。

```
aws ssm describe-maintenance-windows --filters "Key=Enabled,Values=false"
```

系統會傳回與以下相似的資訊。

```
{
    "WindowIdentities": [
        {
            "WindowId": "mw-6e5c9d4b7cEXAMPLE",
            "Name": "My-Disabled-Maintenance-Window",
            "Enabled": false,
            "Duration": 2,
            "Cutoff": 1
        }
    ]
}
```

**列出所有含特定字首開頭名稱的維護時段**  
執行下列命令。

```
aws ssm describe-maintenance-windows --filters "Key=Name,Values=My"
```

系統會傳回與以下相似的資訊。

```
{
    "WindowIdentities": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "Enabled": true,
            "Duration": 2,
            "Cutoff": 0,
            "NextExecutionTime": "2019-05-18T17:01:01.137Z"
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "Name": "My-Second-Maintenance-Window",
            "Enabled": true,
            "Duration": 4,
            "Cutoff": 1,
            "NextExecutionTime": "2019-05-30T03:30:00.137Z"
        },
        {
            "WindowId": "mw-6e5c9d4b7cEXAMPLE",
            "Name": "My-Disabled-Maintenance-Window",
            "Enabled": false,
            "Duration": 2,
            "Cutoff": 1
        }
    ]
}
```

## 'describe-maintenance-window-targets' 的範例
<a name="mw-cli-tutorials-describe-maintenance-window-targets"></a>

**顯示符合特定擁有者資訊值的維護時段目標**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-targets \
    --window-id "mw-6e5c9d4b7cEXAMPLE" \
    --filters "Key=OwnerInformation,Values=CostCenter1"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-targets ^
    --window-id "mw-6e5c9d4b7cEXAMPLE" ^
    --filters "Key=OwnerInformation,Values=CostCenter1"
```

------

**注意**  
支援的篩選條件索引鍵為 `Type`、`WindowTargetId` 和 `OwnerInformation`。

系統會傳回與以下相似的資訊。

```
{
    "Targets": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE",
            "ResourceType": "INSTANCE",
            "Targets": [
                {
                    "Key": "tag:Name",
                    "Values": [
                        "Production"
                    ]
                }
            ],
            "OwnerInformation": "CostCenter1",
            "Name": "Target1"
        }
    ]
}
```

## 'describe-maintenance-window-tasks' 的範例
<a name="mw-cli-tutorials-describe-maintenance-window-tasks"></a>

**顯示所有叫用 SSM 命令文件 `AWS-RunPowerShellScript` 的註冊任務**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-tasks \
    --window-id "mw-0c50858d01EXAMPLE" \
    --filters "Key=TaskArn,Values=AWS-RunPowerShellScript"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-tasks ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --filters "Key=TaskArn,Values=AWS-RunPowerShellScript"
```

------

系統會傳回與以下相似的資訊。

```
{
   "Tasks":[
      {
         "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
         "MaxErrors":"1",
         "TaskArn":"AWS-RunPowerShellScript",
         "MaxConcurrency":"1",
         "WindowTaskId":"4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
         "TaskParameters":{
            "commands":{
               "Values":[
                  "driverquery.exe"
               ]
            }
         },
         "Priority":3,
         "Type":"RUN_COMMAND",
         "Targets":[
            {
               "TaskTargetId":"i-02573cafcfEXAMPLE",
               "TaskTargetType":"INSTANCE"
            }
         ]
      },
      {
         "ServiceRoleArn":"arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
         "MaxErrors":"1",
         "TaskArn":"AWS-RunPowerShellScript",
         "MaxConcurrency":"1",
         "WindowTaskId":"4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
         "TaskParameters":{
            "commands":{
               "Values":[
                  "ipconfig"
               ]
            }
         },
         "Priority":1,
         "Type":"RUN_COMMAND",
         "Targets":[
            {
               "TaskTargetId":"i-02573cafcfEXAMPLE",
               "TaskTargetType":"WINDOW_TARGET"
            }
         ]
      }
   ]
}
```

**顯示所有具有優先順序「3」的註冊任務**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-tasks \
    --window-id "mw-9a8b7c6d5eEXAMPLE" \
    --filters "Key=Priority,Values=3"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-tasks ^
    --window-id "mw-9a8b7c6d5eEXAMPLE" ^
    --filters "Key=Priority,Values=3"
```

------

系統會傳回與以下相似的資訊。

```
{
   "Tasks":[
      {
         "ServiceRoleArn":"arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
         "MaxErrors":"1",
         "TaskArn":"AWS-RunPowerShellScript",
         "MaxConcurrency":"1",
         "WindowTaskId":"4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
         "TaskParameters":{
            "commands":{
               "Values":[
                  "driverquery.exe"
               ]
            }
         },
         "Priority":3,
         "Type":"RUN_COMMAND",
         "Targets":[
            {
               "TaskTargetId":"i-02573cafcfEXAMPLE",
               "TaskTargetType":"INSTANCE"
            }
         ]
      }
   ]
}
```

**顯示所有具有優先順序 "1"，並使用 Run Command 的註冊任務**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-tasks \
    --window-id "mw-0c50858d01EXAMPLE" \
    --filters "Key=Priority,Values=1" "Key=TaskType,Values=RUN_COMMAND"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-tasks ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --filters "Key=Priority,Values=1" "Key=TaskType,Values=RUN_COMMAND"
```

------

系統會傳回與以下相似的資訊。

```
{
    "Tasks": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
            "TaskArn": "AWS-RunShellScript",
            "Type": "RUN_COMMAND",
            "Targets": [
                {
                    "Key": "InstanceIds",
                    "Values": [
                        "i-02573cafcfEXAMPLE"
                    ]
                }
            ],
            "TaskParameters": {},
            "Priority": 1,
            "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
            "MaxConcurrency": "1",
            "MaxErrors": "1"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowTaskId": "8a5c4629-31b0-4edd-8aea-33698EXAMPLE",
            "TaskArn": "AWS-UpdateSSMAgent",
            "Type": "RUN_COMMAND",
            "Targets": [
                {
                    "Key": "InstanceIds",
                    "Values": [
                        "i-0471e04240EXAMPLE"
                    ]
                }
            ],
            "TaskParameters": {},
            "Priority": 1,
            "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
            "MaxConcurrency": "1",
            "MaxErrors": "1",
            "Name": "My-Run-Command-Task",
            "Description": "My Run Command task to update SSM Agent on an instance"
        }
    ]
}
```

## 'describe-maintenance-windows-for-target' 的範例
<a name="mw-cli-tutorials-describe-maintenance-windows-for-target"></a>

**列出維護時段目標或任務 (與特定節點相關) 的資訊**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-windows-for-target \
    --resource-type INSTANCE \
    --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" \
    --max-results 10
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-windows-for-target ^
    --resource-type INSTANCE ^
    --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" ^
    --max-results 10
```

------

系統會傳回與以下相似的資訊。

```
{
    "WindowIdentities": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window"
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "Name": "My-Second-Maintenance-Window"
        }
    ]
}
```

## 'describe-maintenance-window-executions' 的範例
<a name="mw-cli-tutorials-describe-maintenance-window-executions"></a>

**列出在特定日期之前執行的所有任務**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-executions \
    --window-id "mw-9a8b7c6d5eEXAMPLE" \
    --filters "Key=ExecutedBefore,Values=2019-05-12T05:00:00Z"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-executions ^
    --window-id "mw-9a8b7c6d5eEXAMPLE" ^
    --filters "Key=ExecutedBefore,Values=2019-05-12T05:00:00Z"
```

------

系統會傳回與以下相似的資訊。

```
{
    "WindowExecutions": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
            "Status": "FAILED",
            "StatusDetails": "The following SSM parameters are invalid: LevelUp",
            "StartTime": 1557617747.993,
            "EndTime": 1557617748.101
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "WindowExecutionId": "791b72e0-f0da-4021-8b35-f95dfEXAMPLE",
            "Status": "SUCCESS",
            "StartTime": 1557594085.428,
            "EndTime": 1557594090.978
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowExecutionId": "ecec60fa-6bb0-4d26-98c7-140308EXAMPLE",
            "Status": "SUCCESS",
            "StartTime": 1557593793.483,
            "EndTime": 1557593798.978
        }
    ]
}
```

**列出在特定日期之後執行的所有任務**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-executions \
    --window-id "mw-9a8b7c6d5eEXAMPLE" \
    --filters "Key=ExecutedAfter,Values=2018-12-31T17:00:00Z"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-executions ^
    --window-id "mw-9a8b7c6d5eEXAMPLE" ^
    --filters "Key=ExecutedAfter,Values=2018-12-31T17:00:00Z"
```

------

系統會傳回與以下相似的資訊。

```
{
    "WindowExecutions": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
            "Status": "FAILED",
            "StatusDetails": "The following SSM parameters are invalid: LevelUp",
            "StartTime": 1557617747.993,
            "EndTime": 1557617748.101
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "WindowExecutionId": "791b72e0-f0da-4021-8b35-f95dfEXAMPLE",
            "Status": "SUCCESS",
            "StartTime": 1557594085.428,
            "EndTime": 1557594090.978
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "WindowExecutionId": "ecec60fa-6bb0-4d26-98c7-140308EXAMPLE",
            "Status": "SUCCESS",
            "StartTime": 1557593793.483,
            "EndTime": 1557593798.978
        }
    ]
}
```

## 'describe-maintenance-window-schedule' 的範例
<a name="mw-cli-tutorials-describe-maintenance-window-schedule"></a>

**顯示後續 10 個排程替特定節點執行的維護時段**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-schedule \
    --resource-type INSTANCE \
    --targets "Key=InstanceIds,Values=i-07782c72faEXAMPLE" \
    --max-results 10
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-schedule ^
    --resource-type INSTANCE ^
    --targets "Key=InstanceIds,Values=i-07782c72faEXAMPLE" ^
    --max-results 10
```

------

系統會傳回與以下相似的資訊。

```
{
    "ScheduledWindowExecutions": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-05-18T23:35:24.902Z"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-05-25T23:35:24.902Z"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-06-01T23:35:24.902Z"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-06-08T23:35:24.902Z"
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "Name": "My-Second-Maintenance-Window",
            "ExecutionTime": "2019-06-15T23:35:24.902Z"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-06-22T23:35:24.902Z"
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "Name": "My-Second-Maintenance-Window",
            "ExecutionTime": "2019-06-29T23:35:24.902Z"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-07-06T23:35:24.902Z"
        },
        {
            "WindowId": "mw-9a8b7c6d5eEXAMPLE",
            "Name": "My-Second-Maintenance-Window",
            "ExecutionTime": "2019-07-13T23:35:24.902Z"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "My-First-Maintenance-Window",
            "ExecutionTime": "2019-07-20T23:35:24.902Z"
        }
    ],
    "NextToken": "AAEABUXdceT92FvtKld/dGHELj5Mi+GKW/EXAMPLE"
}
```

**顯示替標記了特定金鑰/值對之節點排程的維護時段**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-schedule \
    --resource-type INSTANCE \
    --targets "Key=tag:prod,Values=rhel7"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-schedule ^
    --resource-type INSTANCE ^
    --targets "Key=tag:prod,Values=rhel7"
```

------

系統會傳回與以下相似的資訊。

```
{
    "ScheduledWindowExecutions": [
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "DemoRateStartDate",
            "ExecutionTime": "2019-10-20T05:34:56-07:00"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "DemoRateStartDate",
            "ExecutionTime": "2019-10-21T05:34:56-07:00"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "DemoRateStartDate",
            "ExecutionTime": "2019-10-22T05:34:56-07:00"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "DemoRateStartDate",
            "ExecutionTime": "2019-10-23T05:34:56-07:00"
        },
        {
            "WindowId": "mw-0c50858d01EXAMPLE",
            "Name": "DemoRateStartDate",
            "ExecutionTime": "2019-10-24T05:34:56-07:00"
        }
    ],
    "NextToken": "AAEABccwSXqQRGKiTZ1yzGELR6cxW4W/EXAMPLE"
}
```

**顯示後續四個維護時段的開始時間**  
執行下列命令。

------
#### [ Linux & macOS ]

```
aws ssm describe-maintenance-window-schedule \
    --window-id "mw-0c50858d01EXAMPLE" \
    --max-results "4"
```

------
#### [ Windows ]

```
aws ssm describe-maintenance-window-schedule ^
    --window-id "mw-0c50858d01EXAMPLE" ^
    --max-results "4"
```

------

系統會傳回與以下相似的資訊。

```
{
    "WindowSchedule": [
        {
            "ScheduledWindowExecutions": [
                {
                    "ExecutionTime": "2019-10-04T10:10:10Z",
                    "Name": "My-First-Maintenance-Window",
                    "WindowId": "mw-0c50858d01EXAMPLE"
                },
                {
                    "ExecutionTime": "2019-10-11T10:10:10Z",
                    "Name": "My-First-Maintenance-Window",
                    "WindowId": "mw-0c50858d01EXAMPLE"
                },
                {
                    "ExecutionTime": "2019-10-18T10:10:10Z",
                    "Name": "My-First-Maintenance-Window",
                    "WindowId": "mw-0c50858d01EXAMPLE"
                },
                {
                    "ExecutionTime": "2019-10-25T10:10:10Z",
                    "Name": "My-First-Maintenance-Window",
                    "WindowId": "mw-0c50858d01EXAMPLE"
                }
            ]
        }
    ]
}
```

# 教學課程：使用 檢視任務和任務執行的相關資訊 AWS CLI
<a name="mw-cli-tutorial-task-info"></a>

本教學課程示範如何使用 AWS Command Line Interface (AWS CLI) 來檢視已完成維護時段任務的詳細資訊。

如果您直接從 [教學課程：使用 建立和設定維護時段 AWS CLI](maintenance-windows-cli-tutorials-create.md) 繼續執行，請確保維護時段有足夠的時間可執行至少一次，以查看其執行的結果。

當您按照此教學課程中的步驟，使用自己的選項和 ID 來取代斜體*紅色*文字。例如，使用您所建立之資源 ID 取代維護時段 ID *mw-0c50858d01EXAMPLE* 和執行個體 ID *i-02573cafcfEXAMPLE*。

**使用 檢視任務和任務執行的相關資訊 AWS CLI**

1. 執行以下命令，以檢視特定維護時段的任務執行清單。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-executions \
       --window-id "mw-0c50858d01EXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-executions ^
       --window-id "mw-0c50858d01EXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowExecutions": [
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
               "Status": "SUCCESS",
               "StartTime": 1557593793.483,
               "EndTime": 1557593798.978
           },
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowExecutionId": "791b72e0-f0da-4021-8b35-f95dfEXAMPLE",
               "Status": "SUCCESS",
               "StartTime": 1557593493.096,
               "EndTime": 1557593498.611
           },
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowExecutionId": "ecec60fa-6bb0-4d26-98c7-140308EXAMPLE",
               "Status": "SUCCESS",
               "StatusDetails": "No tasks to execute.",
               "StartTime": 1557593193.309,
               "EndTime": 1557593193.334
           }
       ]
   }
   ```

1. 執行以下命令，以取得有關的維護時段任務執行。

------
#### [ Linux & macOS ]

   ```
   aws ssm get-maintenance-window-execution \
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm get-maintenance-window-execution ^
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
       "TaskIds": [
           "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE"
       ],
       "Status": "SUCCESS",
       "StartTime": 1557593493.096,
       "EndTime": 1557593498.611
   }
   ```

1. 執行以下命令，以列出維護時段執行期間執行的任務清單。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-execution-tasks \
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-execution-tasks ^
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowExecutionTaskIdentities": [
           {
               "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
               "TaskExecutionId": "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE",
               "Status": "SUCCESS",
               "StartTime": 1557593493.162,
               "EndTime": 1557593498.57,
               "TaskArn": "AWS-RunShellScript",
               "TaskType": "RUN_COMMAND"
           }
       ]
   }
   ```

1. 執行以下命令，以取得有關執行任務的詳細資訊。

------
#### [ Linux & macOS ]

   ```
   aws ssm get-maintenance-window-execution-task \
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE" \
       --task-id "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm get-maintenance-window-execution-task ^
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE" ^
       --task-id "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
       "TaskExecutionId": "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE",
       "TaskArn": "AWS-RunShellScript",
       "ServiceRole": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
       "Type": "RUN_COMMAND",
       "TaskParameters": [
           {
               "aws:InstanceId": {
                   "Values": [
                       "i-02573cafcfEXAMPLE"
                   ]
               },
               "commands": {
                   "Values": [
                       "df"
                   ]
               }
           }
       ],
       "Priority": 10,
       "MaxConcurrency": "1",
       "MaxErrors": "1",
       "Status": "SUCCESS",
       "StartTime": 1557593493.162,
       "EndTime": 1557593498.57
   }
   ```

1. 執行以下命令以獲得執行時特定的呼叫式任務。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-execution-task-invocations \
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE" \
       --task-id "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-execution-task-invocations ^
       --window-execution-id "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE" ^
       --task-id "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowExecutionTaskInvocationIdentities": [
           {
               "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
               "TaskExecutionId": "c9b05aba-197f-4d8d-be34-e73fbEXAMPLE",
               "InvocationId": "c336d2ab-09de-44ba-8f6a-6136cEXAMPLE",
               "ExecutionId": "76a5a04f-caf6-490c-b448-92c02EXAMPLE",
               "TaskType": "RUN_COMMAND",
               "Parameters": "{\"documentName\":\"AWS-RunShellScript\",\"instanceIds\":[\"i-02573cafcfEXAMPLE\"],\"maxConcurrency\":\"1\",\"maxErrors\":\"1\",\"parameters\":{\"commands\":[\"df\"]}}",
               "Status": "SUCCESS",
               "StatusDetails": "Success",
               "StartTime": 1557593493.222,
               "EndTime": 1557593498.466
           }
       ]
   }
   ```

# 教學課程：使用 更新維護時段 AWS CLI
<a name="maintenance-windows-cli-tutorials-update"></a>

本教學課程示範如何使用 AWS Command Line Interface (AWS CLI) 更新維護時段。它還說明如何更新不同的任務類型，包括 AWS Systems Manager Run Command和 自動化的任務類型 AWS Lambda，以及 AWS Step Functions。

本節中的範例使用以下 Systems Manager 動作來更新維護時段。
+ [UpdateMaintenanceWindow](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_UpdateMaintenanceWindow.html)
+ [UpdateMaintenanceWindowTarget](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_UpdateMaintenanceWindowTarget.html)
+ [UpdateMaintenanceWindowTask](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_UpdateMaintenanceWindowTask.html)
+ [DeregisterTargetFromMaintenanceWindow](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_DeregisterTargetFromMaintenanceWindow.html)

如需如何使用 Systems Manager 主控台更新維護時段的資訊，請參閱 [使用主控台更新或刪除維護時段資源](sysman-maintenance-update.md)。

當您按照此教學課程中的步驟，使用自己的選項和 ID 來取代斜體*紅色*文字。例如，使用您所建立之資源 ID 取代維護時段 ID *mw-0c50858d01EXAMPLE* 和執行個體 ID *i-02573cafcfEXAMPLE*。

**使用 更新維護時段 AWS CLI**

1. 開啟 AWS CLI 並執行下列命令來更新目標，以包含名稱和描述。

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-target \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
       --name "My-Maintenance-Window-Target" \
       --description "Description for my maintenance window target"
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-target ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^
       --name "My-Maintenance-Window-Target" ^
       --description "Description for my maintenance window target"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE",
       "Targets": [
           {
               "Key": "InstanceIds",
               "Values": [
                   "i-02573cafcfEXAMPLE"
               ]
           }
       ],
       "Name": "My-Maintenance-Window-Target",
       "Description": "Description for my maintenance window target"
   }
   ```

1. 執行以下命令來使用 `replace` 選項移除描述欄位，並新增額外的目標。移除描述欄位，因為更新後不包括此欄位 (空值)。請確定已指定一個額外的執行個體，此節點已設定為與 Systems Manager 搭配使用。

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-target \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-target-id "d208dedf-3f6b-41ff-ace8-8e751EXAMPLE" \
       --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE" \
       --name "My-Maintenance-Window-Target" \
       --replace
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-target ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-target-id "d208dedf-3f6b-41ff-ace8-8e751EXAMPLE" ^
       --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE" ^
       --name "My-Maintenance-Window-Target" ^
       --replace
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE",
       "Targets": [
           {
               "Key": "InstanceIds",
               "Values": [
                   "i-02573cafcfEXAMPLE",
                   "i-0471e04240EXAMPLE"
               ]
           }
       ],
       "Name": "My-Maintenance-Window-Target"
   }
   ```

1. `start-date` 允許您延遲啟用維護時段直到指定的未來日期。`end-date` 選項可讓您設定不再執行維護時段的未來日期和時間點。以 ISO-8601 Extended 格式指定選項。

   執行以下命令，來指定定期排定維護時段執行的日期和時間範圍。

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window \
       --window-id "mw-0c50858d01EXAMPLE" \
       --start-date "2020-10-01T10:10:10Z" \
       --end-date "2020-11-01T10:10:10Z"
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --start-date "2020-10-01T10:10:10Z" ^
       --end-date "2020-11-01T10:10:10Z"
   ```

------

1. 執行下列命令以更新 Run Command 任務
**提示**  
如果您的目標是適用於 Windows Server 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體，則將下列命令中的 `df` 變更為 `ipconfig`，以及 `AWS-RunShellScript`變更為 `AWS-RunPowerShellScript`。

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-task \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
       --task-arn "AWS-RunShellScript" \
       --service-role-arn "arn:aws:iam::account-id:role/MaintenanceWindowsRole" \
       --task-invocation-parameters "RunCommand={Comment=Revising my Run Command task,Parameters={commands=df}}" \
       --priority 1 --max-concurrency 10 --max-errors 4 \
       --name "My-Task-Name" --description "A description for my Run Command task"
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-task ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^
       --task-arn "AWS-RunShellScript" ^
       --service-role-arn "arn:aws:iam::account-id:role/MaintenanceWindowsRole" ^
       --task-invocation-parameters "RunCommand={Comment=Revising my Run Command task,Parameters={commands=df}}" ^
       --priority 1 --max-concurrency 10 --max-errors 4 ^
       --name "My-Task-Name" --description "A description for my Run Command task"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
       "Targets": [
           {
               "Key": "WindowTargetIds",
               "Values": [
                   "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
               ]
           }
       ],
       "TaskArn": "AWS-RunShellScript",
       "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole",
       "TaskParameters": {},
       "TaskInvocationParameters": {
           "RunCommand": {
               "Comment": "Revising my Run Command task",
               "Parameters": {
                   "commands": [
                       "df"
                   ]
               }
           }
       },
       "Priority": 1,
       "MaxConcurrency": "10",
       "MaxErrors": "4",
       "Name": "My-Task-Name",
       "Description": "A description for my Run Command task"
   }
   ```

1. 調整並執行下列命令以更新 Lambda 任務。

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-task \
       --window-id mw-0c50858d01EXAMPLE \
       --window-task-id 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE \
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
       --task-arn "arn:aws:lambda:region:111122223333:function:SSMTestLambda" \
       --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" \
       --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}"}}' \
       --priority 1 --max-concurrency 10 --max-errors 5 \
       --name "New-Lambda-Task-Name" \
       --description "A description for my Lambda task"
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-task ^
       --window-id mw-0c50858d01EXAMPLE ^
       --window-task-id 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE ^
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^
       --task-arn --task-arn "arn:aws:lambda:region:111122223333:function:SSMTestLambda" ^
       --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" ^
       --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}"}}' ^
       --priority 1 --max-concurrency 10 --max-errors 5 ^
       --name "New-Lambda-Task-Name" ^
       --description "A description for my Lambda task"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
       "Targets": [
           {
               "Key": "WindowTargetIds",
               "Values": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
           }
       ],
       "TaskArn": "arn:aws:lambda:us-east-2:111122223333:function:SSMTestLambda",
       "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole",
       "TaskParameters": {},
       "TaskInvocationParameters": {
           "Lambda": {
               "Payload": "e30="
           }
       },
       "Priority": 1,
       "MaxConcurrency": "10",
       "MaxErrors": "5",
       "Name": "New-Lambda-Task-Name",
       "Description": "A description for my Lambda task"
   }
   ```

1. 如果您更新的是 Step Functions 任務，請調整和執行以下命令來更新其 task-invocation-parameters。

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-task \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
       --task-arn "arn:aws:states:region:execution:SSMStepFunctionTest" \
       --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" \
       --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}"}}' \
       --priority 0 --max-concurrency 10 --max-errors 5 \
       --name "My-Step-Functions-Task" \
       --description "A description for my Step Functions task"
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-task ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^
       --task-arn "arn:aws:states:region:execution:SSMStepFunctionTest" ^
       --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" ^
       --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}"}}' ^
       --priority 0 --max-concurrency 10 --max-errors 5 ^
       --name "My-Step-Functions-Task" ^
       --description "A description for my Step Functions task"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
       "Targets": [
           {
               "Key": "WindowTargetIds",
               "Values": [
                   "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
               ]
           }
       ],
       "TaskArn": "arn:aws:states:us-east-2:111122223333:execution:SSMStepFunctionTest",
       "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole",
       "TaskParameters": {},
       "TaskInvocationParameters": {
           "StepFunctions": {
               "Input": "{\"instanceId\":\"{{RESOURCE_ID}}\"}"
           }
       },
       "Priority": 0,
       "MaxConcurrency": "10",
       "MaxErrors": "5",
       "Name": "My-Step-Functions-Task",
       "Description": "A description for my Step Functions task"
   }
   ```

1. 執行下列命令替維護時段取消註冊任務。此範例使用 `safe` 參數來判斷任次參考此目標，因此能夠安全的取消註冊。

------
#### [ Linux & macOS ]

   ```
   aws ssm deregister-target-from-maintenance-window \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
       --safe
   ```

------
#### [ Windows ]

   ```
   aws ssm deregister-target-from-maintenance-window ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^
       --safe
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   An error occurred (TargetInUseException) when calling the DeregisterTargetFromMaintenanceWindow operation: 
   This Target cannot be deregistered because it is still referenced in Task: 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
   ```

1. 執行以下命令替維護時段取消註冊目標，即使任務參考此目標。您可以使用 `no-safe` 強制取消註冊操作。

------
#### [ Linux & macOS ]

   ```
   aws ssm deregister-target-from-maintenance-window \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \
       --no-safe
   ```

------
#### [ Windows ]

   ```
   aws ssm deregister-target-from-maintenance-window ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^
       --no-safe
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
   }
   ```

1. 執行下列命令以更新 Run Command 任務 這個範例使用名為 `UpdateLevel` 的 Systems Manager Parameter Store 參數，格式如下：'`{{ssm:UpdateLevel}}`'

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-task \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \
       --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE"  \
       --task-invocation-parameters "RunCommand={Comment=A comment for my task update,Parameters={UpdateLevel='{{ssm:UpdateLevel}}'}}"
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-task ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^
       --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE"  ^
       --task-invocation-parameters "RunCommand={Comment=A comment for my task update,Parameters={UpdateLevel='{{ssm:UpdateLevel}}'}}"
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
       "Targets": [
           {
               "Key": "InstanceIds",
               "Values": [
                   "i-02573cafcfEXAMPLE"
               ]
           }
       ],
       "TaskArn": "AWS-RunShellScript",
       "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
       "TaskParameters": {},
       "TaskInvocationParameters": {
           "RunCommand": {
               "Comment": "A comment for my task update",
               "Parameters": {
                   "UpdateLevel": [
                       "{{ssm:UpdateLevel}}"
                   ]
               }
           }
       },
       "Priority": 10,
       "MaxConcurrency": "1",
       "MaxErrors": "1"
   }
   ```

1. 執行以下命令更新 Automation 任務來替 `task-invocation-parameters` 參數指定 `WINDOW_ID` 和 `WINDOW_TASK_ID` 參數：

------
#### [ Linux & macOS ]

   ```
   aws ssm update-maintenance-window-task \
       --window-id "mw-0c50858d01EXAMPLE" \
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE \
       --task-arn "AutoTestDoc" \
       --service-role-arn "arn:aws:iam:account-id:role/MyMaintenanceWindowServiceRole \
       --task-invocation-parameters "Automation={Parameters={InstanceId='{{RESOURCE_ID}}',initiator='{{WINDOW_ID}}.Task-{{WINDOW_TASK_ID}}'}}" \
       --priority 3 --max-concurrency 10 --max-errors 5
   ```

------
#### [ Windows ]

   ```
   aws ssm update-maintenance-window-task ^
       --window-id "mw-0c50858d01EXAMPLE" ^
       --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^
       --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE ^
       --task-arn "AutoTestDoc" ^
       --service-role-arn "arn:aws:iam:account-id:role/MyMaintenanceWindowServiceRole ^
       --task-invocation-parameters "Automation={Parameters={InstanceId='{{RESOURCE_ID}}',initiator='{{WINDOW_ID}}.Task-{{WINDOW_TASK_ID}}'}}" ^
       --priority 3 --max-concurrency 10 --max-errors 5
   ```

------

   系統會傳回與以下相似的資訊。

   ```
   {
       "WindowId": "mw-0c50858d01EXAMPLE",
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
       "Targets": [
           {
               "Key": "WindowTargetIds",
               "Values": [
                   "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
               ]
           }
       ],
       "TaskArn": "AutoTestDoc",
       "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
       "TaskParameters": {},
       "TaskInvocationParameters": {
           "Automation": {
               "Parameters": {
                   "multi": [
                       "{{WINDOW_TASK_ID}}"
                   ],
                   "single": [
                       "{{WINDOW_ID}}"
                   ]
               }
           }
       },
       "Priority": 0,
       "MaxConcurrency": "10",
       "MaxErrors": "5",
       "Name": "My-Automation-Task",
       "Description": "A description for my Automation task"
   }
   ```

# 教學課程：使用 刪除維護時段 AWS CLI
<a name="mw-cli-tutorial-delete-mw"></a>

若要刪除您在這些教學中所建立的維護時段，請執行下列命令。

```
aws ssm delete-maintenance-window --window-id "mw-0c50858d01EXAMPLE"
```

系統會傳回與以下相似的資訊。

```
{
   "WindowId":"mw-0c50858d01EXAMPLE"
}
```

# 教學課程：使用主控台建立修補維護時段
<a name="maintenance-window-tutorial-patching"></a>

**重要**  
您可以繼續使用這個舊版主題來建立維護時段，以進行修補。不過，建議您改用修補程式政策。如需詳細資訊，請參閱[Quick Setup中的修補程式政策組態](patch-manager-policies.md)及[使用 Quick Setup 修補程式政策設定組織中執行個體的修補](quick-setup-patch-manager.md)。

為了盡可能降低對伺服器可用性的影響，建議您設定維護時段在不會插斷您商業運作的期間執行修補。

在開始此程序之前 AWS Systems Manager，您必須為 中的Maintenance Windows工具 設定角色和許可。如需詳細資訊，請參閱[設定 Maintenance Windows](setting-up-maintenance-windows.md)。

**建立維護時段以進行修補**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Maintenance Windows**。

1. 選擇**建立維護時段**。

1. 針對 **Name (名稱)**，輸入名稱，並將其指定為維護時段以修補關鍵與重要的更新。

1. 在**描述**，請輸入描述。

1. 如果您想允許維護時段任務在受管節點上執行 (即使您尚未將這些節點註冊為目標)，請選擇 **Allow unregistered targets** (允許未註冊目標)。

   如果您選擇此選項，即可在向維護時段註冊任務時選擇未註冊的節點 (依據節點 ID)。

   如果您未選擇此選項，則必須在向維護時段註冊任務時選擇先前註冊過的目標。

1. 在 **Schedule (排程)** 區段頂端，使用三個排程選項之一來指定維護時段的排程。

   如需有關建立 Cron/Rate 運算式的詳細資訊，請參閱[參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)。

1. 針對 **Duration (持續時間)**，輸入維護時段將執行的時數。您指定的值會根據維護時段的開始時間，決定維護時段的特定結束時間。在產生的結束時間減去您在下一個步驟中為 **Stop initiating tasks (停止啟動任務)** 指定的小時數過後，將不允許啟動任何維護時段任務。

   例如，如果維護時段從下午 3 點開始，持續時間為三小時，而 **Stop initiating tasks (停止啟動任務)** 的值為一小時，則在下午 5 點之後無法啟動任何維護時段任務。

1. 針對 **Stop initiating tasks (停止初始任務)**，輸入在維護時段執行結束之前，系統應該停止排程新任務的時數。

1. (選用) 對於 **Window start date** (時段開始日期)，依照 ISO-8601 Extended 格式，指定您希望開始啟用維護時段的日期和時間。這可讓您延遲啟用維護時段，直到指定的未來日期為止。

1. (選用) 對於 **Window end date** (時段結束日期)，依照 ISO-8601 Extended 格式，指定您希望停用維護時段的日期和時間。這可讓您設定不再執行維護時段的未來日期和時間點。

1. (選用) 對於**排程時區**，以網際網路號碼分配局 (IANA) 格式，指定排程的維護時段執行所依據的時區。例如："America/Los\$1Angeles"、"etc/UTC" 或 "Asia/Seoul"。

   如需有關有效格式的詳細資訊，請參閱 IANA 網站上的[時區資料庫有效格式](https://www.iana.org/time-zones)。

1. (選用) 在 **Manage tags (管理標籤)** 區域，將一或多個標籤金鑰名稱/值對套用到維護時段。

   標籤是您指派給資源的選用性中繼資料。標籤允許您以不同的方式 (例如用途、擁有者或環境) 將資源分類。例如，您可能想要標記此維護時段，以識別其執行的任務類型。在這種情況下，您可以指定以下索引鍵名稱/值對：
   + `Key=TaskType,Value=Patching`

1. 選擇**建立維護時段**。

1. 在維護時段清單中，選擇您剛建立的維護時段，然後選擇 **Actions (動作)**、**Register targets (註冊目標)**。

1. (選用) 在 **Maintenance window target details (維護時段目標詳細資訊)** 部分，提供此目標的名稱、描述及擁有者資訊 (您的名稱或別名)。

1. 對於**目標選擇**，選擇**指定執行個體標籤**。

1. 對於**指定執行個體標籤**，輸入標籤鍵和標籤值，以識別要向維護時段註冊的節點，然後選擇**新增**。

1. 選擇 **Register target (註冊目標)**。系統會建立一個維護時段目標。

1. 在您建立的維護時段的詳細資訊頁面中，選擇 **Actions (動作)**、**Register run command task (註冊執行命令任務)**。

1. (選用) 在 **Maintenance window task details (維護時段任務詳細資訊)** 中提供此任務的名稱與描述。

1. 如需 **Command document** (命令文件)，請選擇 `AWS-RunPatchBaseline`。

1. 在 **Task priority (任務優先順序)** 中選擇優先順序。零 (`0`) 是最高的優先順序。

1. 針對 **Targets (目標)**，請在 **Target by (目標依據)** 下，選擇您之前在此程序建立的維護時段目標。

1. 對於**速率控制**：
   + 在**並行**中，指定可同時執行命令的受管節點數目或百分比。
**注意**  
如果您透過指定套用至受管節點的標籤或指定 AWS 資源群組來選取目標，而且您不確定目標的受管節點數量，則透過指定百分比來限制可同時執行文件的目標數量。
   + 在 **Error threshold** (錯誤閾值) 中，指定在特定數目或百分比之節點上的命令失敗之後，停止在其他受管節點上執行命令。例如，如果您指定三個錯誤，則 Systems Manager 會在收到第四個錯誤時停止傳送命令。仍在處理命令的受管節點也可能會傳送錯誤。

1. (選用) 對於 **IAM 服務角色**，請選擇一個角色，以便為 Systems Manager 提供執行維護時段任務時承擔的許可。

   如果您未指定服務角色 ARN，則 Systems Manager 會使用帳戶中的服務連結角色。如果帳戶中不存在適當的 Systems Manager 服務連結角色，則會在成功註冊任務時建立該角色。
**注意**  
為了改善安全狀態，強烈建議您建立自訂政策和自訂服務角色，以便執行維護時段任務。您可以制定政策，僅提供特定維護時段任務所需的許可。如需詳細資訊，請參閱[設定 Maintenance Windows](setting-up-maintenance-windows.md)。

1. (選用) 針對**輸出選項**，若要將命令輸出儲存至檔案，請選取**啟用將輸出寫入 S3** 方塊。在方塊中輸入儲存貯體和字首 (資料夾) 名稱。
**注意**  
授予能力以將資料寫入至 S3 儲存貯體的 S3 許可，會是指派給受管節點之執行個體設定檔的許可，而不是執行此任務之 IAM 使用者的許可。如需詳細資訊，請參閱[設定 Systems Manager 所需的執行個體許可](setup-instance-permissions.md)或[建立混合環境的 IAM 服務角色](hybrid-multicloud-service-role.md)。此外，若指定的 S3 儲存貯體位於不同的 AWS 帳戶內，請驗證與受管節點相關聯的執行個體設定檔或 IAM 服務角色是否具有寫入該儲存貯體的必要許可。

   若要將輸出串流至 Amazon CloudWatch Logs 日誌群組，請選取 **CloudWatch output** (CloudWatch 輸出) 方塊。在方塊中輸入日誌群組名稱。

1. 在 **SNS notifications** (SNS 通知) 區段中，如果您要傳送有關命令執行狀態的通知，請選取 **Enable SNS notifications** (啟用 SNS 通知) 核取方塊。

   如需為 Run Command 設定 Amazon SNS 通知的詳細資訊，請參閱 [使用 Amazon SNS 通知監控 Systems Manager 狀態變更](monitoring-sns-notifications.md)。

1. 對於 **Parameters (參數)**：
   + 在 **Operation (操作)** 中選擇 **Scan (掃描)** 以掃描遺漏的修補程式，或選擇 **Install (安裝)** 以掃描並安裝遺漏的修補程式。
   + 您無需在 **Snapshot Id (快照 ID)** 欄位中輸入任何資訊。此系統會自動產生並提供此參數。
   + 除非您希望 Patch Manager 使用與修補基準不同的修補程式集，否則不需要在 **Install Override List** (安裝覆寫清單) 欄位中輸入任何內容。如需相關資訊，請參閱[參數名稱：`InstallOverrideList`](patch-manager-aws-runpatchbaseline.md#patch-manager-aws-runpatchbaseline-parameters-installoverridelist)。
   + 針對**重新啟動選項**，指定如果在 `Install` 操作期間安裝了修補程式，或是 Patch Manager 偵測到自上次節點重新啟動後安裝的其他修補程式，是否要重新啟動節點。如需相關資訊，請參閱[參數名稱：`RebootOption`](patch-manager-aws-runpatchbaseline.md#patch-manager-aws-runpatchbaseline-parameters-norebootoption)。
   + (選用) 在 **Comment (註解)** 中輸入有關此命令的追蹤註記或提醒。
   + 在 **Timeout (seconds) (逾時 (秒))** 中，輸入系統應等待操作完成的時間，超過此時間將視為失敗。

1. 選擇 **Register run command task** (註冊執行命令任務)。

在維護時段任務完成後，您可以在 Systems Manager 主控台的 [Fleet Manager](fleet-manager.md) 工具中檢視修補程式合規詳細資訊。

您也可以在 [Patch Manager](patch-manager.md) 工具中的**合規報告**索引標籤上檢視合規資訊。

您也可以使用 [DescribePatchGroupState](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_DescribePatchGroupState.html) 與 [DescribeInstancePatchStatesForPatchGroup](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_DescribeInstancePatchStatesForPatchGroup.html) API，以檢視合規的詳細資訊。如需有關修補程式合規資料的詳細資訊，請參閱[關於修補程式合規](compliance-about.md#compliance-monitor-patch)。

# 使用維護時段進行修補的排程
<a name="sysman-patch-scheduletasks"></a>

在您設定修補基線 (以及選用的修補程式群組) 之後，即可使用維護時段將修補程式套用至您的節點。維護時段可讓您指定執行修補程式的時間，不中斷商業運作，以降低對伺服器可用性的影響。維護時段的運作方式如下：

1. 建立維護時段，其中包含您修補操作的排程。

1. 選擇維護時段目標，方法是指定 `Patch Group` 或 `PatchGroup` 標籤作為標籤名稱，並指定您已定義 Amazon Elastic Compute Cloud (Amazon EC2) 標籤的任何值，例如例如「Web 伺服器」或「US-EAST-PROD」。(如果您已在 [EC2 執行個體中繼資料中允許標籤](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#allow-access-to-tags-in-IMDS)，則必須使用 `PatchGroup`，不留空格。)

1. 建立新的維護時段任務，並指定 `AWS-RunPatchBaseline` 文件。

當您設定任務時，您可以選擇掃描節點，或掃描節點並在其上安裝修補程式。如果您選擇掃描節點，Patch Manager ( AWS Systems Manager中的工具) 將掃描每個節點，並產生遺漏修補程式清單以供您查看。

如果您選擇掃描並安裝修補程式，則 Patch Manager 會掃描每個節點，並將已安裝修補程式清單與基準中的已核准修補程式清單進行比較。Patch Manager 會識別遺漏的修補程式，然後下載並安裝所有遺漏且已核准的修補程式。

如果您要執行一次性掃描或進行安裝以修復問題，可使用 Run Command 直接呼叫 `AWS-RunPatchBaseline` 文件。

**重要**  
安裝修補程式之後，Systems Manager 將重新啟動每個節點。需要重新啟動是為了確保修補程式已正確安裝，並確保系統未讓節點處於可能不良的狀態。(例外：如果 `AWS-RunPatchBaseline` 文件中的 `RebootOption` 參數設為 `NoReboot`，受管節點不會在 Patch Manager 執行之後重新啟動。如需詳細資訊，請參閱 [參數名稱：`RebootOption`](patch-manager-aws-runpatchbaseline.md#patch-manager-aws-runpatchbaseline-parameters-norebootoption)。) 

# 註冊維護時段任務時使用虛擬參數
<a name="maintenance-window-tasks-pseudo-parameters"></a>

在 Maintenance Windows ( AWS Systems Manager中的工具) 中註冊任務時，可以指定四種任務類型特有的參數。(在 CLI 命令中，這些都是使用 `--task-invocation-parameters` 選項提供。)

 您也可以使用「虛擬參數」**語法來參考特定的值，例如 `{{RESOURCE_ID}}`、`{{TARGET_TYPE}}` 和 `{{WINDOW_TARGET_ID}}`。維護時段任務執行時，它會傳遞正確的值，而不是虛擬參數預留位置。本主題後面的[支援的虛擬參數](#pseudo-parameters)中提供了您可以使用之虛擬參數的完整清單。

**重要**  
對於目標類型 `RESOURCE_GROUP`，視任務所需的 ID 格式而定，您可以在任務執行時選擇使用 `{{TARGET_ID}}` 和 `{{RESOURCE_ID}}` 參考資源。`{{TARGET_ID}}` 會傳回資源的完整 ARN。`{{RESOURCE_ID}}` 只會傳回資源的較短名稱或 ID，如下列範例所示。  
`{{TARGET_ID}}` 格式：`arn:aws:ec2:us-east-1:123456789012:instance/i-02573cafcfEXAMPLE`
`{{RESOURCE_ID}}` 格式：`i-02573cafcfEXAMPLE`
對於目標類型 `INSTANCE`，`{{TARGET_ID}}` 和 `{{RESOURCE_ID}}` 參數都只產生執行個體 ID。如需詳細資訊，請參閱[支援的虛擬參數](#pseudo-parameters)。  
`{{TARGET_ID}}` 和 `{{RESOURCE_ID}}`只能用來將 AWS 資源 IDs 傳遞至 Automation、Lambda 和 Step Functions 任務。這兩個虛擬參數不能與 Run Command 任務搭配使用。

## 虛擬參數範例
<a name="pseudo-parameter-examples"></a>

假設您 AWS Lambda 任務的承載需要依其 ID 參考執行個體。

無論您是使用 `INSTANCE` 或 `RESOURCE_GROUP` 維護時段目標，都可以使用 `{{RESOURCE_ID}}` 虛擬參數加以實現。例如：

```
"TaskArn": "arn:aws:lambda:us-east-2:111122223333:function:SSMTestFunction",
    "TaskType": "LAMBDA",
    "TaskInvocationParameters": {
        "Lambda": {
            "ClientContext": "ew0KICAi--truncated--0KIEXAMPLE",
            "Payload": "{ \"instanceId\": \"{{RESOURCE_ID}}\" }",
            "Qualifier": "$LATEST"
        }
    }
```

如果除了 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體之外，您的 Lambda 任務還要針對其他受支援的目標類型執行 (例如 Amazon DynamoDB 資料表)，則可以使用相同的語法，`{{RESOURCE_ID}}` 只會產生資料表的名稱。但是，如果您需要資料表的完整 ARN，請使用 `{{TARGET_ID}}`，如下列範例所示。

```
"TaskArn": "arn:aws:lambda:us-east-2:111122223333:function:SSMTestFunction",
    "TaskType": "LAMBDA",
    "TaskInvocationParameters": {
        "Lambda": {
            "ClientContext": "ew0KICAi--truncated--0KIEXAMPLE",
            "Payload": "{ \"tableArn\": \"{{TARGET_ID}}\" }",
            "Qualifier": "$LATEST"
        }
    }
```

相同的語法適用於定位執行個體或其他資源類型。已將多個資源類型新增至資源群組時，任務會針對每個適當的資源執行。

**重要**  
並非資源群組中包含的所有資源類型都會產生 `{{RESOURCE_ID}}` 參數的值。如需支援的資源類型清單，請參閱[支援的虛擬參數](#pseudo-parameters)。

另一個例子是，若要執行可停止 EC2 執行個體的 Automation 任務，您可將 `AWS-StopEC2Instance` Systems Manager 文件 (SSM 文件) 指定為 `TaskArn` 值，並使用 `{{RESOURCE_ID}}` 虛擬參數：

```
"TaskArn": "AWS-StopEC2Instance",
    "TaskType": "AUTOMATION"
    "TaskInvocationParameters": {
        "Automation": {
            "DocumentVersion": "1",
            "Parameters": {
                "instanceId": [
                    "{{RESOURCE_ID}}"
                ]
            }
        }
    }
```

若要執行複製 Amazon Elastic Block Store (Amazon EBS)磁碟區快照的 Automation 任務，您可以將 `AWS-CopySnapshot` SSM 文件指定為 `TaskArn` 值，並使用 `{{RESOURCE_ID}}` 虛擬參數：

```
"TaskArn": "AWS-CopySnapshot",
    "TaskType": "AUTOMATION"
    "TaskInvocationParameters": {
        "Automation": {
            "DocumentVersion": "1",
            "Parameters": {
                "SourceRegion": "us-east-2",
                "targetType":"RESOURCE_GROUP",
                "SnapshotId": [
                    "{{RESOURCE_ID}}"
                ]
            }
        }
    }
```

## 支援的虛擬參數
<a name="pseudo-parameters"></a>

以下清單說明您可以在 `--task-invocation-parameters` 選項中使用 `{{PSEUDO_PARAMETER}}` 語法指定的虛擬參數。
+ **`WINDOW_ID`**：目標維護時段 ID。
+ **`WINDOW_TASK_ID`**：正在執行的時段任務 ID。
+ **`WINDOW_TARGET_ID`**：包含目標的目標時段的 ID (目標 ID)。
+ **`WINDOW_EXECUTION_ID`**：目前執行時段的 ID。
+ **`TASK_EXECUTION_ID`**：目前執行任務的 ID。
+ **`INVOCATION_ID`**：目前呼叫的 ID。
+ **`TARGET_TYPE`**：目標類型。支援的類型包括 `RESOURCE_GROUP` 和 `INSTANCE`。
+ **`TARGET_ID`**: 

  如果您指定的目標類型為 `INSTANCE`，則 `TARGET_ID` 虛擬參數會由執行個體的 ID 取代。例如 `i-078a280217EXAMPLE`。

  如果您指定的目標類型為 `RESOURCE_GROUP`，則任務執行所參考的值為資源的完整 ARN。例如：`arn:aws:ec2:us-east-1:123456789012:instance/i-078a280217EXAMPLE`。下表提供資源群組中特定資源類型的範例 `TARGET_ID` 值。
**注意**  
`TARGET_ID` 不支援 Run Command 任務。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/systems-manager/latest/userguide/maintenance-window-tasks-pseudo-parameters.html)
+ **`RESOURCE_ID`**：資源群組中所包含資源類型的簡短 ID。下表提供資源群組中特定資源類型的範例 `RESOURCE_ID` 值。
**注意**  
`RESOURCE_ID` 不支援 Run Command 任務。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/systems-manager/latest/userguide/maintenance-window-tasks-pseudo-parameters.html)
**注意**  
如果您指定的 AWS 資源群組包含不會產生`RESOURCE_ID`值的資源類型，且未列在上表中，則不會填入 `RESOURCE_ID` 參數。該資源仍會發生執行呼叫。在這些情況下，請改用 `TARGET_ID` 虛擬參數，這將被取代為資源的完整 ARN。

# 維護時段排程與作用期間選項
<a name="maintenance-windows-schedule-options"></a>

當您建立維護時段時，您必須使用 [Cron 或 Rate 表達式](reference-cron-and-rate-expressions.md)，指定維護時段執行的頻繁程度。或者，您可以指定一段日期範圍，讓維護時段能根據其定期排程來執行，以及該定期排程依循的時區。

不過請注意，該時區選項與開始日期和結束日期選項不互相影響。您指定的任何開始日期和結束日期時間 (包含或不含時區位移) 只能決定維護時段能定期執行的「有效期間」 **。時區選項決定維護時段排程在有效期間「之中」**定期執行的國際時區基準。

**注意**  
您可以使用 ISO-8601 時間戳記格式指定開始和結束日期。例如：`2021-04-07T14:29:00-08:00`  
您可以使用網際網路號碼分配局 (IANA) 格式指定時區。例如，`America/Chicago`、`Europe/Berlin` 或 `Asia/Tokyo`。

**Topics**
+ [範例 1：指定維護時段開始日期](#schedule-example-start-date)
+ [範例 2：指定維護時段開始日期和結束日期](#schedule-example-start-end-date)
+ [範例 3：建立只執行一次的維護時段](#schedule-example-one-time)
+ [範例 4：指定維護時段的排程偏移天數](#schedule-example-schedule-offset)

## 範例 1：指定維護時段開始日期
<a name="schedule-example-start-date"></a>

假設您使用 AWS Command Line Interface (AWS CLI) 建立具有下列選項的維護時段：
+ `--start-date 2021-01-01T00:00:00-08:00`
+ `--schedule-timezone "America/Los_Angeles"`
+ `--schedule "cron(0 09 ? * WED *)"`

例如：

------
#### [ Linux & macOS ]

```
aws ssm create-maintenance-window \
    --name "My-LAX-Maintenance-Window" \
    --allow-unassociated-targets \
    --duration 3 \
    --cutoff 1 \
    --start-date 2021-01-01T00:00:00-08:00 \
    --schedule-timezone "America/Los_Angeles" \
    --schedule "cron(0 09 ? * WED *)"
```

------
#### [ Windows ]

```
aws ssm create-maintenance-window ^
    --name "My-LAX-Maintenance-Window" ^
    --allow-unassociated-targets ^
    --duration 3 ^
    --cutoff 1 ^
    --start-date 2021-01-01T00:00:00-08:00 ^
    --schedule-timezone "America/Los_Angeles" ^
    --schedule "cron(0 09 ? * WED *)"
```

------

這表示維護時段在其指定的開始日期和時間*前*無法第一次執行，也就是美國太平洋時間 2021 年 1 月 1 日星期五的午夜。(此時區比 UTC 時間慢 8 個小時。) 在此情況下，時段期間的開始日期和時間不代表維護時段第一次執行的時間。結合 `--schedule-timezone` 和 `--schedule` 值，代表維護時段會在美國太平洋時區 (IANA 格式的 "America/Los Angeles" (美洲/洛杉磯)) 每週三的上午 9 點執行。啟用期間的第一次執行，會在美國太平洋時間 2021 年 1 月 4 日週三的上午 9 點。

## 範例 2：指定維護時段開始日期和結束日期
<a name="schedule-example-start-end-date"></a>

假設您接下來使用這些選項建立維護時段：
+ `--start-date 2019-01-01T00:03:15+09:00`
+ `--end-date 2019-06-30T00:06:15+09:00`
+ `--schedule-timezone "Asia/Tokyo"`
+ `--schedule "rate(7 days)"`

例如：

------
#### [ Linux & macOS ]

```
aws ssm create-maintenance-window \
    --name "My-NRT-Maintenance-Window" \
    --allow-unassociated-targets \
    --duration 3 \
    --cutoff 1 \
    --start-date 2019-01-01T00:03:15+09:00 \
    --end-date 2019-06-30T00:06:15+09:00 \
    --schedule-timezone "Asia/Tokyo" \
    --schedule "rate(7 days)"
```

------
#### [ Windows ]

```
aws ssm create-maintenance-window ^
    --name "My-NRT-Maintenance-Window" ^
    --allow-unassociated-targets ^
    --duration 3 ^
    --cutoff 1 ^
    --start-date 2019-01-01T00:03:15+09:00 ^
    --end-date 2019-06-30T00:06:15+09:00 ^
    --schedule-timezone "Asia/Tokyo" ^
    --schedule "rate(7 days)"
```

------

此維護時段的啟用期間將會在日本標準時間 2019 年 1 月 1 日的上午 3:15 開始。此維護時段的有效期間會在日本標準時間 2019 年 6 月 30 日星期日的上午 6:15 結束。(此時區比 UTC 時間快 9 個小時)。結合 `--schedule-timezone` 和 `--schedule` 值，代表維護時段會在日本標準時區 (IANA 格式的 "Asia/Tokyo" (亞洲/東京)) 每週二的上午 3:15 執行。這是因為維護時段每七天執行一次，從 1 月 1 日週二上午 3:15 啟用。最後一次執行會在日本標準時間 2019 年 6 月 25 日星期二的上午 3:15。這是已啟用維護時段期間在五天之後結束之前的最後一個週二。

## 範例 3：建立只執行一次的維護時段
<a name="schedule-example-one-time"></a>

現在您使用此選項建立維護時段：
+ `--schedule "at(2020-07-07T15:55:00)"`

例如：

------
#### [ Linux & macOS ]

```
aws ssm create-maintenance-window \
    --name "My-One-Time-Maintenance-Window" \
    --schedule "at(2020-07-07T15:55:00)" \
    --duration 5 \
    --cutoff 2 \
    --allow-unassociated-targets
```

------
#### [ Windows ]

```
aws ssm create-maintenance-window ^
    --name "My-One-Time-Maintenance-Window" ^
    --schedule "at(2020-07-07T15:55:00)" ^
    --duration 5 ^
    --cutoff 2 ^
    --allow-unassociated-targets
```

------

此維護時段只會在 UTC 時間 2020 年 7 月 7 日的下午 3:55 執行一次。維護時段已啟用為視需要執行最多五個小時，但在維護時段期間結束的兩個小時之前都無法啟動新的任務。

## 範例 4：指定維護時段的排程偏移天數
<a name="schedule-example-schedule-offset"></a>

現在您使用此選項建立維護時段：

```
--schedule-offset 2
```

例如：

------
#### [ Linux & macOS ]

```
aws ssm create-maintenance-window \
    --name "My-Cron-Offset-Maintenance-Window" \
    --schedule "cron(0 30 23 ? * TUE#3 *)" \
    --duration 4 \
    --cutoff 1 \
    --schedule-offset 2 \
    --allow-unassociated-targets
```

------
#### [ Windows ]

```
aws ssm create-maintenance-window ^
    --name "My-Cron-Offset-Maintenance-Window" ^
    --schedule "cron(0 30 23 ? * TUE#3 *)" ^
    --duration 4 ^
    --cutoff 1 ^
    --schedule-offset 2 ^
    --allow-unassociated-targets
```

------

排程偏移是在執行維護時段之前，在 CRON 表達式所指定的日期和時間之後等待的天數。

在上述範例中，CRON 表達式會排定維護時段，在每個月的第三個週二晚上 11:30 執行：

```
--schedule "cron(0 30 23 ? * TUE#3 *)
```

但是，包括 `--schedule-offset 2` 表示維護時段將在每個月的第三個星期二「後」**兩天的晚上 11:30 執行。

僅 CRON 表達式支援排程偏移。

**詳細資訊**  
+ [參考：Systems Manager 的 Cron 和 Rate 運算式](reference-cron-and-rate-expressions.md)
+ [使用主控台建立維護時段](sysman-maintenance-create-mw.md)
+ [教學課程：使用 建立和設定維護時段 AWS CLI](maintenance-windows-cli-tutorials-create.md)
+ 《*AWS Systems Manager API 參考*》中的 [CreateMaintenanceWindow](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_CreateMaintenanceWindow.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/ssm/create-maintenance-window.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/create-maintenance-window.html) 在 *AWS Systems Manager AWS CLI 命令參考的 區段中*
+ IANA 網站上的[時區資料庫](https://www.iana.org/time-zones)

# 註冊不含目標的維護時段任務
<a name="maintenance-windows-targetless-tasks"></a>

對於建立的每個維護時段，您可以指定執行維護時段時要執行的一或多個任務。在大多數情況下，您必須指定要在其中執行任務的資源或目標。但是，在某些情況下，您不需要在任務中明確指定目標。

必須為維護時段 Systems Manager Run Command 類型任務指定一或多個目標。根據任務的性質，其他維護時段任務類型 (Systems Manager Automation AWS Lambda和 AWS Step Functions) 的目標為選用。

對於 Lambda 和 Step Functions 任務類型，是否需要目標取決於您建立的函數或狀態機的內容。

**注意**  
當任務已註冊目標 AWS Lambda時，自動化和 AWS Step Functions 任務會從資源群組和標籤解析目標，並為每個已解析的資源傳送一個調用，這會導致多個任務調用。例如，假設您只需要對在包含多個執行個體的資源群組中註冊的 Lambda 任務進行一次調用。在此情況下，如果您在 AWS 管理主控台中工作，請選擇**註冊 Lambda 任務**或**編輯 Lambda 任務**頁面中的**不需要任務目標**選項。如果您使用 AWS CLI 命令，請勿在執行 [https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html) 命令或 [https://docs.aws.amazon.com/cli/latest/reference/ssm/update-maintenance-window-task.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/update-maintenance-window-task.html) 命令時使用 `--targets` 參數指定目標。

在許多情況下，您不需要明確指定自動化任務的目標。例如，假設您正在建立 Automation 類型任務來使用 `AWS-UpdateLinuxAmi` Runbook 更新 Linux 的 Amazon Machine Image (AMI)。當任務執行時，AMI 已更新為可用的最新版本 Linux 發行版本套件和 Amazon 軟體。從 AMI 建立的新執行個體已經安裝這些更新。因為在 Runbook 的輸入參數中指定了要更新的 AMI ID，所以不需要在維護時段任務中再次指定目標。

同樣地，假設您使用 AWS Command Line Interface (AWS CLI) 來註冊使用 `AWS-RestartEC2Instance` Runbook 的維護時段自動化任務。因為要重新啟動的節點是在 `--task-invocation-parameters` 參數中，所以您不需要指定 `--targets` 選項。

**注意**  
對於未指定目標的維護時段任務，您無法提供 `--max-errors` 和 `--max-concurrency` 的值。系統會插入預留位置值 `1`，這可能會在回應指令 (例如 [https://docs.aws.amazon.com/cli/latest/reference/ssm/describe-maintenance-window-tasks.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/describe-maintenance-window-tasks.html) 和 [https://docs.aws.amazon.com/cli/latest/reference/ssm/get-maintenance-window-task.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/get-maintenance-window-task.html)) 中回報。這些值不會影響任務的執行，可以忽略。

下列範例示範針對無目標維護時段任務，省略 `--targets`、`--max-errors` 和 `--max-concurrency` 選項。

------
#### [ Linux & macOS ]

```
aws ssm register-task-with-maintenance-window \
    --window-id "mw-ab12cd34eEXAMPLE" \
    --service-role-arn "arn:aws:iam::123456789012:role/MaintenanceWindowAndAutomationRole" \
    --task-type "AUTOMATION" \
    --name "RestartInstanceWithoutTarget" \
    --task-arn "AWS-RestartEC2Instance" \
    --task-invocation-parameters "{\"Automation\":{\"Parameters\":{\"InstanceId\":[\"i-02573cafcfEXAMPLE\"]}}}" \
    --priority 10
```

------
#### [ Windows ]

```
aws ssm register-task-with-maintenance-window ^
    --window-id "mw-ab12cd34eEXAMPLE" ^
    --service-role-arn "arn:aws:iam::123456789012:role/MaintenanceWindowAndAutomationRole" ^
    --task-type "AUTOMATION" ^
    --name "RestartInstanceWithoutTarget" ^
    --task-arn "AWS-RestartEC2Instance" ^
    --task-invocation-parameters "{\"Automation\":{\"Parameters\":{\"InstanceId\":[\"i-02573cafcfEXAMPLE\"]}}}" ^
    --priority 10
```

------

**注意**  
對於 2020 年 12 月 23 日之前註冊的維護時段任務：如果您為任務指定了目標，且不再需要一個目標，您可以使用 Systems Manager 主控台或 [https://docs.aws.amazon.com/cli/latest/reference/ssm/update-maintenance-window-task.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/update-maintenance-window-task.html) AWS CLI 命令更新該任務以移除目標。

**詳細資訊**  
+ [錯誤訊息：「沒有目標的維護時段任務不支援 MaxConcurrency 值」和「沒有目標的維護時段任務不支援 MaxErrors 值」](troubleshooting-maintenance-windows.md#maxconcurrency-maxerrors-not-supported)

# 對維護時段進行故障診斷
<a name="troubleshooting-maintenance-windows"></a>

使用以下資訊以協助您對維護時段的問題進行故障診斷。

**Topics**
+ [編輯任務錯誤：在可編輯維護時段任務的頁面上，IAM 角色清單傳回錯誤訊息：「我們無法找到為此任務指定的 IAM 維護時段角色。它可能已刪除，也可能尚未建立。」](#maintenance-window-role-troubleshooting)
+ [並非所有維護時段目標都會更新](#targets-not-updated)
+ [任務失敗並顯示任務叫用狀態：「提供的角色未包含正確的 SSM 許可。」](#incorrect-ssm-permissions)
+ [任務失敗並顯示錯誤消息：「正在驗證和解決步驟輸入時，步驟失敗」](#step-fails)
+ [錯誤訊息：「沒有目標的維護時段任務不支援 MaxConcurrency 值」和「沒有目標的維護時段任務不支援 MaxErrors 值」](#maxconcurrency-maxerrors-not-supported)

## 編輯任務錯誤：在可編輯維護時段任務的頁面上，IAM 角色清單傳回錯誤訊息：「我們無法找到為此任務指定的 IAM 維護時段角色。它可能已刪除，也可能尚未建立。」
<a name="maintenance-window-role-troubleshooting"></a>

**問題 1**：建立任務後，您最初指定的 AWS Identity and Access Management (IAM) 維護時段角色已刪除。

**Possible fix** (可能的修正)：1) 選取不同的 IAM 維護時段角色 (如果您帳戶中有現有的)，或者建立新的角色，並為任務選取此角色。

**問題 2**：如果任務是使用 AWS Command Line Interface (AWS CLI) AWS Tools for Windows PowerShell或 AWS SDK 建立的，則可能已指定不存在的 IAM 維護時段角色名稱。例如，在建立任務之前，IAM 維護時段角色可能已遭到刪除，或者角色名稱輸入錯誤，例如 **myrole** 而不是 **my-role**。

**Possible fix** (可能的修正)：選取要使用之 IAM 維護時段角色的正確名稱，或建立新的角色，並為任務指定此角色。

## 並非所有維護時段目標都會更新
<a name="targets-not-updated"></a>

**問題：**您注意到維護時段任務並未在維護時段目標的所有資源上執行。例如，在維護時段的執行結果中，該資源的任務會標示為失敗或逾時。

**解決方案：**維護時段任務未在目標資源上執行的最常見原因涉及連線和可用性。例如：
+ Systems Manager 在維護時段操作之前或期間失去資源的連線。
+ 在維護時段操作期間，資源已離線或停用。

您可以等待下一個排定的維護時段時間，在資源上執行任務。您可以在無法使用或離線的資源上手動執行維護時段任務。

## 任務失敗並顯示任務叫用狀態：「提供的角色未包含正確的 SSM 許可。」
<a name="incorrect-ssm-permissions"></a>

**問題**：您已為任務指定維護時段服務角色，但任務無法成功執行，且任務叫用狀態會報告「提供的角色未包含正確的 SSM 許可」。
+ **解決方案**：在 [任務 1：使用主控台為維護時段服務角色建立自訂政策](configuring-maintenance-window-permissions-console.md#create-custom-policy-console) 中，我們提供您可以連接至[自訂維護時段服務角色](configuring-maintenance-window-permissions-console.md#create-custom-role-console)的基本政策。此政策包含許多任務場景所需的許可。不過，由於您可以執行的任務種類繁多，您可能需要在維護時段角色的政策中提供其他許可。

  例如，某些自動化動作適用於 AWS CloudFormation 堆疊。因此，您可能需要為維護時段服務角色的政策新增其他許可：`cloudformation:CreateStack`、`cloudformation:DescribeStacks` 以及 `cloudformation:DeleteStack`。

  另一個例子：Automation Runbook `AWS-CopySnapshot` 需建立 Amazon Elastic Block Store (Amazon EBS) 快照的權限。因此，您可能需要新增許可 `ec2:CreateSnapshot`。

  如需有關 AWS 受管 Automation Runbook 所需角色許可的資訊，請參閱 Automation Runbook [AWS Systems Manager 參考中的 Runbook](https://docs.aws.amazon.com/systems-manager-automation-runbooks/latest/userguide/automation-runbook-reference.html) 說明。

  如需有關 AWS 受管 SSM 文件所需角色許可的資訊，請在文件區段 Systems Manager 主控台中檢閱[文件](https://console.aws.amazon.com//systems-manager/documents)的內容。

  如需有關 Step Functions 任務、Lambda 任務以及自訂 Automation Runbook 和 SSM 文件所需角色許可的資訊，請向這些資源的作者確認許可需求。

## 任務失敗並顯示錯誤消息：「正在驗證和解決步驟輸入時，步驟失敗」
<a name="step-fails"></a>

**問題**：您在任務中使用的 Automation Runbook 或 Systems Manager 命令文件需要您指定輸入，例如 `InstanceId` 或 `SnapshotId`，但未提供值或未正確提供值。
+ **Solution 1** (解決方案 1)：如果您的任務是針對單一資源 (例如單一節點或單一快照)，請在任務的輸入參數中輸入其 ID。
+ **Solution 2** (解決方案 2)：如果您的任務是針對多個資源，例如當您使用 Runbook `AWS-CreateImage` 時，從多個節點建立映像，您可以在輸入參數中使用支援維護時段任務的其中一個虛擬參數來表示命令中的節點 ID。

  以下命令使用 AWS CLI向維護時段註冊 Systems Manager Automation 任務。`--targets` 值表示維護時段目標 ID。此外，即使 `--targets` 參數會指定時段目標 ID，Automation Runbook 的參數需要提供節點 ID。在這種情況下，命令會使用虛擬參數 `{{RESOURCE_ID}}` 作為 `InstanceId` 值。

  **AWS CLI 命令：**

------
#### [ Linux & macOS ]

  下列範例命令會重新啟動屬於維護時段目標群組且ID 為 e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體。

  ```
  aws ssm register-task-with-maintenance-window \
      --window-id "mw-0c50858d01EXAMPLE" \
      --targets Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE \
      --task-arn "AWS-RestartEC2Instance" \
      --service-role-arn arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole \
      --task-type AUTOMATION \
      --task-invocation-parameters "Automation={DocumentVersion=5,Parameters={InstanceId='{{RESOURCE_ID}}'}}" \
      --priority 0 --max-concurrency 10 --max-errors 5 --name "My-Restart-EC2-Instances-Automation-Task" \
      --description "Automation task to restart EC2 instances"
  ```

------
#### [ Windows ]

  ```
  aws ssm register-task-with-maintenance-window ^
      --window-id "mw-0c50858d01EXAMPLE" ^
      --targets Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE ^
      --task-arn "AWS-RestartEC2Instance" ^
      --service-role-arn arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole ^
      --task-type AUTOMATION ^
      --task-invocation-parameters "Automation={DocumentVersion=5,Parameters={InstanceId='{{RESOURCE_ID}}'}}" ^
      --priority 0 --max-concurrency 10 --max-errors 5 --name "My-Restart-EC2-Instances-Automation-Task" ^
      --description "Automation task to restart EC2 instances"
  ```

------

  如需使用維護時段任務之虛擬參數的詳細資訊，請參閱 [註冊維護時段任務時使用虛擬參數](maintenance-window-tasks-pseudo-parameters.md) 和 [任務註冊範例](mw-cli-register-tasks-examples.md#task-examples)。

## 錯誤訊息：「沒有目標的維護時段任務不支援 MaxConcurrency 值」和「沒有目標的維護時段任務不支援 MaxErrors 值」
<a name="maxconcurrency-maxerrors-not-supported"></a>

**問題：**當註冊 Run Command 類型任務時，您必須至少指定一個目標，以執行任務。對於其他任務類型 （自動化和 AWS Step Functions) AWS Lambda，根據任務的性質，目標是選用的。選項 `MaxConcurrency` (同時執行任務的資源數量) 和 `MaxErrors` (在任務失敗之前在目標資源上執行任務的失敗次數) 不需要或不支援未指定目標的維護時段任務。如果在沒有指定任務目標時為這些選項的其中之一指定了值，則系統會產生這些錯誤訊息。

**解決方案**：如果您收到其中一個錯誤，請移除並行和錯誤閾值的值，然後繼續註冊或更新維護時段任務。

如需有關執行未指定目標之任務的詳細資訊，請參閱《*AWS Systems Manager 使用者指南*》中的 [註冊不含目標的維護時段任務](maintenance-windows-targetless-tasks.md)。