Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

If the VerbosePreference set as string, New-SSHConnection throws InvalidCastException.

Open mkht opened this issue 3 years ago • 0 comments

In a script, I set the value to $VerbosePreference variable as string and run New-SSHConnection command. The script ends with InvalidCastException.

Steps to Reproduce

This is a sample script.

# test.ps1
$VerbosePreference = 'Continue'
$Credential = (Get-Credential)
$SSHHost = 'localhost'
$Session = New-SSHSession -ComputerName $SSHHost -Credential $Credential

This script ends with below error.

PS F:\> .\test.ps1
New-SSHSession : Unable to cast object of type 'System.String' to type 'System.Management.Automation.ActionPreference'.
At F:\test.ps1:5 char:12
+ $Session = New-SSHSession -ComputerName $SSHHost -Credential $Credent …
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [New-SSHSession], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException,SSH.NewSshSession

Environment

PS F:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS F:\> Get-Module Posh-SSH

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   3.0.8                 Posh-SSH                            {Get-SCPItem, Get-SFTPItem, Get-SSHHostKey, Get-SSHJs…

Workaround

Method 1. Set VerbosePrefenrece as ActionPreference

$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue

Method 2. Specify scope of VerbosePreference as Global

$Global:VerbosePreference = 'Continue'

mkht avatar Dec 13 '22 10:12 mkht