使用适用于 Visual Studio Code 的 Azure Policy 扩展Use Azure Policy extension for Visual Studio Code

适用于 Azure Policy 扩展版本 0.0.21 和更高版本Applies to Azure Policy extension version 0.0.21 and newer

了解如何使用适用于 Visual Studio Code 的 Azure Policy 扩展来查找别名以及查看资源和策略。Learn how to use the Azure Policy extension for Visual Studio Code to look up aliases and review resources and policies. 首先,本文将介绍如何在 Visual Studio Code 中安装 Azure Policy 扩展。First, we'll describe how to install the Azure Policy extension in Visual Studio Code. 接下来演练如何查找别名。Then we'll walk through how to look up aliases.

可在 Visual Studio Code 支持的所有平台上安装适用于 Visual Studio Code 的 Azure Policy 扩展。Azure Policy extension for Visual Studio Code can be installed on all platforms that are supported by Visual Studio Code. 支持的平台包括 Windows、Linux 和 macOS。This support includes Windows, Linux, and macOS.

Note

在适用于 Visual Studio Code 的 Azure Policy 扩展中查看的、在本地对策略所做的更改不会同步到 Azure。Changes made locally to policies viewed in the Azure Policy extension for Visual Studio Code aren't synced to Azure.

先决条件Prerequisites

完成本文中的步骤需要以下各项:The following items are required for completing the steps in this article:

安装 Azure Policy 扩展Install Azure Policy extension

满足先决条件后,可遵循以下步骤安装适用于 Visual Studio Code 的 Azure Policy 扩展:After you meet the prerequisites, you can install Azure Policy extension for Visual Studio Code by following these steps:

  1. 打开 Visual Studio Code。Open Visual Studio Code.

  2. 在菜单栏中,转到“视图” > “扩展”。 From the menu bar, go to View > Extensions.

  3. 在搜索框中输入 Azure PolicyIn the search box, enter Azure Policy.

  4. 在搜索结果中选择“Azure Policy”,然后选择“安装”。 Select Azure Policy from the search results, and then select Install.

  5. 根据需要选择“重载”。 Select Reload when necessary.

设置 Azure 环境Set the Azure environment

国家云用户请先遵循以下步骤设置 Azure 环境:For a national cloud user, follow these steps to set the Azure environment first:

  1. 选择“文件”>“首选项”>“设置”。 Select File\Preferences\Settings.

  2. 搜索以下字符串:Azure: 云”Search on the following string: Azure: Cloud

  3. 在列表中选择国家云:Select the nation cloud from the list:

    设置 Visual Studio Code 的默认 Azure 云登录名

连接到 Azure 帐户Connect to an Azure account

若要评估资源和查找别名,必须连接到 Azure 帐户。To evaluate resources and lookup aliases, you must connect to your Azure account. 遵循以下步骤从 Visual Studio Code 连接到 Azure:Follow these steps to connect to Azure from Visual Studio Code:

  1. 通过 Azure Policy 扩展或命令面板登录到 Azure。Sign in to Azure from the Azure Policy extension or the Command Palette.

    • Azure Policy 扩展Azure Policy extension

      在 Azure Policy 扩展中,选择“登录到 Azure”。 From the Azure Policy extension, select Sign in to Azure.

      Azure Policy 扩展中 Visual Studio Code 的 Azure 云登录名

    • 命令面板Command Palette

      在菜单栏中,转到“视图” > “命令面板”,然后输入“Azure: 登录”。From the menu bar, go to View > Command Palette, and enter Azure: Sign In.

      命令面板中 Visual Studio Code 的 Azure 云登录名

  2. 按照登录说明登录到 Azure。Follow the sign in instructions to sign in to Azure. 连接后,Visual Studio Code 窗口底部的状态栏上会显示 Azure 帐户名称。After you're connected, your Azure account name is shown on the status bar at the bottom of the Visual Studio Code window.

选择订阅Select subscriptions

首次登录时,Azure Policy 扩展只会加载默认的订阅资源和策略。When you first sign in, only the default subscription resources and policies are loaded by the Azure Policy extension. 若要添加或删除要显示其资源和策略的订阅,请执行以下步骤:To add or remove subscriptions from displaying resources and policies, follow these steps:

  1. 通过命令面板或窗口页脚启动订阅命令。Start the subscription command from the Command Palette or the window footer.

    • 命令面板:Command Palette:

      在菜单栏中,转到“视图” > “命令面板”,然后输入“Azure: 选择订阅”。From the menu bar, go to View > Command Palette, and enter Azure: Select Subscriptions.

    • 窗口页脚Window footer

      在屏幕底部的窗口页脚中,选择与“Azure: <你的帐户>”匹配的段。 In the window footer at the bottom of the screen, select the segment that matches Azure: <your account>.

  2. 使用筛选框按名称快速查找订阅。Use the filter box to quickly find subscriptions by name. 然后,选中或清除每个订阅对应的复选框,以设置 Azure Policy 扩展要显示的订阅。Then, check or remove the check from each subscription to set the subscriptions shown by the Azure Policy extension. 添加或删除要显示的订阅后,选择“确定”。 When done adding or removing subscriptions to display, select OK.

搜索和查看资源Search for and view resources

Azure Policy 扩展在“资源”窗格中按资源提供程序和资源组列出所选订阅中的资源。 The Azure Policy extension lists resources in the selected subscriptions by Resource Provider and by resource group in the Resources pane. 树视图包含所选订阅中或订阅级别的以下资源分组:The treeview includes the following groupings of resources within the selected subscription or at the subscription level:

  • 资源提供程序Resource Providers
    • 每个已注册的资源提供程序,其中包含具有策略别名的资源和相关子资源Each registered Resource Provider with resources and related child resources that have policy aliases
  • 资源组Resource Groups
    • 按资源所在的资源组列出所有资源All resources by the resource group they're in

默认情况下,该扩展将按现有资源以及具有策略别名的资源筛选“资源提供程序”部分。By default, the extension filters the 'Resource Provider' portion by existing resources and resources that have policy aliases. 在“设置” > “扩展” > “Azure Policy”中更改此行为可在不进行筛选的情况下查看所有资源提供程序。 Change this behavior in Settings > Extensions > Azure Policy to see all Resource Providers without filtering.

在单个订阅中包含数百甚至数千个资源的客户可能更倾向于通过搜索来查找其资源。Customers with hundreds or thousands of resources in a single subscription may prefer a searchable way to locate their resources. 在 Azure Policy 扩展中可通过以下步骤搜索特定的资源:The Azure Policy extension makes it possible to search for a specific resource with the following steps:

  1. 通过 Azure Policy 扩展或命令面板启动搜索界面。Start the search interface from the Azure Policy extension or the Command Palette.

    • Azure Policy 扩展Azure Policy extension

      在 Azure Policy 扩展中,将鼠标悬停在“资源”面板上,选择省略号图标,然后选择“搜索资源”。 From the Azure Policy extension, hover over the Resources panel and select the ellipsis, then select Search Resources.

    • 命令面板:Command Palette:

      在菜单栏中,转到“视图”>“命令面板”,然后输入“资源: 搜索资源”。From the menu bar, go to View > Command Palette, and enter Resources: Search Resources.

  2. 如果已选择显示多个订阅,请使用筛选器来选择要搜索的订阅。If more than one subscription is selected for display, use the filter to select which subscription to search.

  3. 使用筛选器选择属于前面所选订阅的、要搜索的资源组。Use the filter to select which resource group to search that is part of the previously chosen subscription.

  4. 使用筛选器选择要显示的资源。Use the filter to select which resource to display. 可对资源名称和资源类型使用筛选器。The filter works for both the resource name and the resource type.

发现资源属性的别名Discover aliases for resource properties

选择某个资源时(不管是通过搜索界面选择,还是在树视图中选择),Azure Policy 扩展将会打开表示该资源及其所有资源管理器属性值的 JSON 文件。When a resource is selected, whether through the search interface or by selecting it in the treeview, the Azure Policy extension opens the JSON file representing that resource and all its Resource Manager property values.

打开某个资源后,将鼠标悬停在资源管理器属性名称或值上可显示 Azure Policy 别名(如果存在)。Once a resource is open, hovering over the Resource Manager property name or value displays the Azure Policy alias if one exists. 在此示例中,资源的类型是 Microsoft.Compute/virtualMachines,鼠标已悬停在 properties.storageProfile.imageReference.offer 属性上。In this example, the resource is a Microsoft.Compute/virtualMachines resource type and the properties.storageProfile.imageReference.offer property is hovered over. 悬停鼠标可显示匹配的别名。Hovering shows the matching aliases.

在 Azure Policy 扩展中悬停鼠标即可显示资源管理器属性别名

搜索和查看策略与分配Search for and view policies and assignments

对于选择显示的订阅,Azure Policy 扩展将在“策略”窗格中以树视图的形式列出策略类型和策略分配。 The Azure Policy extension lists policy types and policy assignments as a treeview for the subscriptions selected to be displayed in the Policies pane. 在单个订阅中包含数百甚至数千个策略或分配的客户可能更倾向于通过搜索来查找其策略或分配。Customers with hundreds or thousands of policies or assignments in a single subscription may prefer a searchable way to locate their policies or assignments. 在 Azure Policy 扩展中可通过以下步骤搜索特定的策略或分配:The Azure Policy extension makes it possible to search for a specific policy or assignment with the following steps:

  1. 通过 Azure Policy 扩展或命令面板启动搜索界面。Start the search interface from the Azure Policy extension or the Command Palette.

    • Azure Policy 扩展Azure Policy extension

      在 Azure Policy 扩展中,将鼠标悬停在“策略”面板上,选择省略号图标,然后选择“搜索策略”。 From the Azure Policy extension, hover over the Policies panel and select the ellipsis, then select Search Policies.

    • 命令面板:Command Palette:

      在菜单栏中,转到“视图”>“命令面板”,然后输入“策略: 搜索策略”。From the menu bar, go to View > Command Palette, and enter Policies: Search Policies.

  2. 如果已选择显示多个订阅,请使用筛选器来选择要搜索的订阅。If more than one subscription is selected for display, use the filter to select which subscription to search.

  3. 使用筛选器选择属于前面所选订阅的、要搜索的策略类型或分配。Use the filter to select which policy type or assignment to search that is part of the previously chosen subscription.

  4. 使用筛选器选择要显示的策略。Use the filter to select which policy or to display. 可对策略定义或策略分配的 displayName 使用筛选器。The filter works for displayName for the policy definition or policy assignment.

选择策略或分配时(不管是通过搜索界面选择,还是在树视图中选择),Azure Policy 扩展将会打开表示该策略或分配及其所有资源管理器属性值的 JSON。When selecting a policy or assignment, whether through the search interface or by selecting it in the treeview, the Azure Policy extension opens the JSON that represents the policy or assignment and all its Resource Manager property values. 该扩展可以验证已打开的 Azure Policy JSON 架构。The extension can validate the opened Azure Policy JSON schema.

注销Sign out

在菜单栏中,转到“视图” > “命令面板”,然后输入“Azure: 注销”。From the menu bar, go to View > Command Palette, and then enter Azure: Sign Out.

后续步骤Next steps