什么是 Azure PowerShell?

Azure PowerShell 是一组模块,提供用于通过 Windows PowerShell 管理 Azure 的 cmdlet。你可以使用 cmdlet 来创建、测试、部署和管理通过 Azure 平台传送的解决方案和服务。在大多数情况下,这些 cmdlet 可用于执行在 Azure 经典管理门户中可以执行的任务,例如,创建和配置云服务、虚拟机、虚拟网络和 Web 应用。

版本控制的工作原理

Azure PowerShell 使用语义版本控制,这意味着如果版本 A > 版本 B,则版本 A 具有最新的 API。这还意味着主要版本的更改表示一个或多个 cmdlet 中有重大更改。例如,版本 1.7.0 是用于解决 Azure PowerShell 1.x 版本中重大更改问题的修补程序。

有关 Azure PowerShell 中语义版本控制实践的详细信息,请参阅语义版本控制规范:http://semver.org

若要获取最新的 API,应使用版本 2.x。但是,如果针对版本 1.x 编写了脚本,而且不希望吸收 2.x 发行说明中所述的版本 2.x 重大更改,则应安装 1.7.0。

如果已安装配置文件模块的最新版本,并随后加载依赖于它的某个模块的早期版本,则会导致版本不匹配。解决此问题的最简单方法是从最新的 .msi 安装。.msi 会自动清理模块的较旧版本。

并行安装模块版本

版本 2.1.0(以及 AzureStack 版本 1.2.6)是第一个设计为并行安装和使用的模块版本。Azure PowerShell 使用二进制模块,因此,必须打开新的 PowerShell 窗口并使用 Import-Module 来导入 AzureRM cmdlet 的特定版本:

Import-Module AzureRM – RequiredVersion 2.1.0

2.1.0 之前的版本(1.2.6 除外)不能与其他 Azure PowerShell 模块版本并行使用。使用类似如上的命令加载 Azure PowerShell 模块的早期版本时,会加载 AzureRM.Profile 模块的不兼容版本,导致 cmdlet 在每次执行 cmdlet 时要求登录,即使已登录也是如此。

解决此问题的最简单方法是从 WebPI 源或 .msi 安装最新的 Azure PowerShell,这会删除从库安装的模板的早期版本。

请注意,Azure 和 AzureRM 模块拥有共同的依赖项,因此,使用这两个模块时,如果要更新其中的一个模块,则应同时更新两个模块。Azure 模块的早期版本加载并行模块时与 AzureRM 模块的早期版本加载并行模块时遇到的问题一样。

步骤 1:安装

以下是安装 Azure PowerShell 的两种方法。你可以通过 WebPI 或 PowerShell 库进行安装:

从 WebPI 安装 Azure PowerShell

从 WebPI 安装 Azure PowerShell 1.0 和更高版本的方法与安装 0.9.x 版本是一样的。下载 Azure PowerShell 并开始安装。如果安装了 Azure PowerShell 0.9.x,将在升级期间卸载版本 0.9.x。如果从 PowerShell 库安装了 Azure PowerShell 模块,安装程序将在安装之前自动删除这些模块,以确保 Azure PowerShell 环境一致。

Note

如果之前从 PowerShell 库安装了 Azure 模块,安装程序将自动删除这些模块。这是为了防止混淆已安装的模块版本及其所在的位置。PowerShell 库模块通常安装在%ProgramFiles%\WindowsPowerShell\Modules 中。相反,WebPI 安装程序会在%ProgramFiles(x86)%\Microsoft SDKs\Azure\PowerShell 中安装 Azure 模块。如果在安装过程中发生错误,可以手动删除 %ProgramFiles%\WindowsPowerShell\Modules 文件夹中的 Azure* 文件夹,然后重试安装。

安装完成后,$env:PSModulePath 设置中应会有包含 Azure PowerShell cmdlet 的目录。

Note

从 WebPI 安装时,会发生一个有关 PowerShell $env:PSModulePath 的已知问题。如果计算机因系统更新或其他安装而需要重启,有可能会导致更新 $env:PSModulePath 不包含 Azure PowerShell 的安装路径。如果发生这种情况,当你在安装或升级之后尝试使用 Azure PowerShell cmdlet 时,可能会看到“cmdlet 无法识别”消息。如果发生这种情况,重启计算机应该可以解决该问题。

如果尝试加载或执行 cmdlet,会收到如下消息:

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

这可以通过以下方法更正:重新启动计算机,或者按照以下方式从 C:\Program Files\WindowsPowerShell\Modules\Azure\XXXX\ 导入 cmdlet(其中 XXXX 是所安装 PowerShell 的版本):

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

从 PowerShell 库安装 Azure PowerShell

在提升的 Windows PowerShell 或 PowerShell 集成脚本环境 (ISE) 提示符下,使用以下命令从 PowerShell 库安装 Azure PowerShell 1.3.0 或更高版本:

 
  1. # Install the Azure Resource Manager modules from the PowerShell Gallery
  2. Install-Module AzureRM
  3. # Install the Azure Service Management module from the PowerShell Gallery
  4. Install-Module Azure

有关这些命令的详细信息

  • Install-Module AzureRM 为 Azure Resource Manager cmdlet 安装汇总模块。AzureRM 模块依赖于
  • 每个 Azure Resource Manager 模块的特定版本范围。包含的版本范围可以确保在安装同一主要版本的 AzureRM 模块时,不包含重大的模块更改。安装 AzureRM 模块时,会从 PowerShell 库下载并安装先前未安装的所有 Azure Resource Manager 模块。有关 Azure PowerShell 模块所用的语义版本控制的详细信息,请参阅 semver.org
  • Install-Module Azure 安装 Azure 模块。此模块是 Azure PowerShell 0.9.x 中的服务管理模块。这应该没有任何重要更改,而且可与前一版本的 Azure 模块互换。

步骤 2:启动

你可以通过标准的 Windows PowerShell 控制台或 PowerShell 集成脚本环境 (ISE) 运行 cmdlet。您用于打开控制台的方法取决于您正在运行的 Windows 的版本:

  • 在至少运行 Windows 8 或 Windows Server 2012 的计算机上,您可以使用内置搜索。从“开始”屏幕,开始键入 power。此时将返回范围内的应用列表,包括 Windows PowerShell。若要打开控制台,请单击任一应用程序。(若要将应用固定到“开始”屏幕,请右键单击图标。)

  • 在运行早于 Windows 8 或 Windows Server 2012 的版本的计算机上,请使用“开始”菜单。在“开始”菜单上,依次单击“所有程序”、“附件”、“Windows PowerShell”文件夹和“Windows PowerShell”。

也可以运行 Windows PowerShell ISE,使用菜单项和键盘快捷方式来执行可在 Windows PowerShell 控制台中执行的许多相同任务。若要使用 ISE,请在 Windows PowerShell 控制台、Cmd.exe 或“运行”框中,键入 powershell_ise.exe。

帮助入门的命令

 
  1. # To make sure the Azure PowerShell module is available after you install
  2. Get-Module ListAvailable
  3. # To login to Azure Resource Manager
  4. Login-AzureRmAccount -EnvironmentName AzureChinaCloud
  5. # You can also use a specific Tenant if you would like a faster login experience
  6. # Login-AzureRmAccount -EnvironmentName AzureChinaCloud -TenantId xxxx
  7. # To view all subscriptions for your account
  8. Get-AzureRmSubscription
  9. # To select a default subscription for your current session
  10. Get-AzureRmSubscription SubscriptionName your sub | Select-AzureRmSubscription
  11. # View your current Azure PowerShell session context
  12. # This session state is only applicable to the current session and will not affect other sessions
  13. Get-AzureRmContext
  14. # To select the default storage context for your current session
  15. Set-AzureRmCurrentStorageAccount ResourceGroupName your resource group StorageAccountName your storage account name
  16. # View your current Azure PowerShell session context
  17. # Note: the CurrentStorageAccount is now set in your session context
  18. Get-AzureRmContext
  19. # To list all of the blobs in all of your containers in all of your accounts
  20. Get-AzureRmStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob

步骤 3:连接

cmdlet 需要使用你的订阅来管理你的服务。如果你没有 Azure 订阅,可以购买一个。有关说明,请参阅如何购买 Azure

  1. 键入 Login-AzureRmAccount -EnvironmentName AzureChinaCloud

  2. 键入与你的帐户关联的电子邮件地址和密码。Azure 将对凭据信息进行身份验证并保存,然后关闭该窗口。

--或者--

登录到你的工作帐户或学校帐户:

 
  1. $cred = Get-Credential
  2. Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $cred
Note

如果你的组织帐户有多个关联的租户,请指定 TenantId 参数:

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

查看帐户和订阅详细信息

可以拥有多个帐户和订阅以供 Azure PowerShell 使用。可以通过多次运行 Add-AzureRmAccount -EnvironmentName AzureChinaCloud 来添加多个帐户。

若要显示可用的 Azure 帐户,请键入 Get-AzureAccount。

若要显示 Azure 订阅,请键入 Get-AzureRmSubscription。

获取帮助##

这些资源提供特定 cmdlet 的帮助信息:

  • 从控制台内,可以使用内置的帮助系统。Get-Help cmdlet 提供对此系统的访问。

  • 要获得社区中的帮助信息,请尝试以下常见论坛:

了解详细信息

若要详细了解如何使用 cmdlet,请参阅以下资源:

有关使用 Windows PowerShell 的基本说明,请参阅使用 Windows PowerShell

有关cmdlet的参考信息, 请参阅 Azure 命令行参考.

立即访问http://market.azure.cn

Azure资源管理工具Azure PowerShell介绍的更多相关文章

  1. 跨平台数据库工具Azure Data Studio

    Azure Data Studio是一种跨平台数据库工具,适用于在Windows,MacOS和Linux上使用Microsoft系列内部部署和云数据平台的数据专业人员.Azure Data Studi ...

  2. Azure 基础:使用 powershell 创建虚拟机

    在进行与 azure 相关的自动化过程中,创建虚拟主机是避不开的操作.由于系统本身的复杂性,很难用一两条简单的命令完成虚拟主机的创建.所以专门写一篇文章来记录使用 PowerShell 在 azure ...

  3. 使用 Azure 资源管理器向 Windows VM 应用策略

    通过使用策略,组织可以在整个企业中强制实施各种约定和规则. 强制实施所需行为有助于消除风险,同时为组织的成功做出贡献. 本文介绍如何使用 Azure 资源管理器策略,为组织中的虚拟机定义所需的行为. ...

  4. 【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  

    问题描述 使用PowerShell脚本上传文件至App Service目录的示例 脚本示例 对文件进行上传,使用的 WebClient.UploadFile 方法进行上传.当文件夹中包含子目录,执行以 ...

  5. Azure 基础:使用 powershell 创建虚拟网络

    什么是虚拟网络 虚拟网络是您的网络在 Azure 云上的表示形式.您可以完全控制虚拟网络的 IP 地址.DNS 的设置.安全策略和路由表.您还可以更进一步,把虚拟网络划分为多个子网.然后用它们连接您的 ...

  6. Azure 基础:用 PowerShell 自动发布 CloudServices

    在软件的开发过程中,自动化的编译和部署能够带来很多的优势.下面我们聊聊如何自动发布云应用程序到 azure 上的 cloud services. 打包要发布的内容 首先使用 msbuild 编译 *. ...

  7. 微软推 Azure 机器学习工具:Algorithm Cheat Sheet

    微软推 Azure 机器学习工具:Algorithm Cheat Sheet [日期:2015-05-15] 来源:CSDN  作者:Linux [字体:大 中 小]   Azure Machine ...

  8. Azure Cosmos DB (一) 入门介绍

    一,引言 今天是国庆.中秋双节房价的第三天,今天抽时间分享一篇关于使用Azure 提供的一项NoSql 服务-----Azure Cosmos DB.就有人问了,我听说过 MongoDB.Redis ...

  9. 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例

    当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...

随机推荐

  1. 招聘.Net中高级软件研发工程师

    岗位职责: 1.参与国土不动产系列软件需求分析.系统架构分析与设计: 2.为产品中的复杂功能编写产品开发需求文档: 3.根据设计文档或需求说明完成代码编写.调试.测试和维护: 4.配合上级进行技术决策 ...

  2. Httpclient爬取优酷网

    参考:http://www.cnblogs.com/lchzls/p/6277210.html /httpClient/src/main/java/com/louis/youku/Page.java ...

  3. spark学习之简介

    1.   Spark概述 1.1.  什么是Spark(官网:http://spark.apache.org) Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校A ...

  4. 【Linux学习】Linux用户管理1—用户查询指令、用户切换

    Linux用户管理1-用户查询指令.用户切换 一.用户查询指令 who: 查看当前在线用户情况 -a:显示所有用户的所有信息 -m:显示运行该程序的用户名,和"who am I"的 ...

  5. 3-2带命令行参数的Java

    命令行参数: 主方法Main 小括号里面的内容就是命令参数: String[] args class ArgsDemo{ public static void main(String[] args){ ...

  6. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  7. 升级到Angular6后对老版本的RXJS代码做相应的调整

    还没有了解过RXJS6的童鞋,可以查看我的另外一篇博文,此篇博文主要是对于RXJS5升级到RXJS6的代码调整示例 RXJS5版本 在RXJS5上我们是这样写请求的 import 'rxjs/add/ ...

  8. HDU5918【KMP大法好,虽然我不会】

    #include <bits/stdc++.h> using namespace std; typedef long long LL; const; int n,m; int a[MAX] ...

  9. Unity中资源动态加载的几种方式比较

    http://blog.csdn.net/leonwei/article/details/18406103 初学Unity的过程中,会发现打包发布程序后,unity会自动将场景需要引用到的资源打包到安 ...

  10. 洛谷P2480 [SDOI2010]古代猪文(卢卡斯定理+中国剩余定理)

    传送门 好吧我数学差的好像不是一点半点…… 题目求的是$G^{\sum_{d|n}C^d_n}mod\ 999911659$ 我们可以利用费马小定理$a^{k}\equiv a^{k\ mod\ (p ...