共用方式為


Azure CLI 命令的輸出格式

Azure CLI 會使用 JSON 作為其預設輸出格式,但提供其他格式。 --output 使用 (--out-o) 參數來格式化 Azure CLI 輸出。 輸出的自變數值和類型如下:

--輸出 說明
json JSON 字串。 此設定為預設值
jsonc 彩色 JSON
table 以鍵作為欄標題的 ASCII 資料表
tsv 制表符分隔值,不含鍵
yaml YAML,這是 JSON 的人類可讀取替代方案
yamlc 彩色 YAML
none 除了錯誤和警告之外,沒有其他輸出

警告

若要避免公開 API 金鑰和認證等秘密,請使用 的輸出格式 none ,或將命令輸出儲存在變數中。 注意: 某些 CI/CD 環境可能會將已執行命令的輸出儲存在記錄中。 最好確認這些記錄檔的內容,以及可存取記錄的人員。 如需詳細資訊,請參閱 無輸出格式

JSON 輸出格式 (預設值)

下列範例會以預設 JSON 格式顯示訂用帳戶中的虛擬機清單。

az vm list --output json

以下的輸出為求簡潔省略部分欄位,並將識別資訊替換掉。

[
  {
    "availabilitySet": null,
    "diagnosticsProfile": null,
    "hardwareProfile": {
      "vmSize": "Standard_DS1"
    },
    "id": "/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010",
    "instanceView": null,
    "licenseType": null,
    "location": "westus",
    "name": "DemoVM010",
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "/subscriptions/.../resourceGroups/demorg1/providers/Microsoft.Network/networkInterfaces/DemoVM010VMNic",
          "primary": null,
          "resourceGroup": "demorg1"
        }
      ]
    },
          ...
          ...
          ...
]

YAML 輸出格式

格式會將 yaml 輸出列印為 YAML,這是一種純文字資料序列化格式。 YAML 通常比 JSON 更容易讀取,並對應至該格式。 某些應用程式和 Azure CLI 命令會採用 YAML 作為組態輸入,而不是 JSON。

az vm list --output yaml

以下的輸出為求簡潔省略部分欄位,並將識別資訊替換掉。

- availabilitySet: null
  diagnosticsProfile: null
  hardwareProfile:
    vmSize: Standard_DS1_v2
  id: /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010
  identity: null
  instanceView: null
  licenseType: null
  location: westus
  name: ExampleVM1
  networkProfile:
    networkInterfaces:
    - id: /subscriptions/.../resourceGroups/DemoRG1/providers/Microsoft.Network/networkInterfaces/DemoVM010Nic
      primary: null
      resourceGroup: DemoRG1
  ...
...

數據表輸出格式

格式 table 會將輸出列印為 ASCII 資料表,讓您輕鬆閱讀和掃描。 巢狀物件不會包含在數據表輸出中,但仍可篩選為查詢的一部分。 某些欄位未包含在數據表中,因此當您想要快速且可搜尋的數據概觀時,此格式是最佳格式。

az vm list --output table
Name         ResourceGroup    Location
-----------  ---------------  ----------
DemoVM010    DEMORG1          westus
demovm212    DEMORG1          westus
demovm213    DEMORG1          westus
KBDemo001VM  RGDEMO001        westus
KBDemo020    RGDEMO001        westus

您可以使用 --query 參數來自定義要在清單輸出中顯示的屬性和資料行。 下列範例示範如何只選取命令中的 list VM 名稱和資源組名。

az vm list --query "[].{resource:resourceGroup, name:name}" --output table
Resource    Name
----------  -----------
DEMORG1     DemoVM010
DEMORG1     demovm212
DEMORG1     demovm213
RGDEMO001   KBDemo001VM
RGDEMO001   KBDemo020

備註

某些索引鍵預設不會列印在數據表檢視中。 這些索引鍵包括 idtypeetag。 如果您需要輸出中的金鑰,您可以使用 JMESPath 重設金鑰功能來變更金鑰名稱,並避免篩選。

az vm list --query "[].{objectID:id}" --output table

如需使用查詢來篩選數據的詳細資訊,請參閱 搭配 Azure CLI 使用 JMESPath 查詢。

TSV 輸出格式

輸出格式會 tsv 傳回索引標籤和換行符分隔值,而不需要額外的格式、索引鍵或其他符號。 此格式可讓您輕鬆地將輸出取用至其他需要以某種形式處理文字的命令和工具。 table如同格式,tsv不會列印巢狀物件。

使用上述範例搭配 tsv 選項會輸出索引標籤分隔的結果。

az vm list --output tsv
None    None        /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010    None    None    westus    DemoVM010            None    Succeeded    DEMORG1    None            Microsoft.Compute/virtualMachines    cbd56d9b-9340-44bc-a722-25f15b578444
None    None        /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm212    None    None    westus    demovm212            None    Succeeded    DEMORG1    None            Microsoft.Compute/virtualMachines    4bdac85d-c2f7-410f-9907-ca7921d930b4
None    None        /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm213    None    None    westus    demovm213            None    Succeeded    DEMORG1    None            Microsoft.Compute/virtualMachines    2131c664-221a-4b7f-9653-f6d542fbfa34
None    None        /subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo001VM    None    None    westus    KBDemo001VM            None    Succeeded    RGDEMO001    None            Microsoft.Compute/virtualMachines    14e74761-c17e-4530-a7be-9e4ff06ea74b
None    None        /subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo020   None    None    westus    KBDemo020            None    Succeeded    RGDEMO001    None            Microsoft.Compute/virtualMachines    36baa9-9b80-48a8-b4a9-854c7a858ece

tsv 輸出格式的一個限制是不能保證輸出的順序。 Azure CLI 會盡力依字母順序排列回應 JSON 中的索引鍵,以保留原始的排序,然後按照順序列印其值以提供 tsv 輸出。 不保證訂單一律相同,因為 Azure 服務回應格式可能會變更。

若要強制執行一致的排序,您必須使用 --query 參數和 多重選取清單 格式。 當 Azure CLI 命令傳回單一 JSON 字典時,請使用一般格式 [key1, key2, ..., keyN] 來強制執行索引鍵順序。 針對傳回陣列的 Azure CLI 命令,請使用一般格式 [].[key1, key2, ..., keyN] 來排序數據行值。

例如,若要依標識碼、位置、資源群組和 VM 名稱排序此資訊:

az vm list --output tsv --query '[].[id, location, resourceGroup, name]'
/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010    westus    DEMORG1    DemoVM010
/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm212    westus    DEMORG1    demovm212
/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm213    westus    DEMORG1    demovm213
/subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo001VM     westus  RGDEMO001       KBDemo001VM
/subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo020       westus  RGDEMO001       KBDemo020

下一個範例示範如何使用管線將 tsv 輸出導向到 Bash 中的其他命令。 此查詢可用來篩選輸出並強制排序、 grep 選取具有文字 RGD 的項目,然後 cut 命令會選取第四個字段來顯示輸出中 VM 的名稱。

az vm list --output tsv --query '[].[id, location, resourceGroup, name]' | grep RGD | cut -f4
KBDemo001VM
KBDemo020

tsv 值指派給變數時,通常會使用輸出格式。 本範例會取得使用中的訂用帳戶標識碼,並將它儲存在變數中以供腳本使用。

# Bash Script
subscriptionID=$(az account show --query id --output tsv)
echo "Using subscription ID $subscriptionID"

如需更多 --query 參數範例,請參閱 如何查詢 Azure CLI 命令輸出

無輸出格式

有些 Azure CLI 命令會輸出必須保護的資訊。 以下是四個範例:

  • 密碼
  • 連線字串
  • 秘密
  • 鑰匙

若要在使用 Azure CLI 命令時保護秘密和金鑰,請選擇下列其中一個選項:

選項 優點 用例
--output none 輸出格式 讓敏感性信息無法顯示在控制台中。 如果您的命令失敗,您仍會收到錯誤訊息。 1. 當可以稍後擷取命令輸出時使用。
2.當您不需要輸出時,請使用 。
3.當受控識別或服務主體用來管理 Azure 資源時,常見的選擇。
--query 參數 將輸出儲存在變數中。 1. 當無法在稍後擷取命令輸出時使用。
2.當您需要在文稿中使用命令輸出值時,請使用 。

稍後擷取安全性資訊

可以稍後取得某些 Azure 的秘密。 例如,儲存在 Azure Key Vault 中的秘密。 在此範例中,使用 az keyvault secret set 搭配 --output none 選項建立 Azure 金鑰保存庫 秘密。 您可以稍後使用az keyvault secret show命令來擷取秘密。

az keyvault secret set --name MySecretName \
                       --vault-name MyKeyVaultName \
                       --value MySecretValue\
                       --output none

將安全性資訊儲存至變數

使用 --query 將輸出儲存在變數的技術上不是輸出格式。 這是保護秘密的解決方案,也是使用 --output none的替代方案。 例如,當您重設服務主體認證時,無法再次擷取密碼。

將服務主體的認證重設後,以預設的 json 格式輸出結果:

# reset service principal credentials using default output format (json).
az ad sp credential reset --id myServicePrincipalID --output json

主控台輸出,其中顯示控制台中的新密碼。

{
  "appId": "myServicePrincipalID",
  "password": "myServicePrincipalNewPassword",
  "tenant": "myTenantID"
}

更好的解決方案是將敏感性資訊傳回至變數。

# Bash Script
# reset service principal credentials returning results to a variable
myNewPassword=$(az ad sp credential reset --id myServicePrincipalID --query password --output tsv)

# Display the new password (remove this line in production for security)
echo "New password: $myNewPassword"

如需將輸出儲存至變數的詳細資訊,請參閱 成功使用 Azure CLI - 將值傳遞至另一個命令。 若要深入了解 --query 參數語法,請參閱 如何查詢 Azure CLI 命令輸出

設定預設輸出格式

Azure CLI 命令提供可透過兩種方式控制的輸出:

輸出控制件 優點 操作說明
全域設定 選取使用最多的預設輸出值,因此您不需要持續為每個參考命令提供 --output 參數。 使用 az config set 指定預設輸出格式。
命令參數 在命令層級指定輸出,併為您的腳本提供最大的彈性。 您可以控制每個參考命令的主控台輸出和變數輸入。 使用 --output 參數覆蓋參考命令的預設設定。

Azure CLI 的預設輸出是 json。 當不需要主控台輸出時,將預設輸出設定為 none

az config set core.output=none

您可以使用 參數覆寫任何 Azure CLI 參考命令 --output 的預設輸出。 以下是改變和測試命令輸出的命令文稿:

# set your default output to table
az config set core.output=table

# show your active subscription in table format
# notice how only a subset of properties are returned in the table
az account show

# override your table default and show your active subscription in jsonc format
az account show --output jsonc

# reset your default output to json
az config set core.output=json

另請參閱