When the $VerbosePreference variable has been explicitly set to a string, the BeginProcessing function throws an InvalidCastException.
This can be fixed by either getting PowerShell to cast like this:
[System.Management.Automation.LanguagePrimitives]::ConvertTo("Continue", [System.Management.Automation.ActionPreference])
or doing an [Enum]::Parse.
Repro:
function foo {
[CmdletBinding()] param()
$VerbosePreference = "Continue"
$VerbosePreference.GetType().FullName
1 | Split-Pipeline -Count 1 -Script {}
}
foo
$error[0] | fl * -Force
Result:
System.String
Split-Pipeline : Specified cast is not valid.
At line:6 char:13
+ 1 | Split-Pipeline -Count 1 -Script {}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Split-Pipeline], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException,SplitPipeline.SplitPipelineCommand
PSMessageDetails :
Exception : System.InvalidCastException: Specified cast is not valid.
at SplitPipeline.SplitPipelineCommand.BeginProcessing()
at System.Management.Automation.Cmdlet.DoBeginProcessing()
at System.Management.Automation.CommandProcessorBase.DoBegin()
TargetObject :
CategoryInfo : NotSpecified: (:) [Split-Pipeline], InvalidCastException
FullyQualifiedErrorId : System.InvalidCastException,SplitPipeline.SplitPipelineCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at foo, <No file>: line 6
at <ScriptBlock>, <No file>: line 8
PipelineIterationInfo : {}
When the $VerbosePreference variable has been explicitly set to a string, the BeginProcessing function throws an InvalidCastException.
This can be fixed by either getting PowerShell to cast like this:
[System.Management.Automation.LanguagePrimitives]::ConvertTo("Continue", [System.Management.Automation.ActionPreference])or doing an
[Enum]::Parse.Repro:
Result: