全部產品
Search
文件中心

Elastic Compute Service:CreateCommand

更新時間:Jun 25, 2026

新建一條 Shell、PowerShell 或者 Bat 腳本類型的雲端助手命令。

介面說明

介面說明

  • 您可以建立以下類型的命令:
    • Windows 執行個體適用的 Bat 腳本(RunBatScript)。

    • Windows 執行個體適用的 PowerShell 腳本(RunPowerShellScript)。

    • Linux 執行個體適用的 Shell 腳本(RunShellScript)。

  • 您可以透過指定參數 Timeout 為命令設定在 ECS 執行個體中執行時最大的逾時時間,命令執行逾時後,雲端助手 Agent會強制終止命令處理程序,即取消命令的 PID。
    • 對於單次執行,逾時後,該命令針對指定的 ECS 執行個體的執行狀態( InvokeRecordStatus )變為執行失敗(Failed)。

    • 對於定時執行:
      • 定時執行的逾時時間對每一次執行記錄均有效。

      • 某次執行逾時後,該次執行記錄的狀態( InvokeRecordStatus )變為執行失敗(Failed)。

      • 上次執行逾時與否不影響下一次執行。

  • 在一個地域下,您最多可以保有 500~50,000 條雲端助手命令,您也可以申請提升配額,關於如何查詢及提升配額,請參見配額管理

  • 您可以透過指定參數 WorkingDir 為命令指定執行路徑。對於 Linux 執行個體,預設在管理員 root 使用者的 home 目錄下,具體為/root目錄。對於 Windows 執行個體,預設在雲端助手 Agent 處理程序所在目錄,例如:C:\Windows\System32

  • 您可以透過指定參數 EnableParameter=true 啟用自訂參數功能。在設定 CommandContent 時可以透過{{parameter}}的形式表示自訂參數,並在執行命令( InvokeCommand )時,傳入自訂參數鍵值對。例如,您在建立命令時,建立了echo {{name}}命令,在 InvokeCommand 時,透過 Parameters 參數傳入鍵值對<name, Jack>。則自訂參數將自動替換命令,您會得到一條新的命令,並在執行個體中執行echo Jack

調試

您可以在OpenAPI Explorer中直接運行該介面,免去您計算簽名的困擾。運行成功後,OpenAPI Explorer可以自動產生SDK程式碼範例。

調試

授權資訊

下表是API對應的授權資訊,可以在RAM權限原則語句的Action元素中使用,用來給RAM使用者或RAM角色授予調用此API的許可權。具體說明如下:

  • 操作:是指具體的許可權點。

  • 存取層級:是指每個操作的存取層級,取值為寫入(Write)、讀取(Read)或列出(List)。

  • 資源類型:是指操作中支援授權的資源類型。具體說明如下:

    • 對於必選的資源類型,用前面加 * 表示。

    • 對於不支援資源級授權的操作,用全部資源表示。

  • 條件關鍵字:是指雲產品自身定義的條件關鍵字。

  • 關聯操作:是指成功執行操作所需要的其他許可權。操作者必須同時具備關聯操作的許可權,操作才能成功。

操作

存取層級

資源類型

條件關鍵字

關聯操作

ecs:CreateCommand

create

*Command

acs:ecs:{#regionId}:{#accountId}:command/*

請求參數

名稱

類型

必填

描述

樣本值

RegionId

string

地域 ID。您可以呼叫 DescribeRegions 查看最新的阿里雲地域列表。

cn-hangzhou

Name

string

命令名稱,支援全字元集。長度不得超過 128 個字元。

testName

Description

string

命令描述,支援全字元集。長度不得超過 512 個字元。

testDescription

Type

string

命令的類型。取值範圍:

  • RunBatScript:建立一個在 Windows 執行個體中執行的 Bat 腳本。

  • RunPowerShellScript:建立一個在 Windows 執行個體中執行的 PowerShell 腳本。

  • RunShellScript:建立一個在 Linux 執行個體中執行的 Shell 腳本。

RunShellScript

CommandContent

string

命令 Base64 編碼後的內容。

  • 該參數的值必須使用 Base64 編碼後傳輸,且腳本內容的大小在 Base64 編碼之後不能超過 18 KB。

  • 命令內容支援使用自訂參數形式,具體透過指定參數EnableParameter=true啟用自訂參數功能:

    • 自訂參數用{{}}包含的方式定義,在{{}}內參數名前後的空格以及換行符會被忽略。

    • 自訂參數個數不能超過 20 個。

    • 自訂參數名允許 a-zA-Z0-9-_ 的組合,不支援 acs:: 前綴指定非內建環境參數,不支援其餘字元,參數名不區分大小寫。

    • 單個參數名不能超過 64 位元組。

  • 您可以指定內建環境參數作為自訂參數,執行命令時無需手動對參數賦值,雲端助手將為您自動替換為環境中對應的值。支援指定以下內建環境參數:

    • {{ACS::RegionId}}:地域 ID。

    • {{ACS::AccountId}}:阿里雲主帳號 UID。

    • {{ACS::InstanceId}}:執行個體 ID。命令下發到多個執行個體時,如需指定{{ACS::InstanceId}}作為內建環境參數,需確保雲端助手 Agent 不低於以下版本:

      • Linux:2.2.3.309

      • Windows:2.1.3.309

    • {{ACS::InstanceName}}:執行個體名稱。命令下發到多個執行個體時,如需指定{{ACS::InstanceName}}作為內建環境參數,需確保雲端助手 Agent 不低於以下版本:

      • Linux:2.2.3.344

      • Windows:2.1.3.344

    • {{ACS::InvokeId}}:命令執行 ID。如需指定{{ACS::InvokeId}}作為內建環境參數,需確保雲端助手 Agent 不低於以下版本:

      • Linux:2.2.3.309

      • Windows:2.1.3.309

    • {{ACS::CommandId}}:命令 ID。透過呼叫 RunCommand 介面執行命令時,如需指定{{ACS::CommandId}}作為內建環境參數,需確保雲端助手 Agent 不低於以下版本:

      • Linux:2.2.3.309

      • Windows:2.1.3.309。

ZWNobyAxMjM=

WorkingDir

string

您建立的命令在 ECS 執行個體中執行的目錄。長度不得超過 200 個字元。

預設值:

  • Linux 執行個體:管理員 root 使用者的 home 目錄下,具體為/root目錄。

  • Windows 執行個體:雲端助手 Agent 處理程序所在目錄,例如C:\Windows\System32

說明

設定為其他目錄時,請確保執行個體中存在該目錄。

/home/user

Timeout

integer

您建立的命令在 ECS 執行個體中執行時最大的逾時時間,單位為秒。當因為某種原因無法執行您建立的命令時,會出現逾時現象。逾時後,會強制終止命令處理程序,即取消命令的 PID。

預設值:60。

60

EnableParameter

boolean

建立的命令是否使用自訂參數。

預設值:false。

false

ContentEncoding

string

命令內容(CommandContent)的編碼方式。取值範圍:

  • PlainText:不編碼,採用明文傳輸。

  • Base64:Base64 編碼。

預設值:Base64。

說明

錯填該取值會當作 Base64 處理。

PlainText

ClientToken

string

保證請求冪等性。從您的用戶端產生一個參數值,確保不同請求間該參數值唯一。ClientToken 只支援 ASCII 字元,且不能超過 64 個字元。更多資訊,請參見如何保證冪等性

123e4567-e89b-12d3-a456-426655440000

ResourceGroupId

string

命令所屬的資源群組 ID。

rg-123******

Launcher

string

腳本執行的引導程式。長度不能超過 1 KB。

python3 -u {{ACS::ScriptFileName|Ext(".py")}}

Tag

array<object>

標籤列表。

object

標籤列表。

Key

string

命令的標籤鍵。N 的取值範圍為 1~20。一旦傳入該值,則不允許為空字串。

使用一個標籤過濾資源,查詢到該標籤下的資源數量不能超過 1000 個。使用多個標籤過濾資源,查詢到同時繫結了多個標籤的資源數量不能超過 1000 個。如果資源數量超過 1000 個,您需要使用 ListTagResources 介面進行查詢。

最多支援 64 個字元,不能以aliyunacs:開頭,不能包含http://https://

TestKey

Value

string

命令的標籤值。N 的取值範圍為 1~20。該值可以為空字串。

最多支援 128 個字元,不能包含http://https://

TestValue

返回參數

名稱

類型

描述

樣本值

object

CommandId

string

命令 ID。

c-7d2a745b412b4601b2d47f6a768d****

RequestId

string

請求 ID。

473469C7-AA6F-4DC5-B3DB-A3DC0DE3****

樣本

正常返回樣本

JSON格式

{
  "CommandId": "c-7d2a745b412b4601b2d47f6a768d****",
  "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3****"
}

錯誤碼

HTTP status code

錯誤碼

錯誤資訊

描述

400 RegionId.ApiNotSupported The api is not supported in this region.
400 CmdParam.EmptyKey You must specify the parameter names.
400 CmdParam.InvalidParamName Invalid parameter name. The name can contain only lowercase letters (a to z), uppercase letters (A to Z), numbers (0 to 9), hyphens (-), and underscores (_).
400 CmdContent.DecodeError The CommandContent can not be base64 decoded.
400 InvalidParameter.WorkingDir The specified parameter WorkingDir is not valid.
400 InvalidClientToken.Malformed The specified parameter clientToken is not valid.
500 InternalError.Dispatch An error occurred when you dispatched the request.
403 CmdContent.ExceedLimit The length of the command content exceeds the upper limit.
403 CmdName.ExceedLimit The length of the command name exceeds the upper limit.
403 CmdDesc.ExceedLimit The length of the command description exceeds the upper limit.
403 CmdCount.ExceedQuota The total number of commands in the current region exceeds the quota.
403 CmdParamCount.ExceedLimit The maximum number of custom parameters is exceeded.
403 CmdParamName.ExceedLimit The maximum length of a parameter name is exceeded.
403 Operation.Forbidden The operation is not permitted.
403 InvalidStatus.ResourceGroup You cannot perform an operation on a resource group that is being created or deleted.
403 InvalidParameterCharacter.CommandName The command Name contains illegal characters.
403 InvalidParameterCharacter.CommandDescription The command Description contains illegal characters.
403 InvalidParameterCharacter.CommandWorkingDir The command WorkingDir contains illegal characters.
403 InvalidLauncher.LengthLimitExceeded The length of the parameter Launcher exceeds the limit of 1 KB characters.
403 InvalidTimeout.ExceedLimit The specified parameter Timeout exceeds the upper limit.
403 IdempotentParameterMismatch The specified parameter has changed while using an already used clientToken.
403 IdempotentProcessing The previous idempotent request(s) is still processing.
404 InvalidCmdType.NotFound The specified command type does not exist.
404 InvalidRegionId.NotFound The RegionId provided does not exist in our records.
404 InvalidResourceGroup.NotFound The ResourceGroup provided does not exist in our records.

訪問錯誤中心查看更多錯誤碼。

變更歷史

更多資訊,參考變更詳情