我正在通过Visual Studio Teams Services(在线)使用发布管理。我们使用托管构建代理,我真的想避免管理自定义代理的开销。
我需要的一个项目是AzureRM PowerShell模块。最高5.1.1的版本是available on the agent但我需要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
这一切正常(即不会崩溃......)但是当我尝试使用其中一个6.0.1 cmdlet时,我收到错误。
Get-AzureRmADGroup:尚未使用Azure PowerShell会话 正确初始化。请导入模块,然后重试。
我知道我哪里出错或可以使用其他策略来部署AzureRM 6.0.1并在托管代理上使用它吗?
答案 0 :(得分:6)
感谢Murray在正确的方向上的初始点,展示我希望做的事情是不可能的!
我最初尝试在Azure PowerShell任务中执行此操作并且相当远,但使用AzureRm.Profile as you cannot unload an old version达到了死胡同。
诀窍是要了解AzureRM VSTS任务如何进行依赖设置,它实际上需要" Azure Powershell版本" VSTS UI中的字符串,并使用它在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
值得注意的是,安装模块无法安装到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/master/Tasks/AzurePowerShellV3/Utility.ps1#L18
以及如何生成VSTS图像:
Enabing System.Debug = true作为环境释放变量。 然后使用VSCode's Log File Highlighter plugin
我鼓励任何有兴趣的人投票:https://github.com/Microsoft/vsts-image-generation/issues/149
由于撰写本文时AzureRM版本在VSTS Hosted 2017代理商上已经过时了。
很遗憾,我无法提交PR进行升级,因为它是通过私下托管的zip文件提取的。
答案 1 :(得分:5)
我终于明白了 - 为遭受同样困扰的其他人添加答案。
关键是在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 ------------------"