快速入門:使用 Azure CLI 建立 Resource Graph 共用查詢
本快速入門說明如何使用 Azure CLI 和 Resource Graph 延伸模組來建立 Azure Resource Graph 共用查詢。 az graph shared-query 命令是 az graph query 的實驗性功能。
您可以使用實驗性功能的命令從 Azure CLI 執行共用查詢,也可以從 Azure 入口網站執行共用查詢。 共用查詢是 Azure Resource Manager 物件,您可以在 Azure Resource Graph Explorer 中授與權限或執行。 完成時,您可以移除 Resource Graph 延伸模組。
必要條件
- 如果您沒有 Azure 帳戶,請在您開始之前先建立 免費帳戶。
- Azure CLI 必須是 Resource Graph 延伸模組的 2.22.0 版或更高版本。
- 您可以在其中執行 Azure CLI 命令的 Bash 殼層環境。 例如,Visual Studio Code 終端機工作階段中的 Git Bash。
安裝 擴充功能
若要讓 Azure CLI 使用 Azure Resource Graph 查詢資源,必須安裝 Resource Graph 延伸模組。 第一次使用 az graph
執行查詢時,會顯示提示以安裝延伸模組。 否則,使用下列步驟進行手動安裝。
列出可用的延伸模組和版本:
az extension list-available --output table
安裝擴充功能:
az extension add --name resource-graph
確認已安裝延伸模組:
az extension list --output table
顯示延伸模組的語法:
az graph query --help
如需 Azure CLI 延伸模組的詳細資訊,請移至使用和管理 Azure CLI 的延伸模組。
連接到 Azure
從 Visual Studio Code 終端機工作階段中連線到 Azure。 如果您有多個訂用帳戶,請執行命令來設定您訂用帳戶的內容。 使用您的 Azure 訂用帳戶識別碼來取代 <subscriptionID>
。
az login
# Run these commands if you have multiple subscriptions
az account list --output table
az account set --subscription <subscriptionID>
建立共用查詢
建立一個資源群組和一個共用,以用於彙總按位置分組的所有資源的計數。
建立資源群組來儲存 Azure Resource Graph 共用查詢。
az group create --name "demoSharedQuery" --location westus2
建立共用查詢。
az graph shared-query create --name "Summarize resources by location" \ --description "This shared query summarizes resources by location for a pinnable map graphic." \ --graph-query "Resources | summarize count() by location" \ --resource-group demoSharedQuery
列出資源群組中的所有共用查詢。
az graph shared-query list --resource-group demoSharedQuery
將結果限制為特定的共用查詢。
az graph shared-query show --resource-group "demoSharedQuery" \ --name "Summarize resources by location"
執行共用查詢
您可以使用 Azure CLI 實驗性功能語法或 Azure 入口網站來執行共用查詢。
使用實驗性功能來執行共用查詢
使用 az graph query
命令中的 {{shared-query-uri}}
語法,在 Azure CLI 中執行共用查詢。 您會取得您的共用查詢的資源識別碼,並將其儲存在變數中。 當您執行共用查詢時,會使用該變數。
sharedqueryid=$(az graph shared-query show --resource-group "demoSharedQuery" \
--name "Summarize resources by location" \
--query id \
--output tsv)
az graph query --graph-query "{{$sharedqueryid}}"
您可以使用 subscriptions
參數來限制結果。
az graph query --graph-query "{{$sharedqueryid}}" --subscriptions 11111111-1111-1111-1111-111111111111
從入口網站執行共用查詢
您可以使用 Azure Resource Graph 總管來驗證共用查詢是否正常運作。 若要變更範圍,請使用頁面左側的 [範圍] 功能表。
- 登入 Azure 入口網站。
- 在頁面頂端的搜尋欄位中輸入資源圖表。
- 選取 [資源圖表總管]。
- 選取 [開啟查詢]。
- 將 [類型] 變更為 [共用查詢]。
- 選取查詢 [依 OS 計算 VM]。
- 選取 [執行查詢] 並在 [結果] 索引標籤中檢視輸出。
- 選取 [ 圖表 ],然後選取 [ 地圖 ] 以檢視位置地圖。
您也可以從您的資源群組執行查詢。
- 在 Azure 中,移至資源群組 demoSharedQuery。
- 從 [概觀] 索引標籤中,選取查詢依 OS 計算 VM。
- 選取 [結果] 索引標籤。
- 選取 [ 圖表 ],然後選取 [ 地圖 ] 以檢視位置地圖。
清除資源
若要移除共享查詢:
az graph shared-query delete --name "Summarize resources by location" --resource-group demoSharedQuery
刪除資源群組時,系統便會刪除該資源群組及其所有的資源。 若要移除資源群組:
az group delete --name demoSharedQuery
若要移除 Resource Graph 延伸模組,請執行下列命令:
az extension remove --name resource-graph
若要登出您的 Azure CLI 工作階段:
az logout
下一步
在本快速入門中,您已將 Resource Graph 延伸模組新增至 Azure CLI 環境,並建立共用查詢。 若要深入了解 Resource Graph 語言,請繼續前往查詢語言詳細資料頁面。