在Hosted 2017 Agent上升级AzureRM Powershell(VSTS - Visual Studio Team Services)

Mur*_*oft 11 powershell azure-powershell azure-devops

我通过Visual Studio Teams Services(在线)使用发布管理.我们使用托管构建代理,我真的想避免管理自定义代理的开销.

我需要的一个项目是AzureRM PowerShell模块.代理可以使用高达5.1.1的版本,但我需要6.0.0.

我想要做的是在我的发布过程(PowerShell)中使用一个步骤来获取版本6.0.0并使用thart代替,但是我不能让它工作.我已经尝试了一些方法,这些方法都已经解决,目前的方法是:

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
Run Code Online (Sandbox Code Playgroud)

这一切都很好(即不会崩溃...)但是当我尝试使用其中一个6.0.1 cmdlet时,我收到一个错误.

Get-AzureRmADGroup:Azure PowerShell会话尚未正确初始化.请导入模块,然后重试.

知道我出错的地方或者我可以用来部署AzureRM 6.0.1并在托管代理上使用它的替代策略吗?

Ale*_*ith 7

感谢Murray朝着正确方向的初衷,证明我希望做的事情并非不可能!

最初,我尝试在Azure PowerShell任务中执行此操作,但进展很快,但由于无法卸载旧版本,因此AzureRm.Profile陷入了僵局。

诀窍在于了解AzureRM VSTS任务如何进行依赖项设置,它有效地获取VSTS UI中的“ Azure Powershell版本”字符串,并使用它在PSModules环境变量(即)中定义其他搜索路径C:\Modules\azurerm_5.1.1

在搜索用户配置文件之前,它将查找该目录,然后是全局模块路径。

一旦找到该模块,它将执行Azure登录,这将妨碍以后再删除该模块的任何希望。

因此,如果您改为使用普通的PowerShell任务,即未将AzureRM加载到其中(正如Murray得出的结论):

普通的Powershell任务

Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Install-Module -Name AzureRM -RequiredVersion 6.2.1 -Force -Scope CurrentUser -AllowClobber
Run Code Online (Sandbox Code Playgroud)

值得注意的是,install-module不会像vsts image generation project这样安装在c:\ modules上。

在实验时,我似乎需要AllowClobber来解决覆盖旧Powershell版本的问题,但是我怀疑我不再需要了。

下一次使用Azure PowerShell脚本时,将启动优雅的解决方案。

Azure PowerShell任务

用6.2.1填充的首选powershell版本字段将添加C:\Modules\azurerm_6.2.1到PSModules路径中。这不存在,但是值得庆幸的是,因为PSModules仍然包括用户特定的模块路径,所以我们自己加载了6.2.1!

幸运的是,来自5.1.1的AzureRM.Profile具有足够的前向兼容性,以使Azure Powershell任务执行的服务主体登录仍然有效。

跑步

Get-Module AzureRm 
Get-AzureRmContext
Run Code Online (Sandbox Code Playgroud)

将输出您希望的版本:

VSTS Azure RM Powershell版本

值得注意的是,如果它无法登录,我认为可以使用System.AccessToken(如果在代理阶段级别启用了该选项)。

诊断技术,如果解决方法需要调整:

仔细阅读任务中加载在AzureRM中的代码,这些事情大有帮助:

https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/AzurePowerShellV3/AzurePowerShell.ps1#L80

https://github.com/Microsoft/vsts-tasks/blob/0703b8869041d64db994934bde97de167787ed2e/Tasks/Common/VstsAzureHelpers_/ImportFunctions.ps1

https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/AzurePowerShellV3/Utility.ps1#L18

以及VSTS图像的生成方式:

https://github.com/Microsoft/vsts-image-generation/blob/2f57db26dc30ae0f257a3415d26eaa8eea0febf9/images/win/scripts/Installers/Install-AzureModules.ps1

使System.Debug = true作为环境发布变量。然后使用VSCode的Log File Highlighter插件


我鼓励任何有兴趣的人投票https : //github.com/Microsoft/vsts-image-generation/issues/149

由于在撰写本文时,AzureRM版本在VSTS Hosted 2017代理上已过时。

不幸的是,我无法提交PR对其进行升级,因为它是通过私人托管的zip文件拉入的。


Mur*_*oft 6

我终于想通了-为遭受同样痛苦的其他人添加答案。

关键是在升级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 ------------------"
Run Code Online (Sandbox Code Playgroud)