Skip to content

Specified cast is not valid when $VerbosePreference is a string #12

Description

@dreaburn

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 : {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions