PowerShell / PowerShellGet

This module provide functions used with PowerShellGet v3 to provide compatibility with scripts expecting PowerShellGet v2
MIT License
49 stars 13 forks source link

Connecting to protected AzureDevops devops artifact feed. #59

Closed sdixon-a23 closed 7 months ago

sdixon-a23 commented 7 months ago

Prerequisites

Steps to reproduce

I'm trying to connect from PowerShell to a protected feed in Azure DevOps - hoping to avoid using PAT. I want users to be able to SSO and connect to feed and pull-down PowerShell modules.

I'm using CredentialProvider.Microsoft.exe to get and save the creds of the user.

Sample code:

Install-Module -Name PsResourceGet -AllowPrerelease -Force
Install-Module -Name Microsoft.PowerShell.PSResourceGet -AllowPrerelease -Force
Install-Module SecretManagement.JustinGrote.CredMan

Import-Module Microsoft.PowerShell.SecretManagement  -Force -Verbose
Import-Module Microsoft.Powershell.PsResourceGet  -Force -Verbose

Register-SecretVault -Name MyOrg -ModuleName SecretManagement.JustinGrote.CredMan

Get-PSResourceRepository

Unregister-PSResourceRepository -Name "MyDevOpsFeed"

$packageLocation = "https://pkgs.dev.azure.com/myorg/myproject/_packaging/myfeed/nuget/v3/index.json"

$vssCredentialProvider = "$env:UserProfile\.nuget\plugins\netfx\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe"

$tokenJson = ConvertFrom-Json (& $vssCredentialProvider -U $packageLocation -F Json)

$pscred = new-object System.Management.Automation.PSCredential -ArgumentList $tokenJson.Username, (ConvertTo-SecureString $tokenJson.Password -AsPlainText -Force)

Get-SecretInfo -Name MyPsFeed -Vault MyOrg | Remove-Secret

Set-Secret -Name MyPsFeed -Vault MyOrg -Secret $pscred

Get-SecretInfo -Name MyPsFeed -Vault MyOrg

$credentialInfo = New-Object Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo("MyOrg", "MyPsFeed")

$params = @{
    Name = 'MyDevOpsFeed'
    Uri  = $packageLocation
     Trusted = $true
     CredentialInfo = $credentialInfo
}

Register-PSResourceRepository @params

Set-PSResourceRepository -Name MyDevOpsFeed -ApiVersion v3

Get-PSResourceRepository

Find-PSResource -Name Hello-World -Repository MyDevOpsFeed

Install-PSResource -Repository "MyDevOpsFeed" -Name Hello-World

The result is 'Response status code does not indicate success: 401 (Unauthorized).' Re-run the command with -Credential

If I change Find-PSResource -Name Hello-World -Repository MyDevOpsFeed to Find-PSResource -Name Setup-Git -Repository MyDevOpsFeed -Credential $credentialInfo I get the error Cannot bind parameter 'Credential'. Cannot convert the value of type "Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo" to type "System.Management.Automation.PSCredential".

If I change Find-PSResource -Name Hello-World -Repository MyDevOpsFeed to Find-PSResource -Name Setup-Git -Repository MyDevOpsFeed -Credential $pscred I get the error

WARNING: Installing dependencies is not currently supported for V3 server protocol repositories. The package will be installed without installing dependencies. Install-PSResource: .\connect.ps1:46:1 Line | 46 | Install-PSResource -Repository "MyDevOpsFeed" -Name Setup-Git -Cred … | ~~~~~~~~~~~~~~~~~ | 'Response status code does not indicate success: 401 (Unauthorized).' Re-run the command with -Credential.

One thing woth noting, in the examples I have read they use a type Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo, I cannot seem to resolve this. I can however find Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo

Expected behavior

Module installed

Actual behavior

Response status code does not indicate success: 401 (Unauthorized).' Re-run the command with -Credential

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

latest

Visuals

No response