如何安装和配置 Azure PowerShellHow to install and configure Azure PowerShell

什么是 Azure PowerShell?What is Azure PowerShell?

Azure PowerShell 是一组模块,提供用于通过 Windows PowerShell 管理 Azure 的 cmdlet。Azure PowerShell is a set of modules that provide cmdlets to manage Azure with Windows PowerShell. 可以使用 cmdlet 来创建、测试、部署和管理通过 Azure 平台传送的解决方案和服务。You can use the cmdlets to create, test, deploy, and manage solutions and services delivered through the Azure platform. 在大多数情况下,这些 cmdlet 可用于执行在 Azure 门户中可以执行的任务,例如创建和配置云服务、虚拟机、虚拟网络和 Web 应用。In most cases, the cmdlets can be used for the same tasks as the Azure Portal, such as creating and configuring cloud services, virtual machines, virtual networks, and web apps.

版本控制的工作原理How versioning works

Azure PowerShell 使用语义版本控制,也就是说,如果版本 A 更新为版本 B,则版本 A 包含最新的 API。Azure PowerShell uses Semantic Versioning, which means that if version A > version B, then version A has the most up-to-date APIs. 这也意味着主要版本发生更改会导致一个或多个 cmdlet 发生重大更改。Also, it means that changes in the major version mean a breaking change in one or more cmdlets. 例如,版本 1.7.0 是用于解决 Azure PowerShell 1.x 版本中重大更改问题的修补程序。So, for example, version 1.7.0 is a hotfix to address a breaking change in the 1.x versions of Azure PowerShell.

有关在 Azure PowerShell 中实践语义版本控制的详细信息,请参阅 http://semver.org 上的语义版本控制规范For more information about Semantic Versioning practices in Azure PowerShell, see the Semantic Versioning Specification at: http://semver.org

若要获取最新的 API,应使用版本 2.x。To get the latest APIs, you should use version 2.x. 但是,如果针对版本 1.x 编写了脚本,而且不希望吸收 2.x 发行说明中所述的版本 2.x 重大更改,则应安装 1.7.0。But if you have scripts written against version 1.x and you don’t want to absorb the breaking changes in version 2.x described in the 2.x Release Notes, then you should install 1.7.0.

如果已安装配置文件模块的最新版本,并随后加载依赖于它的某个模块的早期版本,则会导致版本不匹配。A version mismatch can result if the latest version of the profile module is installed, and an earlier version of a module that depends on it is subsequently loaded. 解决此问题的最简单方法是从最新的 .msi 安装。The simplest way to resolve this is to install from the latest .msi. .msi 会自动清理模块的较旧版本。The .msi automatically cleans up older versions of the modules.

并行安装模块版本Installing module versions side-by-side

版本 2.1.0(以及 AzureStack 版本 1.2.6)是可以同时安装和使用的第一个模块版本。Version 2.1.0 (and version 1.2.6 for AzureStack) are the first module versions designed to be installed and used side-by-side. Azure PowerShell 使用二进制模块,因此,必须打开新的 PowerShell 窗口并使用 Import-Module 来导入 AzureRM cmdlet 的特定版本:Because Azure PowerShell uses binary modules, you must open a new PowerShell window and use Import-Module to import a specific version of the AzureRM cmdlets:

Import-Module AzureRM – RequiredVersion 2.1.0Import-Module AzureRM – RequiredVersion 2.1.0

2.1.0 以前的版本(1.2.6 除外)不能正常地与其他 Azure PowerShell 模块版本同时工作。Versions before 2.1.0 (other than 1.2.6) do not work well side-by-side with other Azure PowerShell module versions. 使用类似如上的命令加载 Azure PowerShell 模块的早期版本时,会加载 AzureRM.Profile 模块的不兼容版本,导致 cmdlet 在每次执行 cmdlet 时要求登录,即使已登录也是如此。When loading an earlier version of the Azure PowerShell modules using a command like the one above, incompatible versions of the AzureRM.Profile module will be loaded, resulting in the cmdlets asking you to log in whenever you execute a cmdlet, even after you have logged in.

解决此问题的最简单方法是从 WebPI 源或 .msi 安装最新的 Azure PowerShell,这会删除从库安装的模板的早期版本。The easiest way to resolve this is to install the latest Azure PowerShell from the WebPI feed or .msi – this removes earlier versions of the modules installed from the gallery.

请注意,Azure 和 AzureRM 模块拥有共同的依赖项,因此,使用这两个模块时,如果要更新其中的一个模块,则应同时更新两个模块。Note that both Azure and AzureRM modules have dependencies in common, so if you use both modules, when updating one, you should update both. Azure 模块的早期版本加载并行模块时与 AzureRM 模块的早期版本加载并行模块时遇到的问题一样。Earlier versions of the Azure module have the same issue with side-by-side module loading that earlier versions of the AzureRM module have.

步骤 1:安装Step 1: Install

以下是安装 Azure PowerShell 的两种方法。Following are the two methods by which you can install Azure PowerShell. 可以通过 WebPI 或 PowerShell 库进行安装:You can install it either from WebPI, or the PowerShell Gallery:

从 WebPI 安装 Azure PowerShellInstalling Azure PowerShell from WebPI

从 WebPI 安装 Azure PowerShell 1.0 和更高版本的方法与安装 0.9.x 版本是一样的。Installing Azure PowerShell 1.0 and greater from WebPI is the same as it was for 0.9.x. 下载 Azure PowerShell 并开始安装。Download Azure PowerShell and start the install. 如果安装了 Azure PowerShell 0.9.x,会在升级期间卸载版本 0.9.x。If you have Azure PowerShell 0.9.x installed, version 0.9.x will be uninstalled as part of the upgrade. 如果从 PowerShell 库安装了 Azure PowerShell 模块,安装程序会在安装之前自动删除这些模块,以确保 Azure PowerShell 环境一致。If you installed Azure PowerShell modules from PowerShell Gallery, the installer automatically removes the modules before installation to ensure a consistent Azure PowerShell environment.


如果之前从 PowerShell 库安装了 Azure 模块,安装程序将自动删除这些模块。If you have previously installed Azure modules from the PowerShell Gallery, the installer will automatically remove them. 这是为了防止混淆已安装的模块版本及其所在的位置。This prevents confusion about which module versions you have installed and where they are located. PowerShell 库模块通常安装在 %ProgramFiles%\WindowsPowerShell\Modules 中。PowerShell Gallery modules will normally install in %ProgramFiles%\WindowsPowerShell\Modules. 相比之下,WebPI 安装程序将在 %ProgramFiles(x86)%\Microsoft SDKs\Azure\PowerShell\ 中安装 Azure 模块。In contrast, the WebPI installer will install the Azure modules in %ProgramFiles(x86)%\Microsoft SDKs\Azure\PowerShell\. 如果在安装过程中发生错误,可以手动删除 %ProgramFiles%\WindowsPowerShell\Modules 文件夹中的 Azure* 文件夹,然后重试安装。If an error occurs during install, you can manually remove the Azure* folders in your %ProgramFiles%\WindowsPowerShell\Modules folder, and try the installation again.

安装完成后, $env:PSModulePath 设置中应会有包含 Azure PowerShell cmdlet 的目录。Once the installation completes, your $env:PSModulePath setting should include the directories containing the Azure PowerShell cmdlets.


从 WebPI 安装时,会发生一个有关 PowerShell $env:PSModulePath 的已知问题。There is a known issue with PowerShell $env:PSModulePath that can occur when installing from WebPI. 如果计算机因系统更新或其他安装而需要重启,有可能会导致更新 $env:PSModulePath 不包含 Azure PowerShell 的安装路径。If your computer requires a restart due to system updates or other installations, it may cause updates $env:PSModulePath to not include the path where Azure PowerShell is installed. 如果发生这种情况,在安装或升级之后尝试使用 Azure PowerShell cmdlet 时,可能会看到“cmdlet 无法识别”消息。If this occurs, you may see a 'cmdlet not recognized' message when attempting to use Azure PowerShell cmdlets after the installation or upgrade. 如果发生这种情况,重启计算机应该可以解决该问题。If this occurs, restarting the machine should fix the problem.

如果尝试加载或执行 cmdlet,会收到如下消息:If you receive a message like the following when attempting to load or execute cmdlets:

    PS C:\> Get-AzureRmResource
    Get-AzureRmResource : The term 'Get-AzureRmResource' is not recognized as the name of a cmdlet, function,
    script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
    correct and try again.
    At line:1 char:1
    + Get-AzureRmResource
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (get-azurermresourcefork:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

这可以通过以下方法更正:重启计算机,或者按照以下方式从 C:\Program Files\WindowsPowerShell\Modules\Azure\XXXX\ 导入 cmdlet(其中 XXXX 是所安装 PowerShell 的版本):This can be corrected by restarting the machine or importing the cmdlets from C:\Program Files\WindowsPowerShell\Modules\Azure\XXXX\ as following (where XXXX is the version of PowerShell installed:

import-module "C:\Program Files\WindowsPowerShell\Modules\Azure\XXXX\azure.psd1"
import-module "C:\Program Files\WindowsPowerShell\Modules\Azure\XXXX\expressroute\expressroute.psd1"

在提升的 Windows PowerShell 或 PowerShell 集成脚本环境 (ISE) 提示符下,使用以下命令从 PowerShell 库安装 Azure PowerShell 1.3.0 或更高版本:Install Azure PowerShell 1.3.0 or greater from the PowerShell Gallery using an elevated Windows PowerShell or PowerShell Integrated Scripting Environment (ISE) prompt using the following commands:

# Install the Azure Resource Manager modules from the PowerShell Gallery
Install-Module AzureRM

# Install the Azure Service Management module from the PowerShell Gallery
Install-Module Azure

有关这些命令的详细信息More about these commands

  • Install-Module AzureRM 为 Azure Resource Manager cmdlet 安装汇总模块。Install-Module AzureRM installs a rollup module for the Azure Resource Manager cmdlets. AzureRM 模块依赖于The AzureRM module depends on
  • 每个 Azure Resource Manager 模块的特定版本范围。a particular version range for each Azure Resource Manager module. 包含的版本范围可以确保在安装同一主要版本的 AzureRM 模块时,不包含重大的模块更改。The included version range assures that no breaking module changes can be included when installing AzureRM modules with the same major version. 安装 AzureRM 模块时,会从 PowerShell 库下载并安装先前未安装的所有 Azure Resource Manager 模块。When you install the AzureRM module, any Azure Resource Manager module that has not previously been installed will be downloaded and installed from the PowerShell Gallery. 有关 Azure PowerShell 模块所用的语义版本控制的详细信息,请参阅 semver.orgFor more information on the semantic versioning used by Azure PowerShell modules, see semver.org.
  • Install-Module Azure 安装 Azure 模块。Install-Module Azure installs the Azure module. 此模块是 Azure PowerShell 0.9.x 中的服务管理模块。This module is the Service Management module from Azure PowerShell 0.9.x. 这应该没有任何重要更改,而且可与前一版本的 Azure 模块互换。This should have no major changes and be interchangeable for the previous version of the Azure module.

步骤 2:开始Step 2: Start

可以通过标准的 Windows PowerShell 控制台或 PowerShell 集成脚本环境 (ISE) 运行 cmdlet。You can run the cmdlets from the standard Windows PowerShell console, or from PowerShell Integrated Scripting Environment (ISE). 用于打开控制台的方法取决于正在运行的 Windows 的版本:The method you use to open either console depends on the version of Windows you're running:

  • 在至少运行 Windows 8 或 Windows Server 2012 的计算机上,可以使用内置搜索。On a computer running at least Windows 8 or Windows Server 2012, you can use the built-in Search. 从“开始”屏幕开始键入 power。From the Start screen, begin typing power. 此时将返回范围内的应用列表,包括 Windows PowerShell。This returns a scoped list of apps that includes Windows PowerShell. 若要打开控制台,请单击任一应用程序。To open the console, click either app. (若要将应用固定在“开始”屏幕,请右键单击此图标。)(To pin the app to the Start screen, right-click the icon.)

  • 在运行早于 Windows 8 或 Windows Server 2012 的版本的计算机上,请使用“开始”菜单。On a computer running a version earlier than Windows 8 or Windows Server 2012, use the Start menu. 从“开始”菜单中,依次单击“所有程序”、“附件”、“Windows PowerShell”文件夹和“Windows PowerShell”。From the Start menu, click All Programs, click Accessories, click the Windows PowerShell folder, and then click Windows PowerShell.

也可以运行 Windows PowerShell ISE,使用菜单项和键盘快捷方式来执行可在 Windows PowerShell 控制台中执行的许多相同任务。You can also run the Windows PowerShell ISE to use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console. 若要使用 ISE,请在 Windows PowerShell 控制台、Cmd.exe 或“运行”框中,键入 powershell_ise.exeTo use the ISE, in the Windows PowerShell console, Cmd.exe, or in the Run box, type, powershell_ise.exe.

帮助入门的命令Commands to help you get started

# To make sure the Azure PowerShell module is available after you install
Get-Module –ListAvailable 

# To log in to Azure Resource Manager

# You can also use a specific Tenant if you would like a faster log in experience
# Login-AzureRmAccount -TenantId xxxx

# To view all subscriptions for your account

# To select a default subscription for your current session
Get-AzureRmSubscription –SubscriptionName “your sub” | Select-AzureRmSubscription

# View your current Azure PowerShell session context
# This session state is only applicable to the current session and will not affect other sessions

# To select the default storage context for your current session
Set-AzureRmCurrentStorageAccount –ResourceGroupName “your resource group” –StorageAccountName “your storage account name”

# View your current Azure PowerShell session context
# Note: the CurrentStorageAccount is now set in your session context

# To list all of the blobs in all of your containers in all of your accounts
Get-AzureRmStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob

步骤 3:连接 Step 3: Connect

cmdlet 需要使用订阅来管理服务。The cmdlets need your subscription so they can manage your services. 如果没有 Azure 订阅,可以购买一个。You can purchase an Azure subscription if you don't already have one. 有关说明,请参阅如何购买 AzureFor instructions, see How to buy Azure.

  1. 键入 Login-AzureRmAccountType Login-AzureRmAccount

  2. 键入与帐户关联的电子邮件地址和密码。Type the email address and password associated with your account. Azure 将对凭据信息进行身份验证并保存,然后关闭该窗口。Azure authenticates and saves the credential information, and then closes the window.


登录到工作帐户或学校帐户:Sign into your work or school account:

$cred = Get-Credential
Login-AzureRmAccount -Credential $cred


如果组织帐户有多个关联的租户,请指定 TenantId 参数:If you have more than one tenant associated with your organizational account, specify the TenantId parameter:

$loadersubscription = Get-AzureRmSubscription -SubscriptionName $YourSubscriptionName -TenantId $YourAssociatedSubscriptionTenantId


这种非交互式登录方法仅适用于工作或学校帐户。This non-interactive log in method only works with a work or school account. 工作或学校帐户是由公司或学校所管理的用户,并在公司或学校的 Azure Active Directory 实例中定义。A work or school account is a user that is managed by your work or school, and defined in the Azure Active Directory instance for your work or school. 如果当前没有工作或学校帐户,且已使用 Microsoft 帐户登录到 Azure 订阅,则可以按照以下步骤轻松地创建一个工作或学校帐户。If you do not currently have a work or school account, and are using a Microsoft account to log in to your Azure subscription, you can easily create one using the following steps.

  1. 登录到 Azure 经典门户,然后单击“Active Directory”。Log in to the Azure classic portal, and click on Active Directory.

  2. 如果目录不存在,请选择“创建目录”,并提供所请求的信息。If no directory exists, select Create your directory and provide the requested information.

  3. 选择目录,并添加新用户。Select your directory and add a new user. 这个新用户可以使用工作或学校帐户登录。This new user can sign in using a work or school account. 创建用户时,系统将提供用户电子邮件地址和临时密码。During the creation of the user, you will be supplied with both an e-mail address for the user and a temporary password. 保存此信息,因为下面的步骤 5 将要用到。Save this information, as it is used in step 5 below.

  4. 从 Azure 经典门户中,选择“设置”,然后选择“管理员”。From the Azure classic portal, select Settings and then select Administrators. 选择“添加”,并将新用户添加为共同管理员。Select Add, and add the new user as a co-administrator. 这样工作或学校帐户即可管理 Azure 订阅。This allows the work or school account to manage your Azure subscription.

  5. 最后,从 Azure 经典门户注销,然后使用工作或学校帐户重新登录。Finally, log out of the Azure classic portal and then log back in using the work or school account. 如果这是使用此帐户首次登录,系统会提示更改密码。If this is the first time logging in with this account, you will be prompted to change the password.

有关 Azure 中的身份验证和订阅管理的详细信息,请参阅管理帐户、订阅和管理角色For more information about authentication and subscription management in Azure, see Manage Accounts, Subscriptions, and Administrative Roles.

查看帐户和订阅详细信息View account and subscription details

可以设置多个帐户和订阅供 Azure PowerShell 使用。You can have multiple accounts and subscriptions available for use by Azure PowerShell. 可以通过多次运行 Add-AzureRmAccount 来添加多个帐户。You can add multiple accounts by running Add-AzureRmAccount more than once.

若要显示可用的 Azure 帐户,请键入 Get-AzureAccountTo display the available Azure accounts, type Get-AzureAccount.

若要显示 Azure 订阅,请键入 Get-AzureRmSubscriptionTo display your Azure subscriptions, type Get-AzureRmSubscription.

获取帮助##Getting help##

这些资源提供特定 cmdlet 的帮助信息:These resources provide help for specific cmdlets:

  • 从控制台内,可以使用内置的帮助系统。From within the console, you can use the built-in Help system. Get-Help cmdlet 提供对此系统的访问。The Get-Help cmdlet provides access to this system.

  • 要获得社区中的帮助信息,请尝试以下常见论坛:For help from the community, try these popular forums:

了解详细信息Learn More

若要详细了解如何使用 cmdlet,请参阅以下资源:See the following resources to learn more about using the cmdlets:

有关使用 Windows PowerShell 的基本说明,请参阅使用 Windows PowerShellFor basic instructions about using Windows PowerShell, see Using Windows PowerShell.

有关 cmdlet 的参考信息,请参阅 Azure Cmdlet 参考For reference information about the cmdlets, see Azure Cmdlet Reference.

有关可帮助了解如何使用脚本来管理 Azure 的示例脚本和说明,请参阅脚本中心For sample scripts and instructions to help you learn to use scripting to manage Azure, see the Script Center.