【问题标题】:Upgrade AzureRM Powershell on Hosted 2017 Agent (VSTS - Visual Studio Team Services)升级托管 2017 代理上的 AzureRM Powershell (VSTS - Visual Studio Team Services)
【发布时间】:2018-05-16 15:57:59
【问题描述】:

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

我确实需要一个项目是 AzureRM PowerShell 模块。高达 5.1.1 的版本是 available on the agent 但我需要 6.0.0。

我想做的是在我的发布过程 (PowerShell) 中使用一个步骤来获取版本 6.0.0 并改用 tart,但是我不能让它工作。我尝试了几种方法都失败了,目前的方法是:

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 并在托管代理上使用它的替代策略吗?

【问题讨论】:

    标签: powershell azure-devops azure-powershell


    【解决方案1】:

    感谢默里在正确方向上的初始点,以表明我希望做的事情并非不可能!

    我最初尝试在 Azure PowerShell 任务中执行此操作,并取得了相当大的进展,但在 AzureRm.Profile as you cannot unload an old version 中遇到了死胡同。

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

    它会先查看该目录,然后再搜索用户配置文件,然后是全局模块路径。

    一旦找到该模块,它就会执行 Azure 登录,这将阻碍之后移除该模块的任何希望。

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

    Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
    Install-Module -Name AzureRM -RequiredVersion 6.2.1 -Force -Scope CurrentUser -AllowClobber
    

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

    我似乎需要 AllowClobber 来解决在试验时覆盖旧 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
    

    将输出您希望的版本:

    值得注意的是,如果它无法登录,我认为 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's Log File Highlighter plugin


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

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

    很遗憾,我无法提交 PR 来升级它,因为它是通过私人托管的 zip 文件拉入的。

    【讨论】:

    • 你真好@Murray!让你离线:-)
    • 你不会相信需要多少版本和疯狂的代码才能达到这个选项!
    • 请注意,现在阅读本文的人,AzureRM 现在已经升级到我尝试升级到的版本。但如果升级到更高版本,这些信息可能仍然有用。
    【解决方案2】:

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

    关键是升级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 ------------------"
    

    【讨论】:

    • 嗨,我收到The following error occurred while loading the extended type data file: Error in TypeData "Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContextContainer": The TypeConverter was ignored because it already occurs。我怀疑由于加载了旧版本的 AzureRm.Profile 并且无法卸载它github.com/Azure/azure-powershell/blob/preview/documentation/… 请问您是如何解决这个问题的?您还需要进行其他任务设置吗?
    • 哦,除非我应该使用普通的 PowerShell 而不是 Azure Powershell 任务...我刚刚开始了另一条思路 :-)
    • 我正在使用带有部署帐户的普通 PowerShell。我发现 Azure PowerShell 限制太多。
    • 啊哈,谢谢默里 我刚刚得出了同样的结论,因为我昨天呼吸了一些新鲜空气! :-)
    • 我真的希望 Azure PowerShell 能够工作,以便可以使用服务主体,但最终还是放弃了。现在有了完整的 PowerShell,我可以控制一切并部署我的 ARM 模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多