Skip to content

Commit 5c78b82

Browse files
deadlydogCopilot
andcommitted
fix: Prevent error when using PowerShellGet v3 and StrictMode
Co-authored-by: Copilot <copilot@github.com>
1 parent 3c60fa3 commit 5c78b82

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

ManualTests/AzureArtifactsPowerShellModuleHelper.IntegrationTests.ps1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ Import-Module -Name $moduleFilePathToTest -Force
3232

3333
function Remove-PsRepository([string] $feedUrl)
3434
{
35-
Get-PSRepository | Where-Object { $_.SourceLocation -ieq $feedUrl } | Unregister-PSRepository
36-
Get-PSRepository | Where-Object { $_.SourceLocation -ieq $feedUrl } | Should -BeNullOrEmpty
35+
$repositories = Get-PSRepository
36+
37+
# PowerShellGet v2 uses SourceLocation and v3 uses Uri for the feed URL of Get-PSRepository, so check both.
38+
if ($repositories -and $repositories[0].PSObject.Properties.Name -contains 'SourceLocation')
39+
{
40+
$repositories | Where-Object { $_.SourceLocation -ieq $feedUrl } | Unregister-PSRepository
41+
Get-PSRepository | Where-Object { $_.SourceLocation -ieq $feedUrl } | Should -BeNullOrEmpty
42+
}
43+
else
44+
{
45+
$repositories | Where-Object { $_.Uri -ieq $feedUrl } | Unregister-PSRepository
46+
Get-PSRepository | Where-Object { $_.Uri -ieq $feedUrl } | Should -BeNullOrEmpty
47+
}
3748
}
3849

3950
function Remove-PowerShellModule([string] $powerShellModuleName)

src/AzureArtifactsPowerShellModuleHelper/AzureArtifactsPowerShellModuleHelper.psm1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ function Register-AzureArtifactsPSRepository
9090
{
9191
$psRepositories = Get-PSRepository
9292

93-
# Old versions of PowerShellGet use SourceLocation and newer ones use Uri for the feed URL, so check both.
94-
[PSCustomObject] $existingPsRepositoryForFeed = $psRepositories |
95-
Where-Object { $_.SourceLocation -ieq $feedUrl -or $_.Uri -ieq $feedUrl } |
96-
Select-Object -First 1
93+
# PowerShellGet v2 uses SourceLocation and v3 uses Uri for the feed URL of Get-PSRepository, so check both.
94+
[PSCustomObject] $existingPsRepositoryForFeed = $null
95+
if ($psRepositories -and $psRepositories[0].PSObject.Properties.Name -contains 'SourceLocation') {
96+
$existingPsRepositoryForFeed = $psRepositories |
97+
Where-Object { $_.SourceLocation -ieq $feedUrl } |
98+
Select-Object -First 1
99+
} else {
100+
$existingPsRepositoryForFeed = $psRepositories |
101+
Where-Object { $_.Uri -ieq $feedUrl } |
102+
Select-Object -First 1
103+
}
104+
97105
[bool] $psRepositoryIsAlreadyRegistered = ($null -ne $existingPsRepositoryForFeed)
98106
if ($psRepositoryIsAlreadyRegistered)
99107
{

0 commit comments

Comments
 (0)