Skip to content

Save-PSResource with -WhatIf gives unexpected terminating error #1788

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
Gijsreyn opened this issue Feb 7, 2025 · 11 comments
Closed
3 tasks done

Save-PSResource with -WhatIf gives unexpected terminating error #1788

Gijsreyn opened this issue Feb 7, 2025 · 11 comments

Comments

@Gijsreyn
Copy link

Gijsreyn commented Feb 7, 2025

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

Whenever adding the -WhatIf switch when saving a PowerShell module, the module doesn't give the what if response correctly. Instead, it terminates.

Steps to reproduce:

Run: Save-PSResource -Name WSManDsc -Path C:\temp -WhatIf

Expected behavior

Provide details about what if

Actual behavior

Gives both what if results and terminates.

Error details

Save-PSResource: Package(s) 'WSManDsc' could not be installed from repository 'PSGallery'.

Environment data

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Binary     1.1.0      preview2   Microsoft.PowerShell.PSResourceGet  {Compress-PSResource, Find-PSResource, Get-Instal…


Name                           Value
----                           -----
PSVersion                      7.5.0
PSEdition                      Core
GitCommitId                    7.5.0
OS                             Microsoft Windows 10.0.26100
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 Feb 7, 2025

Should it though? Also, I can't reproduce.

PS > Save-PSResource -Name 'WSManDsc' -Path ([System.Environment]::GetFolderPath('Desktop') + '\Tes') -Debug -Verbose -WhatIf

DEBUG: In SavePSResource::ProcessSaveHelper()
DEBUG: In InstallHelper::BeginInstallPackages()
DEBUG: Parameters passed in >>> Name: 'WSManDsc'; VersionRange: ''; NuGetVersion: ''; VersionType: 'NoVersion'; Version: ''; Prerelease: 'False'; Repository: ''; AcceptLicense: 'False'; Quiet: 'False'; Reinstall: 'True'; TrustRepository: 'False'; NoClobber: 'False'; AsNupkg: 'False'; IncludeXml 'False'; SavePackage 'True'; TemporaryPath ''; SkipDependencyCheck: 'False'; AuthenticodeCheck: 'False'; PathsToInstallPkg: 'C:\Users\olav.birkeland\Desktop\Tes'; 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()?%24filter=Id+eq+%27WSManDsc%27+and+IsLatestVersion+eq+true&%24inlinecount=allpages&id=%27WSManDsc%27'
What if: Performing the operation "Save-PSResource" on target "Package to save: 'WSManDsc', version: '3.2.0'".
VERBOSE: Attempting to delete 'C:\Users\olav.birkeland\AppData\Local\Temp\f1077e38-c7f5-4b28-b0b7-d464dc4ca3ff'
VERBOSE: Successfully deleted 'C:\Users\olav.birkeland\AppData\Local\Temp\f1077e38-c7f5-4b28-b0b7-d464dc4ca3ff'

PS >

@Gijsreyn Gijsreyn changed the title Save-PSResource with -WhatIf does not save the module Save-PSResource with -WhatIf gives unexpected terminating error Feb 7, 2025
@Gijsreyn
Copy link
Author

Gijsreyn commented Feb 7, 2025

@o-l-a-v You're right, I should have payed more attention while writing. I've changed the title and description.

@o-l-a-v
Copy link
Contributor

o-l-a-v commented Feb 7, 2025

Whats the output with -Debug?

@Gijsreyn
Copy link
Author

Gijsreyn commented Feb 7, 2025

DEBUG: In SavePSResource::ProcessSaveHelper()
DEBUG: In InstallHelper::BeginInstallPackages()
DEBUG: Parameters passed in >>> Name: 'wsmandsc'; VersionRange: ''; NuGetVersion: ''; VersionType: 'NoVersion'; Version: ''; Prerelease: 'False'; Repository: 'PSGallery'; AcceptLicense: 'True'; Quiet: 'False'; Reinstall: 'True'; TrustRepository: 'False'; NoClobber: 'False'; AsNupkg: 'False'; IncludeXml 'False'; SavePackage 'True'; TemporaryPath ''; SkipDependencyCheck: 'False'; AuthenticodeCheck: 'False'; PathsToInstallPkg: 'C:\temp'; Scope ''
DEBUG: In InstallHelper::ProcessRepositories()
VERBOSE: Attempting to search for packages in 'PSGallery'
VERBOSE: Checking if untrusted repository should be used

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
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()?%24filter=Id+eq+%27wsmandsc%27+and+IsLatestVersion&%24inlinecount=allpages&id=%27wsmandsc%27'
What if: Performing the operation "Save-PSResource" on target "Package to save: 'WSManDsc', version: '3.2.0'".
What if: Performing the operation "Save-PSResource" on target "Exit ShouldProcess".
VERBOSE: Attempting to delete 'C:\Users\Gijs\AppData\Local\Temp\e49e4f5f-84ce-4171-8bb9-85452d88c426'
VERBOSE: Successfully deleted 'C:\Users\Gijs\AppData\Local\Temp\e49e4f5f-84ce-4171-8bb9-85452d88c426'
Save-PSResource: Package(s) 'wsmandsc' could not be installed from repository 'PSGallery'.

@o-l-a-v
Copy link
Contributor

o-l-a-v commented Feb 7, 2025

Ah. I think this is fixed in v1.1.0 stable, seems you are using v1.1.0-preview2?

@Gijsreyn
Copy link
Author

Gijsreyn commented Feb 7, 2025

I thought v1.1.0 was shipped with v7.5.0? If I have both on there, shouldn't v1.1.0 be the one to overrule? My module loading knowledge is a bit rusty.

@o-l-a-v
Copy link
Contributor

o-l-a-v commented Feb 7, 2025

From the "Environment data" section of the original post it says v1.1.0-preview2.

I've noticed that module loading is a can of worms. PowerShell will search though paths by the order of $env:PSModulePath in process context, and even lazy load (i think it was called that) DLLs while it searches for them.

So in some of my scripts I've implemented a workaround. Something like this:

$PsrgConflictingAssembly = [object](
    [Threading.Thread]::GetDomain().GetAssemblies().Where{
        -not [string]::IsNullOrEmpty($_.'Location') -and
        $_.'Location'.Contains('PSResourceGet') -and
        $_.'ManifestModule'.'Name' -eq 'Microsoft.PowerShell.PSResourceGet.dll'
    }[0]
)
if ([string]::IsNullOrEmpty($PsrgConflictingAssembly.'ManifestModule'.'Name')) {
    [System.Environment]::SetEnvironmentVariable(
        'PSModulePath',
        [System.Environment]::ExpandEnvironmentVariables(
            [System.Environment]::GetEnvironmentVariable('PSModulePath','User')
        ),
        'Process'
    )
    $null = Import-Module -Name 'Microsoft.PowerShell.PSResourceGet'
}
else {
    Import-Module -Name $PsrgConflictingAssembly.'ManifestModule'.'FullyQualifiedName'
}

@Gijsreyn
Copy link
Author

Gijsreyn commented Feb 7, 2025

That's pretty slick @o-l-a-v. Thanks for the response. I'll close this one. Sorry for the duplicate.

@Gijsreyn Gijsreyn closed this as completed Feb 7, 2025
@o-l-a-v
Copy link
Contributor

o-l-a-v commented Feb 7, 2025

No problem. :) One can also load module by full path. Or remove paths from $env:PSModulePath in process context you don't want PowerShell to search through. More learnings in my issue here:

@o-l-a-v
Copy link
Contributor

o-l-a-v commented Feb 8, 2025

@Gijsreyn: Now I finally remember why this happens. I made a easy repro here:

PowerShell/PowerShell#21201 (comment)

It's very annoying indeed. 😑

@Gijsreyn
Copy link
Author

Gijsreyn commented Feb 9, 2025

@Gijsreyn: Now I finally remember why this happens. I made a easy repro here:

PowerShell/PowerShell#21201 (comment)

It's very annoying indeed. 😑

Thanks for the share and providing such detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants