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

Save-PSResource script fails #1609

Closed wekempf closed 5 months ago

wekempf commented 6 months ago

Prerequisites

Steps to reproduce

Save-PSResource works for modules.

PS> Save-PSResource Az
Save-PSResource: The 'file' scheme is not supported.

Note: That bit about the 'file' scheme not being supported is caused by having a local directory registered as a repository. This is a separate concern and unrelated to the problem here, as I've tested on multiple machines, some without this local repository.

Save-PSResource for scripts fails.

PS> Save-PSResource New-OnPremiseHybridWorker
Save-PSResource: The 'file' scheme is not supported.
Save-PSResource: Unable to successfully install package 'New-OnPremiseHybridWorker': 'Could not find file 'C:\Users\***\AppData\Local\Temp\1\2b1ede54-ff18-4731-bfbb-d48de23466df\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker_InstalledScriptInfo.xml'.'
Save-PSResource: Operation is not valid due to the current state of the object.
Save-PSResource: Package(s) 'New-OnPremiseHybridWorker' could not be installed from registered repositories 'LocalPSGallery, PSGallery'.

The multiple repositories is not the cause. I get the same behavior on other machines with only PSGallery registered. Most of the messages are very poor. In particular "Operation is not valid due to the current state of the object." is developer speak and totally useless to most PowerShell users. The error reporting here should be vastly improved. The first message about the missing XML file does give a slight clue here, however, and the following "works" by some definition.

PS> Save-PSResource New-OnPremiseHybridWorker -IncludeXml
Save-PSResource: The 'file' scheme is not supported.

This saves the script, but also saves the XML metadata file. I'm not sure why you'd want that file, ever? Regardless, this is different behavior from saving a module. While not fully understanding the desired behavior of these cmdlets (I've posted a question in the discussions, but it appears literally no one reads or at least responds there) I would consider this to be a bug. At best it's inconsistent and unintuitive behavior, making it a usability bug, but I suspect this isn't really the expected behavior.

Expected behavior

PS> Save-PSResource New-OnPremiseHybridWorker

The New-OnPremiseHybridWorker.ps1 script should be saved to the current location without the XML metadata file.


### Actual behavior

```console
PS> Save-PSResource New-OnPremiseHybridWorker
Save-PSResource: Unable to successfully install package 'New-OnPremiseHybridWorker': 'Could not find file 'C:\Users\***\AppData\Local\Temp\1\2b1ede54-ff18-4731-bfbb-d48de23466df\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker_InstalledScriptInfo.xml'.'
Save-PSResource: Operation is not valid due to the current state of the object.
Save-PSResource: Package(s) 'New-OnPremiseHybridWorker' could not be installed from registered repositories 'LocalPSGallery, PSGallery'.

### Error details

```console
Exception             :
    Type    : Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceNotFoundException
    Message : Package(s) 'New-OnPremiseHybridWorker' could not be installed from repository 'PSGallery'.
    HResult : -2146233088
TargetObject          : Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource
CategoryInfo          : InvalidData: (Microsoft.PowerShel…lets.SavePSResource:SavePSResource) [Save-PSResource],
ResourceNotFoundException
FullyQualifiedErrorId : InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource
InvocationInfo        :
    MyCommand        : Save-PSResource
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 10
    Line             : save-psresource New-OnPremiseHybridWorker -Repository PSGallery
    Statement        : save-psresource New-OnPremiseHybridWorker -Repository PSGallery
    PositionMessage  : At line:1 char:1
                       + save-psresource New-OnPremiseHybridWorker -Repository PSGallery
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : save-psresource
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Environment data

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

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

Visuals

No response

o-l-a-v commented 6 months ago

Can confirm I see the same behavior. Here with $VerbosePreference = $DebugPreference = 'Continue':

PS C:\Users\olavb> Save-PSResource -Path 'C:\Users\olavb\Desktop\TestSaveScript' -SkipDependencyCheck -Name 'New-OnPremiseHybridWorker' -TrustRepository -Repository 'PSGallery'
DEBUG: In SavePSResource::ProcessSaveHelper()
DEBUG: In InstallHelper::BeginInstallPackages()
DEBUG: Parameters passed in >>> Name: 'New-OnPremiseHybridWorker'; VersionRange: ''; NuGetVersion: ''; VersionType: 'NoVersion'; Version: ''; Prerelease: 'False'; Repository: 'PSGallery'; AcceptLicense: 'True'; Quiet: 'False'; Reinstall: 'True'; TrustRepository: 'True'; NoClobber: 'False'; AsNupkg: 'False'; IncludeXml 'False'; SavePackage 'True'; TemporaryPath ''; SkipDependencyCheck: 'True'; AuthenticodeCheck: 'False'; PathsToInstallPkg: 'C:\Users\olavb\Desktop\TestSaveScript'; Scope ''
DEBUG: In InstallHelper::ProcessRepositories()
VERBOSE: Attempting to search for packages in 'PSGallery'
DEBUG: In InstallHelper::InstallPackages()
DEBUG: In InstallHelper::InstallPackage()
DEBUG: In V2ServerAPICalls::FindName()
DEBUG: In V2ServerAPICalls::HttpRequestCall()
DEBUG: Request url is 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='New-OnPremiseHybridWorker'&$inlinecount=allpages&$filter=IsLatestVersion and Id eq 'New-OnPremiseHybridWorker''
DEBUG: In V2ServerAPICalls::InstallVersion()
DEBUG: In V2ServerAPICalls::HttpRequestCallForContent()
DEBUG: Request url is 'https://www.powershellgallery.com/api/v2/package/New-OnPremiseHybridWorker/1.7.0'
DEBUG: In InstallHelper::TryInstallToTempPath()
DEBUG: In InstallHelper::DeleteExtraneousFiles()
DEBUG: Deleting 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker.nuspec'
DEBUG: Deleting 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\[Content_Types].xml'
DEBUG: Deleting 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\_rels'
DEBUG: Deleting 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\package'
DEBUG: In InstallHelper::TryMoveInstallContent()
DEBUG: In InstallHelper::MoveFilesIntoInstallPath()
VERBOSE: Installation source path is: 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7'
VERBOSE: Installation destination path is: 'C:\Users\olavb\Desktop\TestSaveScript'
VERBOSE: Moving 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker_InstalledScriptInfo.xml' to 'C:\Users\olavb\Desktop\TestSaveScript\New-OnPremiseHybridWorker_InstalledScriptInfo.xml'
Save-PSResource: Unable to successfully install package 'New-OnPremiseHybridWorker': 'Could not find file 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker_InstalledScriptInfo.xml'.'
Save-PSResource: Operation is not valid due to the current state of the object.
VERBOSE: Attempting to delete 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40'
VERBOSE: Successfully deleted 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40'
Save-PSResource: Package(s) 'New-OnPremiseHybridWorker' could not be installed from repository 'PSGallery'.
PS C:\Users\olavb>

Seems the problem is that it tries to move New-OnPremiseHybridWorker_InstalledScriptInfo.xml even though it's not expected to be there unless -IncludeXml.

VERBOSE: Moving 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker_InstalledScriptInfo.xml' to 'C:\Users\olavb\Desktop\TestSaveScript\New-OnPremiseHybridWorker_InstalledScriptInfo.xml'
Save-PSResource: Unable to successfully install package 'New-OnPremiseHybridWorker': 'Could not find file 'C:\Users\olavb\AppData\Local\Temp\64f9062f-dc5e-45bf-89bb-d8e164c25f40\new-onpremisehybridworker\1.7\New-OnPremiseHybridWorker_InstalledScriptInfo.xml'.'

Seems related to the fix for the issue I opened here:

Also related:

Seems the problematic code is here, not checking for IncludeXml:

There's no test for it here either, only with -IncludeXml:

SydneyhSmith commented 6 months ago

Thanks-- looking at the PRs we are able to repro