搭配 Linux 虛擬機器使用 Azure 自訂指令碼擴充功能第 2 版 \(部分機器翻譯\)

警告

本文參考 CentOS,這是接近結束生命週期 (EOL) 狀態的 Linux 發行版本。 請據以考慮您的使用和規劃。 如需詳細資訊,請參閱 CentOS 生命週期結束指引

自訂指令碼延伸模組 2 版會在 Azure 虛擬機器 (VM) 上下載並執行指令碼。 此延伸模組對部署後設定、軟體安裝或其他任何設定或管理工作很實用。 您可以從 Azure 儲存體或其他可存取的網際網路位置下載指令碼,或是將指令碼提供給擴充功能執行階段。

自訂指令碼功能可以與 Azure Resource Manager 範本整合。 您也可以使用 Azure CLI、Azure PowerShell 或 Azure 虛擬機器 REST API 來執行它。

本文將說明如何從 Azure CLI 使用自訂指令碼延伸模組,以及如何使用 Azure Resource Manager 範本來執行此延伸模組。 本文也提供適用於 Linux 系統的疑難排解步驟。

自訂指令碼延伸模組有兩個版本:

  • 1 版:Microsoft.OSTCExtensions.CustomScriptForLinux
  • 2 版:Microsoft.Azure.Extensions.CustomScript

針對新的和現有的部署使用第 2 版。 新版本為簡易替換 (drop-in replacement)。 移轉與變更名稱和版本一樣簡單。 您不須變更延伸模組設定。

必要條件

支援的 Linux 發行版本

Distribution x64 ARM64
Alma Linux 9.x+ 9.x+
CentOS 7.x+、8.x+ 7.x+
Debian 10+ 11.x+
Flatcar Linux 3374.2.x+ 3374.2.x+
Azure Linux 2.x 2.x
openSUSE 12.3+ 不支援
Oracle Linux 6.4+、7.x+、8.x+ 不支援
Red Hat Enterprise Linux 6.7+、7.x+、8.x+、9.x+ 8.6+、9.x+
Rocky Linux 9.x+ 9.x+
SLES 12.x+、15.x+ 15.x SP4+
Ubuntu 18.04+、20.04+、22.04+ 20.04+、22.04+

指令碼位置

您可以將延伸模組設定為使用 Azure Blob 儲存體認證,以便於存取 Azure Blob 儲存體。 例如,只要 VM 可以路由至端點、GitHub 或內部檔案伺服器,指令碼位置可以位於任一處。

網際網路連線能力

若要從外部下載指令碼,例如 GitHub 或 Azure 儲存體,您需要開啟其他防火牆或網路安全性群組 (NSG) 連接埠。 例如,如果您的指令碼位於 Azure 儲存體,您可以使用適用於儲存體的 Azure NSG 服務標籤允許存取

如果您的指令碼是在本機伺服器上,仍可能需要開啟其他防火牆或 NSG 連接埠。

提示

  • 此擴充功能的最高失敗率導因於指令碼中的語法錯誤。 確認指令碼執行無誤。 將更多記錄放入指令碼,以便於更容易找到失敗。
  • 撰寫具有等冪性的指令碼,因此即使意外執行多次,也不會造成系統變更。
  • 請確定在指令碼執行時,不需要使用者輸入。
  • 指令碼可執行 90 分鐘。 一旦超過此時間,就會導致延伸模組佈建失敗。
  • 請勿將重新開機置於指令碼內。 重新啟動會導致正在安裝的延伸模組發生問題,而且延伸模組也不會在重新開機後繼續。
  • 如果您有指令碼在安裝應用程式和執行指令碼前就導致重新開機,則請使用 cron 作業或如 DSC、Chef 或 Puppet 延伸模組等工具來排程重新啟動。
  • 請勿執行會導致 Azure Linux Agent 停止或更新的指令碼。 它可能會讓延伸模組處於轉換狀態,並導致逾時。
  • 延伸模組只會執行一次指令碼。 如果您想要在每次啟動時都能執行指令碼,您可以使用cloud-init 映像並使用 Scripts Per Boot 模組。 或者您可以使用指令碼建立 systemd 服務單元。
  • 您只能將一個版本的延伸模組套用至 VM。 若要執行第二個自訂指令碼,請使用新的設定更新現有延伸模組。 或者,您可以移除自訂指令碼延伸模組,並重新使用更新的指令碼套用。
  • 如果您要在指令碼執行時排程,請使用延伸模組建立 cron 作業。
  • 當指令碼正在執行時,只會在 Azure 入口網站或 CLI 中看到正在轉換延伸模組狀態。 如果您需要執行中指令碼更頻繁的狀態更新,便必須建立自己的解決方案。
  • 自訂指令碼延伸模組原本不支援 Proxy 伺服器。 不過,您可以在指令碼中使用支援 Proxy 伺服器的檔案傳輸工具,例如 Curl
  • 請留意指令碼或命令可能依賴的非預設目錄位置。 有邏輯可以處理此情況。

擴充功能結構描述

「自訂指令碼擴充功能」組態會指定指令碼位置和要執行命令等項目。 您可將此資訊儲存在設定檔中、在命令列中指定設定,或在 Azure Resource Manager 範本中指定設定。

您可將敏感性資料儲存在受保護的設定中,此設定會經過加密,並且只會在目標 VM 上解密。 當執行命令包含機密資料 (例如密碼) 時,受保護的組態會相當有用。 以下是範例:

{
  "name": "config-app",
  "type": "Extensions",
  "location": "[resourceGroup().location]",
  "apiVersion": "2019-03-01",
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', concat(variables('vmName'),copyindex()))]"
  ],
  "tags": {
    "displayName": "config-app"
  },
  "properties": {
    "publisher": "Microsoft.Azure.Extensions",
    "type": "CustomScript",
    "typeHandlerVersion": "2.1",
    "autoUpgradeMinorVersion": true,
    "settings": {
      "skipDos2Unix":false,
      "timestamp":123456789
    },
    "protectedSettings": {
       "commandToExecute": "<command-to-execute>",
       "script": "<base64-script-to-execute>",
       "storageAccountName": "<storage-account-name>",
       "storageAccountKey": "<storage-account-key>",
       "fileUris": ["https://.."],
       "managedIdentity" : "<managed-identity-identifier>"
    }
  }
}

注意

managedIdentity 屬性「不得」storageAccountNamestorageAccountKey 屬性搭配使用。

屬性值

名稱 值或範例 資料類型
apiVersion 2019-03-01 date
publisher Microsoft.Azure.Extensions string
type CustomScript string
typeHandlerVersion 2.1 int
fileUris https://github.com/MyProject/Archive/MyPythonScript.py 陣列
commandToExecute python MyPythonScript.py \<my-param1> string
script IyEvYmluL3NoCmVjaG8gIlVwZGF0aW5nIHBhY2thZ2VzIC4uLiIKYXB0IHVwZGF0ZQphcHQgdXBncmFkZSAteQo= string
skipDos2Unix false boolean
timestamp 123456789 32 位元整數
storageAccountName examplestorageacct string
storageAccountKey TmJK/1N3AbAZ3q/+hOXoi/l73zOqsaxXDhqa9Y83/v5UpXQp2DQIBuv2Tifp60cE/OaHsJZmQZ7teQfczQj8hg== string
managedIdentity { }{ "clientId": "31b403aa-c364-4240-a7ff-d85fb6cd7232" }{ "objectId": "12dd289c-0583-46e5-b9b4-115d5c19ef4b" } JSON 物件

屬性值詳細資料

屬性 選擇性或必要 詳細資料
apiVersion 不適用 您可以使用資源總管 或使用 Azure CLI 中的命令 az provider list -o json,尋找最新 API 版本。
fileUris 選擇性 要下載的 URL 或檔案。
commandToExecute 如果未設定 script,則為必要 要執行的進入點指令碼。 如果您的命令包含祕密 (例如密碼),請改用此屬性,而非 script
script 如果未設定 commandToExecute,則為必要 /bin/sh 執行的 Base64 編碼並選擇性使用 gzip 指令碼。
skipDos2Unix 選擇性 如果您想要略過指令碼型檔案 URL 或指令碼的 dos2unix 轉換,請將此值設定為 false
timestamp 選擇性 只在觸發重新執行指令碼的情況下,變更此值。 只要與先前的值不同,任何整數值都可接受。
storageAccountName 選擇性 儲存體帳戶的名稱。 如果您指定儲存體認證,則所有 fileUris 值都必須是 Azure Blob 的 URL。
storageAccountKey 選擇性 儲存體帳戶的存取金鑰。
managedIdentity 選擇性 用於下載檔案的受控識別。 值為 clientId (選用、字串),這是受控識別的用戶端識別碼,而 objectId (選用、字串),這是受控識別的物件識別碼。

公開設定會以純文字格式,傳送到執行指令碼所在的 VM。 受保護的設定會透過只有 Azure 和 VM 知道的金鑰加密。 設定會以傳送時的形式,儲存至 VM。 也就是說,如果設定經加密,會以加密形式儲存在 VM 上。 用來解密加密值的憑證會儲存在 VM 上。 此憑證也會用來在執行階段解密設定 (如有需要)。

使用公開設定可能有助於偵錯,但強烈建議您使用受保護的設定。

您可以在公用或受保護的設定中設定下列值。 延伸模組會拒絕任何在公用和受保護的設定中設定下列值的設定。

  • commandToExecute
  • script
  • fileUris

屬性:skipDos2Unix

舊版自訂指令碼延伸模組 Microsoft.OSTCExtensions.CustomScriptForLinux,會藉由將 \r\n 轉譯為 \n,自動將 DOS 檔案轉換為 UNIX 檔案。 這項轉譯仍存在,並預設為開啟。 這項轉換會根據下列任何準則,套用至從 fileUris 下載的所有檔案或指令碼設定:

  • 副檔名為 .sh.txt.py.pl。 指令碼設定一律會符合此準則,因為假設為使用 /bin/sh 執行的指令碼。指令碼設定會在 VM 上儲存為 script.sh
  • 檔案的開頭為 #!

預設值為 false,表示「會」執行 dos2unix 轉換。 您可以將 skipDos2Unix 設定為 true,略過 dos2unix 轉換:

{
  "fileUris": ["<url>"],
  "commandToExecute": "<command-to-execute>",
  "skipDos2Unix": true
}

屬性:指令碼

自訂指令碼延伸模組支援執行使用者定義的指令碼。 指令碼設定將 commandToExecutefileUris 合併為單一設定。 您不須設定要從 Azure 儲存體或 GitHub Gist 下載的檔案,即可將指令碼編碼為設定。 您可以使用指令碼取代 commandToExecutefileUris

以下為部分需求:

  • 指令碼必須採用 Base64 編碼。
  • 指令碼可以選擇性地使用 gzip。
  • 您可以在公用或受保護的設定中使用指令碼設定。
  • 指令碼參數的資料大小上限為 256 KB。 如果指令碼超過此大小,則不會執行。

例如,下列指令碼儲存至檔案 /script.sh/

#!/bin/sh
echo "Creating directories ..."
mkdir /data
chown user:user /data
mkdir /appdata
chown user:user /appdata

您可以取得以下命令的輸出,建構正確的自訂指令碼延伸模組指令碼設定:

cat script.sh | base64 -w0
{
  "script": "IyEvYmluL3NoCmVjaG8gIlVwZGF0aW5nIHBhY2thZ2VzIC4uLiIKYXB0IHVwZGF0ZQphcHQgdXBncmFkZSAteQo="
}

大部分情況下,指令碼可選擇性使用 gzip 進一步縮減大小。 自訂指令碼延伸模組會自動偵測使用 gzip 壓縮。

cat script | gzip -9 | base64 -w 0

自訂指令碼延伸模組會使用以下演算法來執行指令碼:

  1. 宣告指令碼值的長度不超過 256 KB。
  2. Base64 將指令碼的值解碼。
  3. 「嘗試」對 Base64 解碼的值使用 gunzip。
  4. 將解碼 (並選擇性解壓縮) 的值寫入至磁碟:/var/lib/waagent/custom-script/#/script.sh
  5. 使用 _/bin/sh -c /var/lib/waagent/custom-script/#/script.sh 執行指令碼。

屬性:managedIdentity

注意

必須在受保護的設定中,才能指定此屬性。

自訂指令碼延伸模組 (2.1 版和更新版本) 支援受控識別,從 fileUris 設定中提供的 URL 下載檔案。 此方法可讓自訂指令碼延伸模組存取 Azure 儲存體私人 Blob 或容器,而不需要使用者傳遞共用存取簽章 (SAS) 權杖或儲存體帳戶金鑰之類的秘密。

若要使用這項功能,可將系統指派使用者指派的身分識別,新增至預期執行自訂指令碼延伸模組的 VM 或虛擬機器擴展集。 然後將受控識別存取權授與 Azure 儲存體容器或 Blob

若要在目標 VM 或虛擬機器擴展集上,使用系統指派的身分識別,請將 managedidentity 設定為空白 JSON 物件。

{
  "fileUris": ["https://mystorage.blob.core.windows.net/privatecontainer/script1.sh"],
  "commandToExecute": "sh script1.sh",
  "managedIdentity" : {}
}

若要在目標 VM 或虛擬機器擴展集上,使用使用者指派的身分識別,請使用受控識別的用戶端識別碼或物件識別碼設定 managedidentity

{
  "fileUris": ["https://mystorage.blob.core.windows.net/privatecontainer/script1.sh"],
  "commandToExecute": "sh script1.sh",
  "managedIdentity" : { "clientId": "31b403aa-c364-4240-a7ff-d85fb6cd7232" }
}
{
  "fileUris": ["https://mystorage.blob.core.windows.net/privatecontainer/script1.sh"],
  "commandToExecute": "sh script1.sh",
  "managedIdentity" : { "objectId": "12dd289c-0583-46e5-b9b4-115d5c19ef4b" }
}

注意

managedIdentity 屬性「不得」storageAccountNamestorageAccountKey 屬性搭配使用。

範本部署

您可以使用 Azure Resource Manager 範本部署 Azure VM 延伸模組。 上一節詳述的 JSON 結構描述可以用於 Azure Resource Manager 範本,在部署範本期間執行自訂指令碼延伸模組。 您可以在 GitHub 找到包含自訂指令碼延伸模組的範例範本。

{
  "name": "config-app",
  "type": "extensions",
  "location": "[resourceGroup().location]",
  "apiVersion": "2019-03-01",
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', concat(variables('vmName'),copyindex()))]"
  ],
  "tags": {
    "displayName": "config-app"
  },
  "properties": {
    "publisher": "Microsoft.Azure.Extensions",
    "type": "CustomScript",
    "typeHandlerVersion": "2.1",
    "autoUpgradeMinorVersion": true,
    "settings": {
      },
    "protectedSettings": {
      "commandToExecute": "sh hello.sh <param2>",
      "fileUris": ["https://github.com/MyProject/Archive/hello.sh"
      ]
    }
  }
}

注意

這些屬性名稱會區分大小寫。 為了避免發生部署問題,請使用如下所示的名稱。

Azure CLI

您要使用 Azure CLI 來執行自訂指令碼延伸模組時,請建立一個或多個設定檔。 設定檔至少必須包含 commandToExecuteaz vm extension set 命令會參考設定檔:

az vm extension set \
  --resource-group myResourceGroup \
  --vm-name myVM --name customScript \
  --publisher Microsoft.Azure.Extensions \
  --protected-settings ./script-config.json

或者,您也可以在命令中以 JSON 格式化字串的形式指定設定。 此方法可讓您在執行期間指定設定,而不需使用個別的設定檔。

az vm extension set \
  --resource-group exttest \
  --vm-name exttest \
  --name customScript \
  --publisher Microsoft.Azure.Extensions \
  --protected-settings '{"fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"],"commandToExecute": "./config-music.sh"}'

範例:含指令檔的公用設定

此範例會使用名為 script-config.json 的下列指令碼檔案:

{
  "fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"],
  "commandToExecute": "./config-music.sh"
}
  1. 使用您選擇的文字編輯器或使用下列 CLI 命令來建立指令碼檔案:

    cat <<EOF > script-config.json
    {
      "fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"],
      "commandToExecute": "./config-music.sh"
    }
    EOF
    
  2. 執行以下命令:

    az vm extension set \
      --resource-group myResourceGroup \
      --vm-name myVM --name customScript \
      --publisher Microsoft.Azure.Extensions \
      --settings ./script-config.json
    

範例:不含指令檔的公用設定

此範例使用下列 JSON 格式的內容:

{
  "commandToExecute": "apt-get -y update && apt-get install -y apache2"
}

執行以下命令:

az vm extension set \
  --resource-group tim0329vmRG \
  --vm-name tim0329vm --name customScript \
  --publisher Microsoft.Azure.Extensions \
  --settings '{"commandToExecute": "apt-get -y update && apt-get install -y apache2"}'

範例:公用且受保護的設定檔

使用公用設定檔來指定指令檔的 URI:

{
  "fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"]
}

使用受保護的設定檔來指定要執行的命令:

{
  "commandToExecute": "./config-music.sh"
}
  1. 使用您選擇的文字編輯器或使用下列 CLI 命令來建立公用設定檔:

    cat <<EOF > script-config.json
    {
      "fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"]
    }
    EOF
    
  2. 使用您選擇的文字編輯器或使用下列 CLI 命令來建立受保護的設定檔:

    cat <<EOF > protected-config.json
    {
      "commandToExecute": "./config-music.sh"
    }
    EOF
    
  3. 執行以下命令:

    az vm extension set \
      --resource-group myResourceGroup \
      --vm-name myVM \
      --name customScript \
      --publisher Microsoft.Azure.Extensions \
      --settings ./script-config.json \
      --protected-settings ./protected-config.json
    

虛擬機器擴展集

如果您從 Azure 入口網站部署自訂指令碼延伸模組,就無法對於在儲存體帳戶中存取指令碼的 SAS 權杖控制該權杖的到期日。 初始部署可運作,但儲存體帳戶的 SAS 權杖到期時,任何後續擴充作業都失敗,因為自訂指令碼延伸模組無法再存取儲存體帳戶。

在虛擬機器擴展集上部署自訂指令碼延伸模組時,建議您使用 PowerShellAzure CLIAzure Resource Manager 範本。 如此一來,您可以選擇使用受控識別,或直接控制 SAS 權杖的到期日,以便只要您需要,即可存取儲存體帳戶中的指令碼。

疑難排解

當「自訂指令碼擴充功能」執行時,會建立指令碼,或將指令碼下載到類似下列範例的目錄。 命令輸出也會儲存到這個目錄的 stdoutstderr 檔案中。

sudo ls -l /var/lib/waagent/custom-script/download/0/

若要進行疑難排解,請先檢查 Linux 代理程式記錄,確認延伸模組已執行:

sudo cat /var/log/waagent.log

尋找延伸模組執行。 它看起來會像:

2018/04/26 17:47:22.110231 INFO [Microsoft.Azure.Extensions.customScript-2.0.6] [Enable] current handler state is: notinstalled
2018/04/26 17:47:22.306407 INFO Event: name=Microsoft.Azure.Extensions.customScript, op=Download, message=Download succeeded, duration=167
2018/04/26 17:47:22.339958 INFO [Microsoft.Azure.Extensions.customScript-2.0.6] Initialize extension directory
2018/04/26 17:47:22.368293 INFO [Microsoft.Azure.Extensions.customScript-2.0.6] Update settings file: 0.settings
2018/04/26 17:47:22.394482 INFO [Microsoft.Azure.Extensions.customScript-2.0.6] Install extension [bin/custom-script-shim install]
2018/04/26 17:47:23.432774 INFO Event: name=Microsoft.Azure.Extensions.customScript, op=Install, message=Launch command succeeded: bin/custom-script-shim install, duration=1007
2018/04/26 17:47:23.476151 INFO [Microsoft.Azure.Extensions.customScript-2.0.6] Enable extension [bin/custom-script-shim enable]
2018/04/26 17:47:24.516444 INFO Event: name=Microsoft.Azure.Extensions.customScript, op=Enable, message=Launch command succeeded: bin/custom-sc

在上述輸出中:

  • Enable 為命令開始執行的時間。
  • Download 與從 Azure 下載自訂指令碼延伸模組套件有關,與 fileUris 中指定的指令檔無關。

「Azure 指令碼擴充功能」會產生一個記錄檔,位置如下:

sudo cat /var/log/azure/custom-script/handler.log

尋找個別執行。 它看起來會像:

time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event=start
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event=pre-check
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="comparing seqnum" path=mrseq
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="seqnum saved" path=mrseq
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="reading configuration"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="read configuration"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="validating json schema"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="json schema valid"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="parsing configuration json"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="parsed configuration json"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="validating configuration logically"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="validated configuration"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="creating output directory" path=/var/lib/waagent/custom-script/download/0
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="created output directory"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 files=1
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 file=0 event="download start"
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 file=0 event="download complete" output=/var/lib/waagent/custom-script/download/0
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="executing command" output=/var/lib/waagent/custom-script/download/0
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="executing protected commandToExecute" output=/var/lib/waagent/custom-script/download/0
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event="executed command" output=/var/lib/waagent/custom-script/download/0
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event=enabled
time=2018-04-26T17:47:23Z version=v2.0.6/git@1008306-clean operation=enable seq=0 event=end

您可以在此發現:

  • 啟動此記錄的 enable 命令。
  • 傳遞至延伸模組的設定。
  • 下載檔案及其動作結果的延伸模組。
  • 正在執行的命令和結果。

您也可以使用 Azure CLI 來擷取自訂指令碼延伸模組的執行狀態,包括以 commandToExecute 傳遞的實際引數:

az vm extension list -g myResourceGroup --vm-name myVM

輸出看起來會如同下列文字:

[
  {
    "autoUpgradeMinorVersion": true,
    "forceUpdateTag": null,
    "id": "/subscriptions/subscriptionid/resourceGroups/rgname/providers/Microsoft.Compute/virtualMachines/vmname/extensions/customscript",
    "resourceGroup": "rgname",
    "settings": {
      "commandToExecute": "sh script.sh > ",
      "fileUris": [
        "https://catalogartifact.azureedge.net/publicartifacts/scripts/script.sh",
        "https://catalogartifact.azureedge.net/publicartifacts/scripts/script.sh"
      ]
    },
    "tags": null,
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "typeHandlerVersion": "2.0",
    "virtualMachineExtensionType": "CustomScript"
  },
  {
    "autoUpgradeMinorVersion": true,
    "forceUpdateTag": null,
    "id": "/subscriptions/subscriptionid/resourceGroups/rgname/providers/Microsoft.Compute/virtualMachines/vmname/extensions/OmsAgentForLinux",
    "instanceView": null,
    "location": "eastus",
    "name": "OmsAgentForLinux",
    "protectedSettings": null,
    "provisioningState": "Succeeded",
    "publisher": "Microsoft.EnterpriseCloud.Monitoring",
    "resourceGroup": "rgname",
    "settings": {
      "workspaceId": "workspaceid"
    },
    "tags": null,
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "typeHandlerVersion": "1.0",
    "virtualMachineExtensionType": "OmsAgentForLinux"
  }
]

Azure CLI 語法問題

Azure CLI 可以在數個殼層環境中執行,但格式稍有不同。 如果使用 Azure CLI 命令時出現非預期的結果,請參閱如何順利使用 Azure CLI

下一步

若要查看程式碼、目前問題和版本,請參閱 custom-script-extension-linux