diff --git a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs
index b12680865f4..f7e149ba14e 100644
--- a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs
+++ b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs
@@ -1060,7 +1060,17 @@ protected void ValidateComputerName(string[] computerNames)
/// Parameter value as string.
private static string GetSSHConnectionStringParameter(object param)
{
- if (param is string paramValue && !string.IsNullOrEmpty(paramValue))
+ string paramValue;
+ try
+ {
+ paramValue = LanguagePrimitives.ConvertTo(param);
+ }
+ catch (PSInvalidCastException e)
+ {
+ throw new PSArgumentException(e.Message, e);
+ }
+
+ if (!string.IsNullOrEmpty(paramValue))
{
return paramValue;
}
@@ -1075,12 +1085,19 @@ private static string GetSSHConnectionStringParameter(object param)
/// Parameter value as integer.
private static int GetSSHConnectionIntParameter(object param)
{
- if (param is int paramValue)
+ if (param == null)
{
- return paramValue;
+ throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter);
}
- throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter);
+ try
+ {
+ return LanguagePrimitives.ConvertTo(param);
+ }
+ catch (PSInvalidCastException e)
+ {
+ throw new PSArgumentException(e.Message, e);
+ }
}
#endregion Private Methods
diff --git a/test/powershell/engine/Remoting/SSHRemotingCmdlets.Tests.ps1 b/test/powershell/engine/Remoting/SSHRemotingCmdlets.Tests.ps1
index 4141ab6c1ba..a84186ab260 100644
--- a/test/powershell/engine/Remoting/SSHRemotingCmdlets.Tests.ps1
+++ b/test/powershell/engine/Remoting/SSHRemotingCmdlets.Tests.ps1
@@ -40,3 +40,21 @@ Describe "SSHConnection parameter hashtable error conditions" -Tags 'Feature' {
{ & $scriptBlock } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.NewPSSessionCommand"
}
}
+
+Describe "SSHConnection parameter hashtable type conversions" -Tags 'Feature', 'Slow' {
+
+ BeforeAll {
+
+ # Port 49151 is IANA reserved, so it should not be in use. If type conversion fails
+ # then New-PSSession will throw before even attempting to open the session.
+ $TestCasesSSHConnection = @(
+ @{scriptBlock = {New-PSSession -ErrorAction Stop -SSHConnection @{ Port = 49151; ComputerName = [psobject]'localhost' }}; testName = 'SSHConnection can convert PSObject to string'}
+ @{scriptBlock = {New-PSSession -ErrorAction Stop -SSHConnection @{ Port = [psobject]49151; ComputerName = 'localhost' }}; testName = 'SSHConnection can convert PSObject to int'}
+ )
+ }
+
+ It "" -TestCases $TestCasesSSHConnection {
+ param($scriptBlock)
+ { & $scriptBlock } | Should -Throw -ErrorId '2100,PSSessionOpenFailed'
+ }
+}