PowerShell / PSResourceGet

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

Update-PSResource Az failure #1642

Open Liturgist opened 7 months ago

Liturgist commented 7 months ago

Prerequisites

Steps to reproduce

I am unable to update the Az module.

Expected behavior

Az module would be updated.

Actual behavior

PS C:\> Update-PSResource Az -Repository PSGallery

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its Trusted value by running the Set-PSResourceRepository cmdlet. Are
 you sure you want to install the PSResource from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
Update-PSResource: 'An error occurred while sending the request.' Request sent: 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='Az.Advisor'&$orderby=NormalizedVersion%20desc&$inlinecount=allpages&$skip=0&$filter=Id%20eq%20'Az.Advisor'%20and%20NormalizedVersion%20ge%20'2.0.0'%20and%20NormalizedVersion%20le%20'2.0.9'' Inner exception: 'Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..'
Update-PSResource: Object reference not set to an instance of an object.

Error details

PS C:\> Get-Error

Exception             :
    Type       : System.NullReferenceException
    TargetSite :
        Name          : InstallVersion
        DeclaringType : Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls, Microsoft.PowerShell.PSResourceGet, Version=1.0.1.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.InstallVersion(String packageName, String version, ErrorRecord& errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.InstallPackage(String packageName, String packageVersion, Boolean includePrerelease, ErrorRecord&
errRecord)
   at Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallHelper.BeginPackageInstall(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.UpdatePSResource.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Update-PSResource], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.PSResourceGet.Cmdlets.UpdatePSResource
InvocationInfo        :
    MyCommand        : Update-PSResource
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 83
    Line             : Update-PSResource Az -Repository PSGallery
    Statement        : Update-PSResource Az -Repository PSGallery
    PositionMessage  : At line:1 char:1
                       + Update-PSResource Az -Repository PSGallery
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Update-PSResource
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

PS C:\> $PSVersionTable

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

Version

Script 2.2.5 PowerShellGet Desk

Visuals

No response

anamnavi commented 5 months ago

@Liturgist can you repro this with the latest version of Microsoft.PowerShell.PSResourceGet? And if so, can you share the output of gmo "Microsoft.PowerShell.PSResourceGet"? Thanks!

o-l-a-v commented 1 week ago

Can't repro. It's very slow, but it works. PSResourceGet v1.0.5.

# Clean up destination
[System.IO.Directory]::GetDirectories(
    ('{0}\PowerShell\Modules' -f [System.Environment]::GetFolderPath('MyDocuments'))
).Where{
    $_.Split([System.IO.Path]::DirectorySeparatorChar)[-1] -eq 'Az' -or
    $_.Split([System.IO.Path]::DirectorySeparatorChar)[-1].StartsWith('Az.','OrdinalIgnoreCase')
}.ForEach{
    [System.IO.Directory]::Delete($_,$true)
}

# Install older version first
Install-PSResource -Repository 'PSGallery' -TrustRepository -Name 'Az' -Version '11.6.0' -Scope 'CurrentUser' -Verbose

# Update
Update-PSResource -Repository 'PSGallery' -TrustRepository -Name 'Az' -Scope 'CurrentUser' -Verbose

Close issue?

Liturgist commented 1 week ago

@anamnavi, Az.* directories are in 'C:\Users\$Env:USERNAME\OneDrive\Documents\PowerShell\Modules'. Different failure message now.

PS C:\src> Update-PSResource Az -Repository PSGallery

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its Trusted value by running the Set-PSResourceRepository cmdlet.
Are you sure you want to install the PSresource from 'PSGallery' ?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
Update-PSResource: Module 'Az' was not updated because no valid module was found in the module directory.Verify that the module is located in the folder specified by $env: PSModulePath.
PS C:\src> $PSVersionTable.PSVersion.ToString()
7.4.5
PS C:\src> gmo "Microsoft.PowerShell.PSResourceGet"
PS C:\src>
o-l-a-v commented 1 week ago

@Liturgist Run it again with -Debug?

Update-PSResource -Repository 'PSGallery' -TrustRepository -Name 'Az' -Debug

What version of Az do you currently have installed?

Get-Module -ListAvailable -Name 'Az'

# PSResourceGet must be able to detect it too
Get-InstalledPSResource -Name 'Az' -Scope 'CurrentUser'

What version of Microsoft.PowerShell.PSResourceGet did you use?

Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -PassThru

Microsoft.PowerShell.PSResourceGet currently does not care about / read the $env:PSModulePath variable, instead it's hard coded to use MyDocuments for user context:

Unless you have folder redirection / known folder move enabled in OneDrive, PSResourceGet might not check that OneDrive folder. So what does this return for you?

[System.Environment]::GetFolderPath('MyDocuments')