快速入門:使用 JavaScript 建立原則指派,以識別不符合規範的資源
了解 Azure 中合規性的第一個步驟是識別您資源的狀態。 在本快速入門中,您會建立原則指派,以識別未使用受控磁碟的虛擬機器。 完成時,您會識別出「不符合規範」的虛擬機器。
JavaScript 程式庫可用來從命令列或在指令碼中管理 Azure 資源。 本指南說明如何使用 JavaScript 程式庫建立原則指派。
必要條件
Azure Cloud Shell
Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。
要啟動 Azure Cloud Shell:
選項 | 範例/連結 |
---|---|
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 | ![]() |
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 | ![]() |
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 | ![]() |
若要使用 Azure Cloud Shell:
啟動 Cloud Shell。
選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。
透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。
選取 Enter 鍵執行程式碼或命令。
新增原則程式庫
若要讓 JavaScript 能夠與 Azure 原則搭配使用,必須新增程式庫。 這些程式庫適用於可以使用 JavaScript 的任何位置,包括 Windows 10 上的 Bash。
執行下列命令以設定新的 Node.js 專案。
npm init -y
新增 yargs 程式庫的參考。
npm install yargs
新增 Azure 原則程式庫的參考。
# arm-policy is for working with Azure Policy objects such as definitions and assignments npm install @azure/arm-policy # arm-policyinsights is for working with Azure Policy compliance data such as events and states npm install @azure/arm-policyinsights
新增 Azure 驗證程式庫的參考。
npm install @azure/identity
注意
在 package.json 中驗證
@azure/arm-policy
的版本是 5.0.1 或更高版本、@azure/arm-policyinsights
的版本是 5.0.0 或更高版本,以及@azure/identity
的版本是 2.0.4 或更高版本。
建立原則指派
在本快速入門中,您會建立一個原則指派,並且指派稽核沒有受控磁碟的虛擬機器 (06a78e20-9358-41c9-923c-fb736d382a4d
) 定義。 此原則定義會識別與原則定義中設定之條件不相符的資源。
建立名為 policyAssignment.js 的新檔案,並輸入下列程式碼。
const argv = require("yargs").argv; const { DefaultAzureCredential } = require("@azure/identity"); const { PolicyClient } = require("@azure/arm-policy"); if (argv.subID && argv.name && argv.displayName && argv.policyDefID && argv.scope && argv.description) { const createAssignment = async () => { const credentials = new DefaultAzureCredential(); const client = new PolicyClient(credentials, argv.subID); const result = await client.policyAssignments.create( argv.scope, argv.name, { displayName: argv.displayName, policyDefinitionId: argv.policyDefID, description: argv.description } ); console.log(result); }; createAssignment(); }
在終端機中輸入下列命令:
node policyAssignment.js ` --subID "{subscriptionId}" ` --name "audit-vm-manageddisks" ` --displayName "Audit VMs without managed disks Assignment" ` --policyDefID "/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d" ` --description "Shows all virtual machines not using managed disks" ` --scope "{scope}"
上述命令會使用下列資訊:
- subID - 驗證內容的訂用帳戶識別碼。 請務必將
{subscriptionId}
取代為您的訂用帳戶。 - 名稱 - 原則指派物件的唯一名稱。 上述範例使用 audit-vm-manageddisks。
- displayName - 原則指派的顯示名稱。 在此案例中,您會使用稽核沒有受控磁碟指派的虛擬機器。
- policyDefID - 原則定義路徑,用來建立指派的依據。 在此案例中,即為原則定義稽核沒有受控磁碟的虛擬機器的 ID。
- description - 原則用途或為何指派給此範圍的更深入說明。
- scope - 範圍會決定在哪些資源或資源群組上強制執行原則指派。 此範圍可包含管理群組到個別資源。 請務必將
{scope}
取代為下列其中一個模式:- 管理群組:
/providers/Microsoft.Management/managementGroups/{managementGroup}
- 訂用帳戶:
/subscriptions/{subscriptionId}
- 資源群組:
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}
- 資源:
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/[{parentResourcePath}/]
- 管理群組:
您現在可以識別不相容的資源,以了解環境的相容性狀態。
識別不符合規範的資源
現在您已建立原則指派,您可以識別不符合規範的資源。
建立名為 policyState.js 的新檔案,並輸入下列程式碼。
const argv = require("yargs").argv; const { DefaultAzureCredential } = require("@azure/identity"); const { PolicyInsightsClient } = require("@azure/arm-policyinsights"); if (argv.subID && argv.name) { const getStates = async () => { const credentials = new DefaultAzureCredential(); const client = new PolicyInsightsClient(credentials); const result = client.policyStates.listQueryResultsForSubscription( "latest", argv.subID, { queryOptions: { filter: "IsCompliant eq false and PolicyAssignmentId eq '" + argv.name + "'", apply: "groupby((ResourceId))" } } ); console.log(result); }; getStates(); }
在終端機中輸入下列命令:
node policyState.js --subID "{subscriptionId}" --name "audit-vm-manageddisks"
將 {subscriptionId}
取代為您想要查看特定原則指派之合規性結果的訂用帳戶;該指派的名稱為 'audit-vm-manageddisks',是我們在先前的步驟中建立的。 如需其他範圍和資料摘要方式的清單,請參閱 PolicyStates* 方法。
您的結果類似下列範例:
{
'additional_properties': {
'@odata.nextLink': None
},
'odatacontext': 'https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest',
'odatacount': 12,
'value': [{data}]
}
結果會符合原則指派在 Azure 入口網站檢視中的 [資源合規性] 索引標籤所顯示的內容。
清除資源
刪除原則指派 透過入口網站稽核沒有受控磁碟指派的 VM。 原則定義是內建的,因此沒有任何定義可供移除。
如果您想要從應用程式中移除已安裝的程式庫,請執行下列命令。
npm uninstall @azure/arm-policy @azure/arm-policyinsights @azure/identity yargs
後續步驟
在這個快速入門中,您指派原則定義以識別 Azure 環境中的不相容資源。
若要深入了解指派原則定義,以驗證新資源是符合規範的,請繼續進行以下的教學課程: