|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
| 4 | +# cspell:ignore accesstoken |
| 5 | + |
4 | 6 | function Load-Module { |
5 | 7 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseApprovedVerbs', '', Justification = 'Prefer verb usage')] |
| 8 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'The token is provided at runtime via the pipeline SYSTEM_ACCESSTOKEN environment variable, not a hardcoded secret. A PSCredential is required to authenticate Install-Module against the Azure Artifacts feed.')] |
6 | 9 | [CmdletBinding()] |
7 | 10 | [OutputType([bool])] |
8 | 11 | param ( |
@@ -39,15 +42,40 @@ function Load-Module { |
39 | 42 | } |
40 | 43 |
|
41 | 44 | $params = @{ |
42 | | - Name = $Name |
| 45 | + Name = $Name |
| 46 | + Force = $true |
43 | 47 | } |
44 | 48 |
|
45 | 49 | if (-not [string]::IsNullOrEmpty($MinimumVersion)) { |
46 | 50 | $params.MinimumVersion = $MinimumVersion |
47 | 51 | } |
48 | 52 |
|
| 53 | + # Under Network Isolation the public PowerShell Gallery is blocked, so install |
| 54 | + # modules from the internal Azure Artifacts feed. These environment variables are |
| 55 | + # only set by the pipeline; local development continues to use the PowerShell Gallery. |
| 56 | + if (-not [string]::IsNullOrEmpty($env:CFS_FEED_SOURCE) -and |
| 57 | + -not [string]::IsNullOrEmpty($env:SYSTEM_ACCESSTOKEN)) { |
| 58 | + $repositoryName = "CssExchangeCfs" |
| 59 | + $secureToken = ConvertTo-SecureString $env:SYSTEM_ACCESSTOKEN -AsPlainText -Force |
| 60 | + $credential = [PSCredential]::new("azdo", $secureToken) |
| 61 | + |
| 62 | + # Register the feed if it is missing, otherwise normalize it in case a reused |
| 63 | + # agent left it pointing at a different location or not trusted. Install-Module |
| 64 | + # is pinned to this repository below, so the public PowerShell Gallery does not |
| 65 | + # need to be unregistered. |
| 66 | + $existingRepository = Get-PSRepository -Name $repositoryName -ErrorAction SilentlyContinue |
| 67 | + if ($null -eq $existingRepository) { |
| 68 | + Register-PSRepository -Name $repositoryName -SourceLocation $env:CFS_FEED_SOURCE -InstallationPolicy Trusted -Credential $credential |
| 69 | + } elseif ($existingRepository.SourceLocation -ne $env:CFS_FEED_SOURCE -or $existingRepository.InstallationPolicy -ne "Trusted") { |
| 70 | + Set-PSRepository -Name $repositoryName -SourceLocation $env:CFS_FEED_SOURCE -InstallationPolicy Trusted -Credential $credential |
| 71 | + } |
| 72 | + |
| 73 | + $params.Repository = $repositoryName |
| 74 | + $params.Credential = $credential |
| 75 | + } |
| 76 | + |
49 | 77 | $errorCount = $Error.Count |
50 | | - Install-Module @params -Force |
| 78 | + Install-Module @params |
51 | 79 | if ($Error.Count -gt $errorCount) { |
52 | 80 | return $false |
53 | 81 | } |
|
0 commit comments