Using PowerShellGet on VSTS hosted agents

妖精的绣舞 提交于 2020-01-13 13:49:34

问题


I am using Visual Studio Team Services (VSTS) hosted build agents in my build process. My builds mostly rely on the 'PowerShell' step that calls a script that I have in git. From within this script, I would like to manage PowerShell modules using PowerShellGet. For example, I would like to be able to install pscx simply by running

Install-Module -Name pscx

Unfortunately, hosted agents use PowerShell 4 and they don't have the PowerShellGet module installed. As a result, the Install-Module function is not available.

Anybody has any suggestion to use the PowerShellGet module on VSTS hosted agent? Note that since I don't have admin rights on this machine, I can't install the msi that installs PowerShellGet for PowerShell 4.


回答1:


To be able to use PowerShellGet, two PowerShell modules are required:

  • PowerShellGet
  • PackageManagement

These are available out of the box with PowerShell 5 or through the msi installer available on the PowerShell Gallery.

Instead of deploying these modules through the msi, you can simply add them to your git repository (ex: in a folder named PsModules). You will be able to get a hand on these modules on a machine that has either PS5 or the msi installed. They are usually in the C:\Program Files\WindowsPowerShell\Modules folder.

Then, add the PsModules folders to your PSModulePath environment variable. Starting from there, it is possible to use PowerShellGet as in the following:

$env:PSModulePath = "$env:BUILD_SOURCESDIRECTORY\PsModules;$env:PSModulePath"
Import-Module PowerShellGet
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope currentuser
Save-Module pscx -path "$env:BUILD_SOURCESDIRECTORY\PsModules"
import-module pscx
Write-Host '************************'
Get-Command -module pscx


来源:https://stackoverflow.com/questions/36954237/using-powershellget-on-vsts-hosted-agents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!