

• 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)。

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

# 在 Parameter Store 中透過 Run Command 命令來使用參數
<a name="sysman-param-runcommand"></a>

您可以在 Run Command ( AWS Systems Manager中的工具) 中使用參數。如需詳細資訊，請參閱[AWS Systems Manager Run Command](run-command.md)。

## 使用主控台執行字串參數
<a name="param-test-console"></a>

以下程序會逐步解說如何執行使用 `String` 參數的命令。

**使用 Parameter Store 執行字串參數**

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

1. 在導覽窗格中，選擇 **Run Command**。

1. 選擇**執行命令**。

1. 在 **Command document (命令文件)** 清單中選擇 `AWS-RunPowerShellScript` (Windows) 或 `AWS-RunShellScript` (Linux)。

1. 對於 **Command parameters (命令參數)**，輸入 **echo \$1\$1ssm:*parameter-name*\$1\$1**。例如：**echo \$1\$1ssm:/Test/helloWorld\$1\$1**。

1. 在 **Targets** (目標) 區段中，透過手動指定標籤、選取執行個體或邊緣裝置，或指定資源群組，選擇您要執行這項操作的受管節點。
**提示**  
如果您預期看到的受管節點未列出，請參閱 [疑難排解受管節點的可用性](fleet-manager-troubleshooting-managed-nodes.md) 以取得疑難排解秘訣。

1. 對於**其他參數**：
   + 在 **Comment** (註解) 中，輸入此命令的相關資訊。
   + 在**逾時 (秒)** 中，指定在命令執行全面失敗之前，系統要等候的秒數。

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

1. (選用) 針對**輸出選項**，若要將命令輸出儲存至檔案，請選取**將命令輸出寫入至 S3 儲存貯體**方塊。在方塊中輸入儲存貯體和字首 (資料夾) 名稱。
**注意**  
授予能力以將資料寫入至 S3 儲存貯體的 S3 許可，會是指派給執行個體之執行個體設定檔 (適用於 EC2 執行個體) 或 IAM 服務角色 (啟用混合模式的機器) 的許可，而不是執行此任務之 IAM 使用者的許可。如需詳細資訊，請參閱[設定 Systems Manager 所需的執行個體許可](setup-instance-permissions.md)或[建立混合環境的 IAM 服務角色](hybrid-multicloud-service-role.md)。此外，若指定的 S3 儲存貯體位於不同的 AWS 帳戶內，請確保與受管節點相關聯的執行個體設定檔或 IAM 服務角色是否具有寫入該儲存貯體的必要許可。

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

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

1. 選擇**執行**。

1. 在 **Command ID (命令 ID)** 頁面的 **Targets and outputs (目標和輸出)** 區域中，選取執行命令的節點 ID 旁邊的按鈕，然後選擇 **View output (檢視輸出)**。確認命令的輸出是您為參數提供的數值，例如 **This is my first parameter**。

## 使用 執行參數 AWS CLI
<a name="param-test-cli"></a>

**範例 1：簡單命令**  
下列範例命令包含名為 `DNS-IP` 的 Systems Manager 參數。此參數的值就是節點的 IP 地址。此範例使用 an AWS Command Line Interface (AWS CLI) 命令來回應參數值。

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

```
aws ssm send-command \
    --document-name "AWS-RunShellScript" \
    --document-version "1" \
    --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" \
    --parameters "commands='echo {{ssm:DNS-IP}}'" \
    --timeout-seconds 600 \
    --max-concurrency "50" \
    --max-errors "0" \
    --region us-east-2
```

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

```
aws ssm send-command ^
    --document-name "AWS-RunPowerShellScript" ^
    --document-version "1" ^
    --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" ^
    --parameters "commands='echo {{ssm:DNS-IP}}'" ^
    --timeout-seconds 600 ^
    --max-concurrency "50" ^
    --max-errors "0" ^
    --region us-east-2
```

------

該命令會傳回相關資訊，如以下所示。

```
{
    "Command": {
        "CommandId": "c70a4671-8098-42da-b885-89716EXAMPLE",
        "DocumentName": "AWS-RunShellScript",
        "DocumentVersion": "1",
        "Comment": "",
        "ExpiresAfter": "2023-12-26T15:19:17.771000-05:00",
        "Parameters": {
            "commands": [
                "echo {{ssm:DNS-IP}}"
            ]
        },
        "InstanceIds": [],
        "Targets": [
            {
                "Key": "instanceids",
                "Values": [
                    "i-02573cafcfEXAMPLE"
                ]
            }
        ],
        "RequestedDateTime": "2023-12-26T14:09:17.771000-05:00",
        "Status": "Pending",
        "StatusDetails": "Pending",
        "OutputS3Region": "us-east-2",
        "OutputS3BucketName": "",
        "OutputS3KeyPrefix": "",
        "MaxConcurrency": "50",
        "MaxErrors": "0",
        "TargetCount": 0,
        "CompletedCount": 0,
        "ErrorCount": 0,
        "DeliveryTimedOutCount": 0,
        "ServiceRole": "",
        "NotificationConfig": {
            "NotificationArn": "",
            "NotificationEvents": [],
            "NotificationType": ""
        },
        "CloudWatchOutputConfig": {
            "CloudWatchLogGroupName": "",
            "CloudWatchOutputEnabled": false
        },
        "TimeoutSeconds": 600,
        "AlarmConfiguration": {
            "IgnorePollAlarmFailure": false,
            "Alarms": []
        },
        "TriggeredAlarms": []
    }
}
```

命令執行完成後，您可以使用下列命令來檢視其詳細資訊：
+ [https://docs.aws.amazon.com/cli/latest/reference/ssm/get-command-invocation.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/get-command-invocation.html)：檢視有關命令執行的詳細資訊。
+ [https://docs.aws.amazon.com/cli/latest/reference/ssm/link-cli-ref-list-command-invocations.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/link-cli-ref-list-command-invocations.html)：檢視特定受管節點上的命令執行狀態。
+ [https://docs.aws.amazon.com/cli/latest/reference/ssm/link-cli-ref-list-commands.html](https://docs.aws.amazon.com/cli/latest/reference/ssm/link-cli-ref-list-commands.html)：檢視受管節點之間的命令執行狀態。

**範例 2：解密 `SecureString` 參數值**  
下一個範例命令會使用名為 **SecurePassword** 的 `SecureString` 參數。`parameters` 命令會擷取並解密 `SecureString` 參數的值，然後重設本機管理員密碼，而不需要以明文傳送密碼。

------
#### [ Linux ]

```
aws ssm send-command \
        --document-name "AWS-RunShellScript" \
        --document-version "1" \
        --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" \
        --parameters '{"commands":["secure=$(aws ssm get-parameters --names SecurePassword --with-decryption --query Parameters[0].Value --output text --region us-east-2)","echo $secure | passwd myuser --stdin"]}' \
        --timeout-seconds 600 \
        --max-concurrency "50" \
        --max-errors "0" \
        --region us-east-2
```

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

```
aws ssm send-command ^
        --document-name "AWS-RunPowerShellScript" ^
        --document-version "1" ^
        --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" ^
        --parameters "commands=['$secure = (Get-SSMParameterValue -Names SecurePassword -WithDecryption $True).Parameters[0].Value','net user administrator $secure']" ^
        --timeout-seconds 600 ^
        --max-concurrency "50" ^
        --max-errors "0" ^
        --region us-east-2
```

------

**範例 3：參照 SSM 文件中的參數**  
您也可以在 SSM 文件的*參數*部分中參考 Systems Manager 參數，如以下範例所示。

```
{
   "schemaVersion":"2.0",
   "description":"Sample version 2.0 document v2",
   "parameters":{
      "commands" : {
        "type": "StringList",
        "default": ["{{ssm:parameter-name}}"]
      }
    },
    "mainSteps":[
       {
          "action":"aws:runShellScript",
          "name":"runShellScript",
          "inputs":{
             "runCommand": "{{commands}}"
          }
       }
    ]
}
```

不要混淆 SSM 文件的 `runtimeConfig` 區段中使用的*本機參數*與 Parameter Store 參數的相似語法。本機參數與 Systems Manager 參數不同。您可以從缺少 `ssm:` 字首這一點來區分本機參數與 Systems Manager 參數。

```
"runtimeConfig":{
        "aws:runShellScript":{
            "properties":[
                {
                    "id":"0.aws:runShellScript",
                    "runCommand":"{{ commands }}",
                    "workingDirectory":"{{ workingDirectory }}",
                    "timeoutSeconds":"{{ executionTimeout }}"
```

**注意**  
SSM 文件不支援參考 `SecureString` 參數。這表示若要在 Run Command 中 (舉例來說) 使用 `SecureString` 參數，您必須先擷取參數值，再傳遞給 Run Command，如以下範例所示：  

```
value=$(aws ssm get-parameters --names parameter-name --with-decryption)
```

```
aws ssm send-command \
    --name AWS-JoinDomain \
    --parameters password=$value \
    --instance-id instance-id
```

```
aws ssm send-command ^
    --name AWS-JoinDomain ^
    --parameters password=$value ^
    --instance-id instance-id
```

```
$secure = (Get-SSMParameterValue -Names parameter-name -WithDecryption $True).Parameters[0].Value | ConvertTo-SecureString -AsPlainText -Force
```

```
$cred = New-Object System.Management.Automation.PSCredential -argumentlist user-name,$secure
```