

• Amazon Systems Manager CloudWatch 控制面板在 2026 年 4 月 30 日之后将不再可用。客户可以像现在一样继续使用 Amazon CloudWatch 控制台来查看、创建和管理其 Amazon CloudWatch 控制面板。有关更多信息，请参阅 [Amazon CloudWatch 控制面板文档](https://docs.amazonaws.cn/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

# `aws:runCommand` - 在托管实例上运行命令


运行指定的命令。

**注意**  
自动化仅支持 Amazon Systems Manager Run Command 操作的一个*output*。一个运行手册可以包括多个 Run Command 操作，但一次仅一个操作支持 output。

**Input**  
此操作支持大多数 send command 参数。有关更多信息，请参阅 [SendCommand](https://docs.amazonaws.cn/systems-manager/latest/APIReference/API_SendCommand.html)。

------
#### [ YAML ]

```
- name: checkMembership
  action: 'aws:runCommand'
  inputs:
    DocumentName: AWS-RunPowerShellScript
    InstanceIds:
      - '{{InstanceIds}}'
    Parameters:
      commands:
        - (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain
```

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

```
{
    "name": "checkMembership",
    "action": "aws:runCommand",
    "inputs": {
        "DocumentName": "AWS-RunPowerShellScript",
        "InstanceIds": [
            "{{InstanceIds}}"
        ],
        "Parameters": {
            "commands": [
                "(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain"
            ]
        }
    }
}
```

------

DocumentName  
如果命令类型文档属于您或 Amazon，请指定文档的名称。如果您使用的是另一个 Amazon Web Services 账户与您共享的文档，指定文档的 Amazon Resource Name (ARN)。有关如何使用共享文档的更多信息，请参阅 [使用共享 SSM 文档](documents-ssm-sharing.md#using-shared-documents)。  
类型：字符串  
是否必需：是

InstanceIds  
要在其中运行命令的实例 ID。您可以指定最多 50 个 ID。  
您还可以使用虚拟参数 `{{RESOURCE_ID}}` 代替实例 ID，以便在目标组中的所有实例上运行命令。有关伪参数的更多信息，请参阅[注册维护时段任务时使用伪参数](maintenance-window-tasks-pseudo-parameters.md)。  
另一种替代方法是使用 `Targets` 参数向一组实例发送命令。`Targets` 参数接受 Amazon Elastic Compute Cloud (Amazon EC2) 标签。有关如何使用 `Targets` 参数的更多信息，请参阅 [大规模运行命令](send-commands-multiple.md)。  
类型：StringList  
必需：否（如果未指定实例 ID 或使用 `{{RESOURCE_ID}}` 虚拟参数，则必须指定 `Targets` 参数。）

Targets  
一组搜索条件，使用您指定的键/值组合来设置实例目标。如果未在调用中提供一个或多个实例 ID，则 `Targets` 为必需。有关如何使用 `Targets` 参数的更多信息，请参阅 [大规模运行命令](send-commands-multiple.md)。  
类型：MapList（列表中 map 的架构必须与对象匹配。） 有关更多信息，请参阅 *Amazon Systems ManagerAPI 参考*中的[目标](https://docs.amazonaws.cn/systems-manager/latest/APIReference/API_Target.html)。  
必需：否（如果未指定 `Targets`，则必须指定实例 ID 或使用 `{{RESOURCE_ID}}` 虚拟参数。）  
以下为示例。  

```
- name: checkMembership
  action: aws:runCommand
  inputs:
    DocumentName: AWS-RunPowerShellScript
    Targets:
      - Key: tag:Stage
        Values:
          - Gamma
          - Beta
      - Key: tag-key
        Values:
          - Suite
    Parameters:
      commands:
        - (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain
```

```
{
    "name": "checkMembership",
    "action": "aws:runCommand",
    "inputs": {
        "DocumentName": "AWS-RunPowerShellScript",
        "Targets": [                   
            {
                "Key": "tag:Stage",
                "Values": [
                    "Gamma", "Beta"
                ]
            },
            {
                "Key": "tag:Application",
                "Values": [
                    "Suite"
                ]
            }
        ],
        "Parameters": {
            "commands": [
                "(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain"
            ]
        }
    }
}
```

Parameters  
文档中指定的必需参数和可选参数。  
类型：映射  
必需：否

CloudWatchOutputConfig  
用于将命令输出发送到 Amazon CloudWatch Logs 的配置选项。有关将命令输出发送到 CloudWatch Logs 的更多信息，请参阅 [为 Run Command 配置 Amazon CloudWatch Logs](sysman-rc-setting-up-cwlogs.md)。  
类型：StringMap（映射的架构必须与对象匹配。有关更多信息，请参阅 *Amazon Systems ManagerAPI 参考* 中的 [CloudWatchOutputConfig](https://docs.amazonaws.cn/systems-manager/latest/APIReference/API_CloudWatchOutputConfig.html)）。  
必需：否  
以下为示例。  

```
- name: checkMembership
  action: aws:runCommand
  inputs:
    DocumentName: AWS-RunPowerShellScript
    InstanceIds:
      - "{{InstanceIds}}"
    Parameters:
      commands:
        - "(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain"
    CloudWatchOutputConfig:
      CloudWatchLogGroupName: CloudWatchGroupForSSMAutomationService
      CloudWatchOutputEnabled: true
```

```
{
    "name": "checkMembership",
    "action": "aws:runCommand",
    "inputs": {
        "DocumentName": "AWS-RunPowerShellScript",
        "InstanceIds": [
            "{{InstanceIds}}"
        ],
        "Parameters": {
            "commands": [
                "(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain"
            ]
        },
        "CloudWatchOutputConfig" : { 
                "CloudWatchLogGroupName": "CloudWatchGroupForSSMAutomationService",
                "CloudWatchOutputEnabled": true
        }
    }
}
```

Comment  
有关此命令的用户定义的信息。  
类型：字符串  
必需：否

DocumentHash  
文档的哈希。  
类型：字符串  
必需：否

DocumentHashType  
哈希的类型。  
类型：字符串  
有效值：`Sha256` \$1 `Sha1`  
必需：否

NotificationConfig  
用于发送通知的配置。  
必需：否

OutputS3BucketName  
命令输出响应的 S3 存储桶的名称。托管式节点必须具有 S3 存储桶的权限才能成功记录输出。  
类型：字符串  
必需：否

OutputS3KeyPrefix  
 前缀。  
类型：字符串  
必需：否

ServiceRoleArn  
Amazon Identity and Access Management (IAM) 角色的 ARN。  
类型：字符串  
必需：否

TimeoutSeconds  
等待命令传递到实例上的 Amazon Systems Manager SSM Agent 的时间（单位：秒）。如果在达到指定的值之前，命令未被实例上的 SSM Agent 接收，则命令的状态将更改为 `Delivery Timed Out`。  
类型：整数  
必需：否  
有效值：30-2592000Output

CommandId  
命令的 ID。

Status  
命令的状态。

ResponseCode  
命令的响应代码。如果运行的文档有多个步骤，则不会为此输出返回值。

Output  
命令的输出。如果您使用命令定位一个标记或多个实例，则不会返回 output 值。您可以使用 `GetCommandInvocation` 和 `ListCommandInvocations` API 操作来检索单个实例的输出。