PowerShell / PowerShellGetv2

PowerShellGet is the Package Manager for PowerShell
https://www.PowerShellGallery.com
MIT License
431 stars 138 forks source link

Update-Module fails when using -Force in authenticated scenario #655

Closed peppekerstens closed 3 years ago

peppekerstens commented 3 years ago

Scenario

I am trying to update several modules from a private repository, maintained within Azure Devops. The method used is exactly as described here

I want to 'force update if exist'. So, by using -Force.

This issue only happens when trying to update from an authenticated source and using -Force. This works fine:

Update-Module -Name posh-git -Repository PSGallery -Force

I have double checked access/validity of PAT key; this is not the issue

Steps to reproduce

C:\temp> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGalleryIton             Trusted              https://pkgs.dev.azure.com/......../_packaging/......./nuget/v2
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2

C:\temp> Get-Module -Name GenericFunctions -ListAvailable

    Directory: C:\Users\peppe\OneDrive\Documenten\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.15.2     GenericFunctions                    {Compare-Hashtable, Compare-ObjectProperties, Convert-Fqdn...
Script     1.11.9     GenericFunctions                    {Compare-Hashtable, Compare-ObjectProperties, Convert-Fqdn...
Script     1.11.8     GenericFunctions                    {Compare-Hashtable, Compare-ObjectProperties, Convert-Fqdn...

C:\temp> [string]$PSGetAccount = 'xxxxxxxxxxx'
C:\temp> [String]$PSGetApiKey = 'xxxxxxxxxxxx'
C:\temp> $SecurePassword = ConvertTo-SecureString $PSGetApiKey -AsPlainText -Force
C:\temp> $PSGetCred = [System.Management.Automation.PSCredential]::new($PSGetAccount, $SecurePassword)
C:\temp> Find-Module -Name GenericFunctions -Repository PSGalleryIton -Credential $PSGetCred

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
1.15.2               GenericFunctions                    PSGalleryIton        Module with generic functions, used in...

Expected behavior

Update-Module -Name GenericFunctions -Credential $PSGetCred -Force
[no output]

Actual behavior

PS 5

C:\temp> Update-Module -Name GenericFunctions -Credential $PSGetCred -Force
PackageManagement\Install-Package : Toegang tot het cloudbestand is geweigerd
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:13069 char:20
+ ...           $sid = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exce
   ption
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand,Mic
   rosoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

$error[0]|select *

writeErrorStream      : True
PSMessageDetails      :
Exception             : System.Exception: Toegang tot het cloudbestand is geweigerd
TargetObject          : Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
CategoryInfo          : NotSpecified: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand,Microsoft.
                        PowerShell.PackageManagement.Cmdlets.InstallPackage
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Update-Module<Process>, C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSMod
                        ule.psm1: line 13069
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

"Toegang tot het cloudbestand is geweigerd" \<translated> "Access denied to cloud file"

PS 7

C:\temp> get-module -name genericfunctions -ListAvailable

    Directory: C:\Users\peppe\OneDrive\Documenten\PowerShell\Modules

oduleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     1.15.2                GenericFunctions                    Desk      {Compare-Hashtable, Compare-ObjectPrope…
Script     1.15.1                GenericFunctions                    Desk      {Compare-Hashtable, Compare-ObjectPrope… 
Script     1.15.0                GenericFunctions                    Desk      {Compare-Hashtable, Compare-ObjectPrope… 
Script     1.11.9                GenericFunctions                    Desk      {Compare-Hashtable, Compare-ObjectPrope… 
Script     1.11.8                GenericFunctions                    Desk      {Compare-Hashtable, Compare-ObjectPrope… 
Script     1.11.5                GenericFunctions                    Desk      {Compare-Hashtable, Compare-ObjectPrope…                                                                                                                        

C:\temp> Update-Module -Name GenericFunctions -Credential $PSGetCred -Force
Install-Package: Administrator rights are required to install or update. Log on to the computer with an account that has Administrator
rights, and then try again, or install by adding "-Scope CurrentUser" to your command. You can also try running the
Windows PowerShell session with elevated rights (Run as Administrator).

$error[0]|select *

C:\temp> $error[0]|select *

PSMessageDetails      :
Exception             : System.Exception: Administrator rights are required to install or update. Log on to the compute
                        r with an account that has Administrator rights, and then try again, or install by adding "-Sco
                        pe CurrentUser" to your command. You can also try running the Windows PowerShell session with e
                        levated rights (Run as Administrator).
TargetObject          : System.Collections.ArrayList
CategoryInfo          : InvalidArgument: (System.Collections.ArrayList:String) [Install-Package], Exception
FullyQualifiedErrorId : AdministratorRightsNeededOrSpecifyCurrentUserScope,Copy-Module,Microsoft.PowerShell.PackageMana
                        gement.Cmdlets.InstallPackage
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Update-Module<Process>, C:\Users\peppe\OneDrive\Documenten\PowerShell\Modules\PowerShellGet\
                        2.2.1\PSModule.psm1: line 12987
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

Environment data

PS 5

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1023
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1023
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
> Get-Module -ListAvailable PowerShellGet,PackageManagement

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.4.7      PackageManagement                   {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...
Binary     1.0.0.1    PackageManagement                   {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}
> Get-PackageProvider -ListAvailable

Name                     Version          DynamicOptions
----                     -------          --------------
msi                      3.0.0.0          AdditionalArguments
msu                      3.0.0.0
nuget                    2.8.5.208
NuGet                    3.0.0.1          Destination, ExcludeVersion, Scope, SkipDependencies, Headers, FilterOnTag...
PowerShellGet            2.2.5.0          PackageManagementProvider, Type, Scope, AllowClobber, SkipPublisherCheck, ...
PowerShellGet            1.0.0.1
Programs                 3.0.0.0          IncludeWindowsInstaller, IncludeSystemComponent

PS 7

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.0-preview.7
PSEdition                      Core
GitCommitId                    7.2.0-preview.7
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> Get-Module -ListAvailable PowerShellGet,PackageManagement

C:\temp> Get-Module -ListAvailable PowerShellGet,PackageManagement

    Directory: C:\Users\peppe\OneDrive\Documenten\PowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     1.4.5                 PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Script     1.4.2                 PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Script     3.0.0      beta10     PowerShellGet                       Desk      {Find-PSResource, Get-PSResourceReposit…
Script     2.2.1                 PowerShellGet                       Desk      {Find-Command, Find-DSCResource, Find-M…

    Directory: C:\program files\windowsapps\microsoft.powershellpreview_7.2.7.0_x64__8wekyb3d8bbwe\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     1.4.7                 PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Script     2.2.5                 PowerShellGet                       Desk      {Find-Command, Find-DSCResource, Find-M…

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     1.4.7                 PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Binary     1.0.0.1               PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Script     2.2.5                 PowerShellGet                       Desk      {Find-Command, Find-DSCResource, Find-M…
Script     1.0.0.1               PowerShellGet                       Desk      {Install-Module, Find-Module, Save-Modu…
> Get-PackageProvider -ListAvailable

Name                     Version          DynamicOptions
----                     -------          --------------
NuGet                    3.0.0.1          Destination, ExcludeVersion, Scope, SkipDependencies, Headers, FilterOnTag, …
PowerShellGet            2.2.5.0          PackageManagementProvider, Type, Scope, AllowClobber, SkipPublisherCheck, In…
PowerShellGet            2.2.1.0
PowerShellGet            1.0.0.1
peppekerstens commented 3 years ago

Found likely cause; I installed previous versions of the modules with install-package instead of install-module. PowerShell modules do not get registered correctly by install-package

PowerShell already provides some hints;

C:\temp> Update-Module -Name GenericFunctions -Credential $PSGetCred
Update-Module: Module 'GenericFunctions' was not installed by using Install-Module, so it cannot be updated.

When I delete a previous version of the module and re-install that version, using install-module, everything works as expected;

C:\temp> Install-Module -Name GenericFunctions -Credential $PSGetCred -Scope CurrentUser -Repository psgalleryiton -Force -RequiredVersion '1.15.1'
C:\temp> Update-Module -Name GenericFunctions -Credential $PSGetCred -force
[no error/no output/updated module to latest]
C:\temp> Update-Module -Name GenericFunctions -Credential $PSGetCred -force
[no error/no output/same latest module]

So this is a very specific use-case. Don't bother...