Skip to content

Save-PSResource script fails #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
wekempf opened this issue Mar 29, 2024 · 2 comments · Fixed by #1614
Closed
3 tasks done

Save-PSResource script fails #1609

wekempf opened this issue Mar 29, 2024 · 2 comments · Fixed by #1614
Labels
Area-Save Issue-Bug Something isn't working

Comments

@wekempf
Copy link

wekempf commented Mar 29, 2024

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues.

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
Copy link
Contributor

o-l-a-v commented Mar 31, 2024

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:

  • else
    {
    string scriptInfoFolderPath = Path.Combine(installPath, "InstalledScriptInfos");
    string scriptXML = pkgInfo.Name + "_InstalledScriptInfo.xml";
    string scriptXmlFilePath = Path.Combine(scriptInfoFolderPath, scriptXML);
    if (!_savePkg)
    {
    // Need to ensure "InstalledScriptInfos directory exists
    if (!Directory.Exists(scriptInfoFolderPath))
    {
    _cmdletPassedIn.WriteVerbose($"Created '{scriptInfoFolderPath}' path for scripts");
    Directory.CreateDirectory(scriptInfoFolderPath);
    }
    // Need to delete old xml files because there can only be 1 per script
    _cmdletPassedIn.WriteVerbose(string.Format("Checking if path '{0}' exists: '{1}'", scriptXmlFilePath, File.Exists(scriptXmlFilePath)));
    if (File.Exists(scriptXmlFilePath))
    {
    _cmdletPassedIn.WriteVerbose("Deleting script metadata XML");
    File.Delete(Path.Combine(scriptInfoFolderPath, scriptXML));
    }
    _cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, "InstalledScriptInfos", scriptXML)));
    Utils.MoveFiles(Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, "InstalledScriptInfos", scriptXML));
    // Need to delete old script file, if that exists
    _cmdletPassedIn.WriteVerbose(string.Format("Checking if path '{0}' exists: ", File.Exists(Path.Combine(finalModuleVersionDir, pkgInfo.Name + PSScriptFileExt))));
    if (File.Exists(Path.Combine(finalModuleVersionDir, pkgInfo.Name + PSScriptFileExt)))
    {
    _cmdletPassedIn.WriteVerbose("Deleting script file");
    File.Delete(Path.Combine(finalModuleVersionDir, pkgInfo.Name + PSScriptFileExt));
    }
    }
    else {
    _cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, scriptXML)));
    Utils.MoveFiles(Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, scriptXML));
    }
    _cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", scriptPath, Path.Combine(finalModuleVersionDir, pkgInfo.Name + PSScriptFileExt)));
    Utils.MoveFiles(scriptPath, Path.Combine(finalModuleVersionDir, pkgInfo.Name + PSScriptFileExt));
    }

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

@SydneyhSmith
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Save Issue-Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants