Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,17 @@ protected void ValidateComputerName(string[] computerNames)
/// <returns>Parameter value as string.</returns>
private static string GetSSHConnectionStringParameter(object param)
{
if (param is string paramValue && !string.IsNullOrEmpty(paramValue))
string paramValue;
try
{
paramValue = LanguagePrimitives.ConvertTo<string>(param);
}
catch (PSInvalidCastException e)
{
throw new PSArgumentException(e.Message, e);
}

if (!string.IsNullOrEmpty(paramValue))
{
return paramValue;
}
Expand All @@ -1075,12 +1085,19 @@ private static string GetSSHConnectionStringParameter(object param)
/// <returns>Parameter value as integer.</returns>
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<int>(param);
}
catch (PSInvalidCastException e)
{
throw new PSArgumentException(e.Message, e);
}
}

#endregion Private Methods
Expand Down
18 changes: 18 additions & 0 deletions test/powershell/engine/Remoting/SSHRemotingCmdlets.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<testName>" -TestCases $TestCasesSSHConnection {
param($scriptBlock)
{ & $scriptBlock } | Should -Throw -ErrorId '2100,PSSessionOpenFailed'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is out of the PR but I'd prefer to see more useful message.

}
}