PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
489 stars 94 forks source link

Install-PSResource can't install module from private feed #1422

Closed thisjustin816 closed 1 year ago

thisjustin816 commented 1 year ago

Prerequisites

Steps to reproduce

I have a private PowerShell module feed setup on Azure DevOps that I've been using for years with both legacy PowerShellGet and the beta versions before 3.0.22. When trying to using this new module with my existing *-PSResource functions I'm getting a 404 error when trying to install a module even though Find-PSResource can find it. Here's the commands being called:

$moduleName = 'PSSiOps'
$repository = 'PSSiOpsFeed'
Write-Progress -Activity "Installing $moduleName..."
$psCredential = New-Object `
    -TypeName 'System.Management.Automation.PSCredential' `
    -ArgumentList (
        $env:USERNAME,
        (
            ConvertTo-SecureString `
                -String $env:System_AccessToken `
                -AsPlainText `
                -Force
        )
    )
$findParams = @{
    Name       = $moduleName
    Repository = $repository
    Credential = $psCredential
}
Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue
$moduleToInstall = Find-PSResource @findParams
$moduleToInstall | Install-PSResource `
    -Scope CurrentUser `
    -Credential $psCredential `
    -Reinstall `
    -PassThru

The PSResource repository is registered properly because $moduleToInstall has the expected value.

Expected behavior

$moduleToInstall is installed. In my current testing it should be:

Name    Version Prerelease Repository  Description
----    ------- ---------- ----------  -----------
PSSiOps 10.1.12            PSSiOpsFeed A PowerShell module

Actual behavior

Install-PSResource : 'Response status code does not indicate success: 404 (Not Found).' Request sent: 
'https://pkgs.dev.azure.com/NuvaDev/_packaging/PSSiOpsFeed/nuget/v2/package/PSSiOps/10.1.12'
At line:1 char:20
+ $moduleToInstall | Install-PSResource `
+                    ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-PSResource], ResourceNotFoundException
    + FullyQualifiedErrorId : Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceNotFoundException,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource

Error details

Exception             :
    Type       : Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceNotFoundException
    TargetSite :
        Name          : MoveNext
        DeclaringType :
Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls+<SendV2RequestForContentAsync>d__35,
Microsoft.PowerShell.PSResourceGet, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null
        MemberType    : Method
        Module        : Microsoft.PowerShell.PSResourceGet.dll
    Message    : 'Response status code does not indicate success: 404 (Not Found).' Request sent:
'https://pkgs.dev.azure.com/NuvaDev/_packaging/PSSiOpsFeed/nuget/v2/package/PSSiOps/10.1.12'
    Source     : Microsoft.PowerShell.PSResourceGet
    HResult    : -2146233088
    StackTrace :
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.SendV2RequestForContentAsync(HttpRequestMessage
message, HttpClient s_client)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.HttpRequestCallForContent(String requestUrlV2,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.InstallVersion(String packageName, String version,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.InstallPackage(VersionType searchVersionType,
NuGetVersion specificVersion, VersionRange versionRange, String pkgNameToInstall, PSRepositoryInfo repository,
ServerApiCall currentServer, ResponseUtil currentResponseUtil, String tempInstallPath, Hashtable packagesHash,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.InstallPackages(String[] pkgNamesToInstall,
PSRepositoryInfo repository, ServerApiCall currentServer, ResponseUtil currentResponseUtil, ScopeType scope, Boolean
skipDependencyCheck, FindHelper findHelper)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.ProcessRepositories(String[] repository, Boolean
trustRepository, Boolean skipDependencyCheck, ScopeType scope)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.BeginInstallPackages(String[] names, VersionRange
versionRange, NuGetVersion nugetVersion, VersionType versionType, String versionString, Boolean prerelease, String[]
repository, Boolean acceptLicense, Boolean quiet, Boolean reinstall, Boolean force, Boolean trustRepository, Boolean
noClobber, Boolean asNupkg, Boolean includeXml, Boolean skipDependencyCheck, Boolean authenticodeCheck, Boolean
savePkg, List`1 pathsToInstallPkg, Nullable`1 scope, String tmpPath, HashSet`1 pkgsInstalled)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource.ProcessInstallHelper(String[] pkgNames, String
pkgVersion, Boolean pkgPrerelease, String[] pkgRepository, PSCredential pkgCredential, InstallPkgParams
reqResourceParams)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Install-PSResource], ResourceNotFoundException
FullyQualifiedErrorId : Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceNotFoundException,Microsoft.PowerShell.P
SResourceGet.Cmdlets.InstallPSResource
InvocationInfo        :
    MyCommand        : Install-PSResource
    ScriptLineNumber : 1
    OffsetInLine     : 20
    HistoryId        : 14
    Line             : $moduleToInstall | Install-PSResource `

    PositionMessage  : At line:1 char:20
                       + $moduleToInstall | Install-PSResource `
                       +                    ~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Install-PSResource
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Binary     0.9.0      rc1        Microsoft.PowerShell.PSResourceGet  {Find-PSResource, Get-InstalledPSResource, Get-PS…

Key   : PSVersion
Value : 7.3.7
Name  : PSVersion

Key   : PSEdition
Value : Core
Name  : PSEdition

Key   : GitCommitId
Value : 7.3.7
Name  : GitCommitId

Key   : OS
Value : Microsoft Windows 10.0.23550
Name  : OS

Key   : Platform
Value : Win32NT
Name  : Platform

Key   : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0…}
Name  : PSCompatibleVersions

Key   : PSRemotingProtocolVersion
Value : 2.3
Name  : PSRemotingProtocolVersion

Key   : SerializationVersion
Value : 1.1.0.1
Name  : SerializationVersion

Key   : WSManStackVersion
Value : 3.0
Name  : WSManStackVersion

Visuals

No response

thisjustin816 commented 1 year ago

I'm using a PAT. This part of the code I posted is passed to the -Credential parameter:

$psCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList ( $env:USERNAME, ( ConvertTo-SecureString -String $env:System_AccessToken -AsPlainText ` -Force ) )

-- Justin Beeson (He/Him) DevOps Engineer @. @.> https://github.com/thisjustin816 https://github.com/thisjustin816 https://www.linkedin.com/in/justinbeeson/ https://www.linkedin.com/in/justinbeeson/

Sent with Shortwave https://www.shortwave.com?utm_medium=email&utm_content=signature&utm_source=amJlZXNvbjgxNkBnbWFpbC5jb20=

On Wed Sep 27, 2023, 10:38 PM GMT, alerickson @.***> wrote:

@thisjustin816 https://github.com/thisjustin816 Thanks for reporting this issue. I'm trying to repro with my own private ADO feed and I noticed that when I use my username + my password as credentials I get a 401, but if I use my username with a PAT it connects successfully. Are you experiencing the same thing? If you haven't tested using a PAT as the password could you try that and let me know if that's successful or not? — Reply to this email directly, view it on GitHub https://github.com/PowerShell/PSResourceGet/issues/1422#issuecomment-1738199449, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJZZKT4ZUZ33SLSZSHWUOQDX4STH3ANCNFSM6AAAAAA5IFA4ZY. You are receiving this because you were mentioned.Message ID: @.***>

thisjustin816 commented 1 year ago

And the credential works with Find-PSResource

-- Justin Beeson (He/Him) DevOps Engineer @. @.> https://github.com/thisjustin816 https://github.com/thisjustin816 https://www.linkedin.com/in/justinbeeson/ https://www.linkedin.com/in/justinbeeson/

Sent with Shortwave https://www.shortwave.com?utm_medium=email&utm_content=signature&utm_source=amJlZXNvbjgxNkBnbWFpbC5jb20=

On Wed Sep 27, 2023, 10:46 PM GMT, Justin Beeson @.***> wrote:

I'm using a PAT. This part of the code I posted is passed to the -Credential parameter:

$psCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList ( $env:USERNAME, ( ConvertTo-SecureString -String $env:System_AccessToken -AsPlainText ` -Force ) )

-- Justin Beeson (He/Him) DevOps Engineer @. @.> https://github.com/thisjustin816 https://github.com/thisjustin816 https://www.linkedin.com/in/justinbeeson/ https://www.linkedin.com/in/justinbeeson/

Sent with Shortwave https://www.shortwave.com?utm_medium=email&utm_content=signature&utm_source=amJlZXNvbjgxNkBnbWFpbC5jb20=

On Wed Sep 27, 2023, 10:38 PM GMT, alerickson @.***> wrote:

@thisjustin816 https://github.com/thisjustin816 Thanks for reporting this issue. I'm trying to repro with my own private ADO feed and I noticed that when I use my username + my password as credentials I get a 401, but if I use my username with a PAT it connects successfully. Are you experiencing the same thing? If you haven't tested using a PAT as the password could you try that and let me know if that's successful or not? — Reply to this email directly, view it on GitHub https://github.com/PowerShell/PSResourceGet/issues/1422#issuecomment-1738199449, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJZZKT4ZUZ33SLSZSHWUOQDX4STH3ANCNFSM6AAAAAA5IFA4ZY. You are receiving this because you were mentioned.Message ID: @.***>

alerickson commented 1 year ago

@thisjustin816 so sorry, I was looking at a couple different issues, got them mixed up (was hoping I deleted my comment before you got to it). This looks like it may be a piping issue (it's reproing for me when I follow your steps but I can successfully install if I'm not piping into Install). Can you try using Install-PSResource with the module name and see if that works so we can narrow down the scope?

thisjustin816 commented 1 year ago

@thisjustin816 so sorry, I was looking at a couple different issues, got them mixed up (was hoping I deleted my comment before you got to it). This looks like it may be a piping issue (it's reproing for me when I follow your steps but I can successfully install if I'm not piping into Install). Can you try using Install-PSResource with the module name and see if that works so we can narrow down the scope?

I get an object reference error if I don't use the pipeline

❯ Install-PSResource @findParams -Scope CurrentUser -Credential $psCredential -Reinstall -PassThru
Install-PSResource: Object reference not set to an instance of an object.
❯ Get-Error

Exception             :
    Type       : System.NullReferenceException
    TargetSite :
        Name          : HttpRequestCallForContent
        DeclaringType : Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls,
Microsoft.PowerShell.PSResourceGet, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null
        MemberType    : Method
        Module        : Microsoft.PowerShell.PSResourceGet.dll
    Message    : Object reference not set to an instance of an object.
    Source     : Microsoft.PowerShell.PSResourceGet
    HResult    : -2147467261
    StackTrace :
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.HttpRequestCallForContent(String requestUrlV2,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.InstallName(String packageName, Boolean
includePrerelease, ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.InstallPackage(VersionType searchVersionType,
NuGetVersion specificVersion, VersionRange versionRange, String pkgNameToInstall, PSRepositoryInfo repository,
ServerApiCall currentServer, ResponseUtil currentResponseUtil, String tempInstallPath, Hashtable packagesHash,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.InstallPackages(String[] pkgNamesToInstall,
PSRepositoryInfo repository, ServerApiCall currentServer, ResponseUtil currentResponseUtil, ScopeType scope, Boolean
skipDependencyCheck, FindHelper findHelper)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.ProcessRepositories(String[] repository, Boolean
trustRepository, Boolean skipDependencyCheck, ScopeType scope)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.BeginInstallPackages(String[] names, VersionRange
versionRange, NuGetVersion nugetVersion, VersionType versionType, String versionString, Boolean prerelease, String[]
repository, Boolean acceptLicense, Boolean quiet, Boolean reinstall, Boolean force, Boolean trustRepository, Boolean
noClobber, Boolean asNupkg, Boolean includeXml, Boolean skipDependencyCheck, Boolean authenticodeCheck, Boolean
savePkg, List`1 pathsToInstallPkg, Nullable`1 scope, String tmpPath, HashSet`1 pkgsInstalled)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource.ProcessInstallHelper(String[] pkgNames, String
pkgVersion, Boolean pkgPrerelease, String[] pkgRepository, PSCredential pkgCredential, InstallPkgParams
reqResourceParams)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Install-PSResource], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource
InvocationInfo        :
    MyCommand        : Install-PSResource
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 16
    Line             : Install-PSResource @findParams -Scope CurrentUser -Credential $psCredential -Reinstall -PassThru
    PositionMessage  : At line:1 char:1
                       + Install-PSResource @findParams -Scope CurrentUser -Credential $psCred …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Install-PSResource
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
alerickson commented 1 year ago

@thisjustin816 does it work if you run Install-PSResource -Name $moduleName -Repository $repository -Credential $psCredential ?

thisjustin816 commented 1 year ago

Install-PSResource -Name $moduleName -Repository $repository -Credential $psCredential

Same results

❯ $moduleToInstall | Install-PSResource `
>    -Scope CurrentUser `
>    -Credential $psCredential `
>    -Reinstall `
>    -PassThru
Install-PSResource: 'Response status code does not indicate success: 404 (Not Found).' Request sent: 'https://pkgs.dev.azure.com/NuvaDev/_packaging/PSSiOPsFeed/nuget/v2/package/PSSiOps/10.1.16'
❯ Install-PSResource -Name $moduleName -Repository $repository -Credential $psCredential
Install-PSResource: Object reference not set to an instance of an object.
❯ get-error

Exception             :
    Type       : System.NullReferenceException
    TargetSite :
        Name          : HttpRequestCallForContent
        DeclaringType : Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls,
Microsoft.PowerShell.PSResourceGet, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null
        MemberType    : Method
        Module        : Microsoft.PowerShell.PSResourceGet.dll
    Message    : Object reference not set to an instance of an object.
    Source     : Microsoft.PowerShell.PSResourceGet
    HResult    : -2147467261
    StackTrace :
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.HttpRequestCallForContent(String requestUrlV2,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.InstallName(String packageName, Boolean
includePrerelease, ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.InstallPackage(VersionType searchVersionType,
NuGetVersion specificVersion, VersionRange versionRange, String pkgNameToInstall, PSRepositoryInfo repository,
ServerApiCall currentServer, ResponseUtil currentResponseUtil, String tempInstallPath, Hashtable packagesHash,
ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.InstallPackages(String[] pkgNamesToInstall,
PSRepositoryInfo repository, ServerApiCall currentServer, ResponseUtil currentResponseUtil, ScopeType scope, Boolean
skipDependencyCheck, FindHelper findHelper)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.ProcessRepositories(String[] repository, Boolean
trustRepository, Boolean skipDependencyCheck, ScopeType scope)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.BeginInstallPackages(String[] names, VersionRange
versionRange, NuGetVersion nugetVersion, VersionType versionType, String versionString, Boolean prerelease, String[]
repository, Boolean acceptLicense, Boolean quiet, Boolean reinstall, Boolean force, Boolean trustRepository, Boolean
noClobber, Boolean asNupkg, Boolean includeXml, Boolean skipDependencyCheck, Boolean authenticodeCheck, Boolean
savePkg, List`1 pathsToInstallPkg, Nullable`1 scope, String tmpPath, HashSet`1 pkgsInstalled)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource.ProcessInstallHelper(String[] pkgNames, String
pkgVersion, Boolean pkgPrerelease, String[] pkgRepository, PSCredential pkgCredential, InstallPkgParams
reqResourceParams)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Install-PSResource], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource
InvocationInfo        :
    MyCommand        : Install-PSResource
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 9
    Line             : Install-PSResource -Name $moduleName -Repository $repository -Credential $psCredential
    PositionMessage  : At line:1 char:1
                       + Install-PSResource -Name $moduleName -Repository $repository -Credent …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Install-PSResource
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1