

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 搭配使用 `RegisterTargetWithMaintenanceWindow` 與 CLI
<a name="ssm_example_ssm_RegisterTargetWithMaintenanceWindow_section"></a>

下列程式碼範例示範如何使用 `RegisterTargetWithMaintenanceWindow`。

------
#### [ CLI ]

**AWS CLI**  
**範例 1：向維護時段註冊單一目標**  
下列 `register-target-with-maintenance-window` 範例示範向維護時段註冊執行個體。  

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-ab12cd34ef56gh78" \
    --target "Key=InstanceIds,Values=i-0000293ffd8c57862" \
    --owner-information "Single instance" \
    --resource-type "INSTANCE"
```
輸出：  

```
{
    "WindowTargetId":"1a2b3c4d-1a2b-1a2b-1a2b-1a2b3c4d-1a2"
}
```
**範例 2：使用執行個體 ID 向維護時段註冊多個目標**  
下列 `register-target-with-maintenance-window` 範例示範透過指定執行個體 ID，向維護時段註冊兩個執行個體。  

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-ab12cd34ef56gh78" \
    --target "Key=InstanceIds,Values=i-0000293ffd8c57862,i-0cb2b964d3e14fd9f" \
    --owner-information "Two instances in a list" \
    --resource-type "INSTANCE"
```
輸出：  

```
{
    "WindowTargetId":"1a2b3c4d-1a2b-1a2b-1a2b-1a2b3c4d-1a2"
}
```
**範例 3：使用資源標籤向維護時段註冊目標**  
下列 `register-target-with-maintenance-window` 範例示範透過指定已套用至執行個體的資源標籤，向維護時段註冊執行個體。  

```
aws ssm register-target-with-maintenance-window \
    --window-id "mw-06cf17cbefcb4bf4f" \
    --targets "Key=tag:Environment,Values=Prod" "Key=Role,Values=Web" \
    --owner-information "Production Web Servers" \
    --resource-type "INSTANCE"
```
輸出：  

```
{
    "WindowTargetId":"1a2b3c4d-1a2b-1a2b-1a2b-1a2b3c4d-1a2"
}
```
**範例 4：使用一組標籤索引鍵註冊目標**  
下列 `register-target-with-maintenance-window` 範例示範註冊執行個體，這些執行個體擁有一個或多個標指派的標籤索引鍵，無論其索引鍵值為何。  

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

```
{
    "WindowTargetId":"1a2b3c4d-1a2b-1a2b-1a2b-1a2b3c4d-1a2"
}
```
**範例 5：使用資源群組名稱註冊目標**  
下列 `register-target-with-maintenance-window` 範例示範註冊指定的資源群組，無論其中包含的資源類型為何。  

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

```
{
    "WindowTargetId":"1a2b3c4d-1a2b-1a2b-1a2b-1a2b3c4d-1a2"
}
```
如需詳細資訊，請參閱 *AWS Systems Manager 使用者指南*中的[向維護時段 (AWS CLI) 註冊目標執行個體](https://docs.aws.amazon.com/systems-manager/latest/userguide/mw-cli-tutorial-targets.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [RegisterTargetWithMaintenanceWindow](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ssm/register-target-with-maintenance-window.html)。

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

**Tools for PowerShell V4**  
**範例 1：此範例示範向維護時段註冊執行個體。**  

```
$option1 = @{Key="InstanceIds";Values=@("i-0000293ffd8c57862")}
Register-SSMTargetWithMaintenanceWindow -WindowId "mw-06cf17cbefcb4bf4f" -Target $option1 -OwnerInformation "Single instance" -ResourceType "INSTANCE"
```
**輸出：**  

```
d8e47760-23ed-46a5-9f28-927337725398
```
**範例 2：此範例示範向維護時段註冊多個執行個體。**  

```
$option1 = @{Key="InstanceIds";Values=@("i-0000293ffd8c57862","i-0cb2b964d3e14fd9f")}
Register-SSMTargetWithMaintenanceWindow -WindowId "mw-06cf17cbefcb4bf4f" -Target $option1 -OwnerInformation "Single instance" -ResourceType "INSTANCE"
```
**輸出：**  

```
6ab5c208-9fc4-4697-84b7-b02a6cc25f7d
```
**範例 3：此範例示範使用 EC2 標籤向維護時段註冊執行個體。**  

```
$option1 = @{Key="tag:Environment";Values=@("Production")}
Register-SSMTargetWithMaintenanceWindow -WindowId "mw-06cf17cbefcb4bf4f" -Target $option1 -OwnerInformation "Production Web Servers" -ResourceType "INSTANCE"
```
**輸出：**  

```
2994977e-aefb-4a71-beac-df620352f184
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [RegisterTargetWithMaintenanceWindow](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：此範例示範向維護時段註冊執行個體。**  

```
$option1 = @{Key="InstanceIds";Values=@("i-0000293ffd8c57862")}
Register-SSMTargetWithMaintenanceWindow -WindowId "mw-06cf17cbefcb4bf4f" -Target $option1 -OwnerInformation "Single instance" -ResourceType "INSTANCE"
```
**輸出：**  

```
d8e47760-23ed-46a5-9f28-927337725398
```
**範例 2：此範例示範向維護時段註冊多個執行個體。**  

```
$option1 = @{Key="InstanceIds";Values=@("i-0000293ffd8c57862","i-0cb2b964d3e14fd9f")}
Register-SSMTargetWithMaintenanceWindow -WindowId "mw-06cf17cbefcb4bf4f" -Target $option1 -OwnerInformation "Single instance" -ResourceType "INSTANCE"
```
**輸出：**  

```
6ab5c208-9fc4-4697-84b7-b02a6cc25f7d
```
**範例 3：此範例示範使用 EC2 標籤向維護時段註冊執行個體。**  

```
$option1 = @{Key="tag:Environment";Values=@("Production")}
Register-SSMTargetWithMaintenanceWindow -WindowId "mw-06cf17cbefcb4bf4f" -Target $option1 -OwnerInformation "Production Web Servers" -ResourceType "INSTANCE"
```
**輸出：**  

```
2994977e-aefb-4a71-beac-df620352f184
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [RegisterTargetWithMaintenanceWindow](https://docs.aws.amazon.com/powershell/v5/reference)。

------