PowershellFrameworkCollective / psframework

A module that provides tools for other modules and scripts
MIT License
435 stars 41 forks source link

:bug: ParameterBindingArgumentTransformationException for Invoke-PSMDTemplate > [PSFramework.Serialization.ClixmlSerializer]::FromByteCompressed #645

Closed FH-Inway closed 1 month ago

FH-Inway commented 1 month ago

Invoke-PSMDTemplate started failing with the following error on Windows Server 2022:

WARNING: [14:05:22][Invoke-PSMDTemplate] Failed to: Creating file/project from template CommandTest 1.0.0.0 | Cannot process argument transformation on parameter 'Template'. Cannot convert value "CommandTest 1.0.0.0" to type "PSModuleDevelopment.Template.TemplateInfo". Error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."

This happens with versions 1.12.345 and 1.12.346 of PSFramework. Version 1.11.343 works. Example failing run with version 1.12.346: https://github.com/d365collaborative/d365fo.tools/actions/runs/11161820250/job/31030721873#step:7:22

This looks similar to https://github.com/PowershellFrameworkCollective/PSModuleDevelopment/issues/202, but not sure if it is the same root cause.

Call stack I have been able to piece together:

Content of $error:

Invoke-Template : Cannot process argument transformation on parameter 'Template'. Cannot convert value "CommandTest 
1.0.0.0" to type "PSModuleDevelopment.Template.TemplateInfo". Error: "The magic number in GZip header is not
correct. Make sure you are passing in a GZip stream."
At C:\Program Files\WindowsPowerShell\Modules\PSModuleDevelopment\2.2.11.168\PSModuleDevelopment.psm1:5630 char:44    
+                 Invoke-Template @invokeParam -Template $item
+                                                        ~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Template], ParameterBindingArgumentTransformationException     
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Invoke-Template

Exception calling "FromByteCompressed" with "1" argument(s): "The magic number in GZip header is not correct. Make 
sure you are passing in a GZip stream."
At C:\Program Files\WindowsPowerShell\Modules\psframework\1.12.345\PSFramework.psm1:11779 char:10
+ ...       try { [PSFramework.Serialization.ClixmlSerializer]::FromByteCom ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidDataException

More detailed error messages from Get-PSFMessage -Errors:

Error message 1

Message               : Failed to: Creating file/project from template CommandTest 1.0.0.0 | Cannot process argument  
                        transformation on parameter 'Template'. Cannot convert value "CommandTest 1.0.0.0" to type    
                        "PSModuleDevelopment.Template.TemplateInfo". Error: "The magic number in GZip header is not   
                        correct. Make sure you are passing in a GZip stream."
Source                : System.Management.Automation
StackTrace            :    at
                        System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext       
                        funcContext, Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame   
                        frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interprete
                        dFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interprete
                        dFrame frame)
                           at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)        
                           at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
                           at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke   
                        clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1
                        variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object    
                        input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)      
                           at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()   
                           at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithTh
                        readCheck(Action action)
                           at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope,
                        ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object
                        scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean
                        propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine,        
                        Object[] args)
                           at System.Management.Automation.ScriptBlock.DoInvokeReturnAsIs(Boolean useLocalScope,      
                        ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object
                        scriptThis, Object[] args)
                           at System.Management.Automation.CommandInvocationIntrinsics.InvokeScript(ScriptBlock sb,   
                        Boolean useNewScope, PipelineResultTypes writeToPipeline, IList input, Object[] args)
                           at System.Management.Automation.CommandInvocationIntrinsics.InvokeScript(Boolean
                        useLocalScope, ScriptBlock scriptBlock, IList input, Object[] args)
                           at PSFramework.Commands.InvokePSFProtectedCommand.ProcessRecord() in
                        C:\Code\github\psframework\library\PSFramework\Commands\InvokePSFProtectedCommand.cs:line 248 
TargetSite            : Void CheckActionPreference(System.Management.Automation.Language.FunctionContext, 
                        System.Exception)
HResult               : -2146233087
HelpLink              :
Data                  : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException        : Cannot convert value "CommandTest 1.0.0.0" to type
                        "PSModuleDevelopment.Template.TemplateInfo". Error: "The magic number in GZip header is not   
                        correct. Make sure you are passing in a GZip stream."
ExceptionTypeName     : System.Management.Automation.ParameterBindingArgumentTransformationException
ExceptionData         : {ErrorId, WasThrownFromThrowStatement, ParameterName, TypeSpecified...}
CategoryInfo          : NotSpecified: (CommandTest 1.0.0.0:PSObject) [],
                        ParameterBindingArgumentTransformationException
ErrorDetails          :
FullyQualifiedErrorId : PSModuleDevelopment_Invoke-PSMDTemplate
InvocationInfo        :
ScriptStackTrace      :
TargetObject          : CommandTest 1.0.0.0
FunctionName          : Invoke-PSMDTemplate
Timestamp             : 03.10.2024 16:51:34
Runspace              : f81663db-f0ec-4d45-a43d-f3a856ac06de
ComputerName          : <redacted>

Error message 2

Message               : [Maintenance] Task 'psmd_dotNetTemplateCache' failed to execute | Exception calling
                        "DownloadString" with "1" argument(s): "The remote server returned an error: (403)
                        Forbidden."
Source                : System.Management.Automation
StackTrace            :    at
                        System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext       
                        funcContext, Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame   
                        frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interprete
                        dFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interprete 
                        dFrame frame)
TargetSite            : Void CheckActionPreference(System.Management.Automation.Language.FunctionContext,
                        System.Exception)
HResult               : -2146233087
HelpLink              :
Data                  : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException        : The remote server returned an error: (403) Forbidden.
ExceptionTypeName     : System.Management.Automation.MethodInvocationException
ExceptionData         : {ErrorRecord, WasThrownFromThrowStatement}
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
ErrorDetails          :
FullyQualifiedErrorId : WebException
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, C:\Program
                        Files\WindowsPowerShell\Modules\PSModuleDevelopment\2.2.11.168\PSModuleDevelopment.psm1:      
                        line 8275
                        at <ScriptBlock>, <No file>: line 23
TargetObject          :
FunctionName          : task:TaskEngine
Timestamp             : 03.10.2024 16:51:35
Runspace              : 66cfb92c-c5c8-4c7b-9c34-90909abbd742
ComputerName          : <redacted>

@splaxi: fyi

FriedrichWeinmann commented 1 month ago

Thanks for reporting this. It's one of the unfortunate side-effects of the latest, critical security update on PSFramework, which really could not wait :(. Combined with a bit of overload at work and it's been taking me to solve this one. Expect a fix within the next few hours.

It should work with old templates, but if you have custom templates that keep failing, try recording them again.

FriedrichWeinmann commented 1 month ago

Version 2.2.12.171 is now out, should resolve the issue

FH-Inway commented 1 month ago

Thanks for the quick fix. The good news is, the error does not occur anymore. The bad news: The files generated by Invoke-PSMDTemplate are now missing content.

I used the following command: Invoke-PSMDTemplate CommandTest -Name Invoke-PSMDTemplate

I ran it once with the latest versions of PSFramework (1.12.346) and PSModuleDevelopment (2.2.12.171): Invoke-PSMDTTemplate.Tests.2.2.12.171.ps1 And once with earlier versions (PSFramework: 1.11.343, PSModuleDevelopment 2.2.11.168), where the content of the generated file matches what I expected so far: Invoke-PSMDTTemplate.Tests.2.2.11.168.ps1

My gist Test Invoke-PSMDTemplate also includes a script for running Invoke-PSMDTemplate with the earlier versions (assuming these versions are installed).

Since this issue is resolved and this new behavior seems more like a regression issue for PSModuleDevelopment, do you want me to close this issue and create a new one over at https://github.com/PowershellFrameworkCollective/PSModuleDevelopment/issues ?

FriedrichWeinmann commented 1 month ago

Thanks for the swift response and test. Yes, shifting this over to the other repository would be good. Will look into it right away and also include automatic tests for template integrity for the next version. Really should have caught that - "Looked good" when I manually checked the result, but obviously didn't check well enough, sorry.

FH-Inway commented 1 month ago

All good. Good idea with the automated template integrity test. Closing down here, will create a new issue on PSModuleDevelopment.