azure-devopsazure-pipelinesazure-pipelines-build-taskpowershellget

Using PowerShellGet on VSTS hosted agents


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.


Solution

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

    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