在托管的2017代理(VSTS-Visual Studio团队服务)上升级AzureRM Powershell
我通过Visual Studio团队服务(联机)使用版本管理。我们使用托管构建代理,我真的希望避免管理自定义代理的开销 我确实需要的一项是AzureRM PowerShell模块。高达5.1.1的版本是可用的,但我需要6.0.0 我想做的是在我的发布过程中使用一个步骤(PowerShell)来获得版本6.0.0,并使用thart,但是我不能完全让它工作。我尝试过几种方法,但都没有成功,目前的方法是:在托管的2017代理(VSTS-Visual Studio团队服务)上升级AzureRM Powershell,powershell,azure-devops,azure-powershell,Powershell,Azure Devops,Azure Powershell,我通过Visual Studio团队服务(联机)使用版本管理。我们使用托管构建代理,我真的希望避免管理自定义代理的开销 我确实需要的一项是AzureRM PowerShell模块。高达5.1.1的版本是可用的,但我需要6.0.0 我想做的是在我的发布过程中使用一个步骤(PowerShell)来获得版本6.0.0,并使用thart,但是我不能完全让它工作。我尝试过几种方法,但都没有成功,目前的方法是: Write-Output "------------------ Install package
Write-Output "------------------ Install package provider ------------------"
Find-PackageProvider -Name "NuGet" | Install-PackageProvider -Scope CurrentUser -Force
Write-Output "------------------ Remove Modules ------------------"
Get-Module -ListAvailable | Where-Object {$_.Name -like 'AzureRM*'} | Remove-Module
Write-Output "------------------ Install the AzureRM version we want - 6.0.1! ------------------"
Install-Package AzureRM -RequiredVersion 6.0.1 -Scope CurrentUser -Force
Write-Output "------------------ Import AzureRM 6.0.1 ------------------"
Import-Module AzureRM -RequiredVersion 6.0.1
这一切都可以正常工作(即不会崩溃…),但当我尝试使用6.0.1 cmdlet之一时,会出现错误
Get-AzureRmADGroup:Azure PowerShell会话尚未启动
正确初始化。请导入模块并重试
你知道我哪里出了问题,或者我可以使用其他策略来部署AzureRM 6.0.1并在托管代理上使用它吗?我终于找到了答案——为其他遭受同样问题的人添加了答案 关键是升级AzureRM模块后登录 PowerShell代码:
Write-Output "------------------ Start: Upgrade AzureRM on build host ------------------"
Write-Output "- - - - - Install package provider"
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Write-Output "- - - - - List Modules Before"
Get-Module -ListAvailable| where {$_.Name -Like “*AzureRM*”} | Select Name, Version
Write-Output "- - - - - Remove alll existing AzureRM Modules"
Get-Module -ListAvailable | Where-Object {$_.Name -like '*AzureRM*'} | Remove-Module -Force
Write-Output "- - - - - Install AzureRM 6.0.1"
Install-Module -Name AzureRM -RequiredVersion 6.0.1 -Force -Scope CurrentUser
Write-Output "- - - - - Import AzureRM 6.0.1"
Import-Module AzureRM -Force -Verbose -Scope Local
Write-Output "- - - - - List Modules After"
Get-Module -ListAvailable| where {$_.Name -Like “*AzureRM*”} | Select Name, Version
Write-Output "------------------ End: Upgrade AzureRM on build host ------------------"
Write-Output "------------------ Start: LoginToAzure ------------------"
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$AdminCredential = New-Object System.Management.Automation.PSCredential ($AdminUserEmailAddress, $SecurePassword)
Login-AzureRmAccount -Credential $AdminCredential
Get-AzureRmSubscription –SubscriptionId $SubscriptionId | Select-AzureRmSubscription
Write-Output "------------------ End: LoginToAzure ------------------"
多亏了穆雷在正确方向上的初始点,展示我希望做的事情并非不可能 我最初试图在Azure PowerShell任务中实现这一点,但在AzureRm.Profile中遇到了死胡同 诀窍在于了解AzureRM VSTS任务如何进行依赖项设置,它有效地接受VSTS UI中的“Azure Powershell版本”字符串,并使用它在PSModules环境变量中定义一个额外的搜索路径,即
C:\Modules\AzureRM_5.1.1
在搜索用户配置文件之前,它将查看该目录,然后是全局模块路径
一旦找到模块,它就会执行Azure登录,这将阻碍任何在登录后删除模块的希望
因此,如果您改为使用普通的powershell任务,即AzureRM未加载到的任务(正如Murray得出的结论):
值得注意的是,安装模块不会安装到c:\模块,如
在进行实验时,我似乎需要AllowClobber绕过覆盖旧powershell版本的问题,但我怀疑我不再需要它了
优雅的解决方案在下次使用Azure PowerShell脚本时生效
用6.2.1填充的首选powershell版本字段将向PSModules路径添加C:\Modules\azurerm_6.2.1
。这是不存在的,但谢天谢地,因为PSModules仍然包含用户特定的模块路径,所以我们的6.2.1是自己加载的
幸运的是,5.1.1中的AzureRM.Profile具有足够的转发兼容性,因此Azure Powershell任务执行的服务主体登录仍然有效
运行
Get-Module AzureRm
Get-AzureRmContext
将输出您希望的版本:
值得注意的是,如果无法登录,我认为可以使用System.AccessToken(如果在代理阶段级别打开该选项)
如果解决方案需要调整,请使用诊断技术:
通过阅读任务中加载到AzureRM中的代码,可以获得很大帮助:
以及如何生成VSTS图像:
Enabing System.Debug=true作为环境版本变量。
然后使用
我鼓励任何有兴趣投票的人: 由于撰写本文时的AzureRM版本在VSTS托管的2017代理上已过时
不幸的是,我无法提交PR来升级它,因为它是通过一个私人托管的zip文件拉入的 既然问题已经解决,您可以接受它作为答案。您好,下面我得到
加载扩展类型数据文件时发生以下错误:TypeData“Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContextContainer”中的错误:TypeConverter被忽略,因为它已经发生了。我怀疑是因为AzureRm.Profile的旧版本正在加载,无法卸载,请问您是如何解决的?您还需要执行其他任务设置吗?哦,除非我应该使用普通PowerShell而不是Azure PowerShell任务。。。我刚刚开始思考另一个问题:-)我正在使用带有部署帐户的普通PowerShell。我发现Azure PowerShell限制性太强了。啊哈,谢谢Murray,我刚刚得出了与昨天呼吸新鲜空气相同的结论!:-)你真是太好了,穆雷!离线抓到你:-)你不会相信有多少版本和疯狂的代码达到了这个选项!请注意,对于现在阅读本文的人来说,AzureRM现在已经升级到我试图升级到的版本。但是,如果使用更高版本,这些信息可能仍然有用。
Get-Module AzureRm
Get-AzureRmContext