diff --git a/src/System.Management.Automation/engine/LanguagePrimitives.cs b/src/System.Management.Automation/engine/LanguagePrimitives.cs index 9979d7433e5..ac78e5e80d5 100644 --- a/src/System.Management.Automation/engine/LanguagePrimitives.cs +++ b/src/System.Management.Automation/engine/LanguagePrimitives.cs @@ -21,6 +21,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Xml; +using System.Security; using Dbg = System.Management.Automation.Diagnostics; using MethodCacheEntry = System.Management.Automation.DotNetAdapter.MethodCacheEntry; @@ -4918,8 +4919,26 @@ internal static Tuple GetInvalidCastMessages(object valueToConve typeConversion.WriteLine("Type Conversion failed."); errorId = "ConvertToFinalInvalidCastException"; - errorMsg = StringUtil.Format(ExtendedTypeSystem.InvalidCastException, valueToConvert.ToString(), - ObjectToTypeNameString(valueToConvert), resultType.ToString()); + + string valueToConvertTypeName = ObjectToTypeNameString(valueToConvert); + string resultTypeName = resultType.ToString(); + + if (resultType == typeof(SecureString) || resultType == typeof(PSCredential)) + { + errorMsg = StringUtil.Format( + ExtendedTypeSystem.InvalidCastExceptionWithoutValue, + valueToConvertTypeName, + resultTypeName); + } + else + { + errorMsg = StringUtil.Format( + ExtendedTypeSystem.InvalidCastException, + valueToConvert.ToString(), + valueToConvertTypeName, + resultTypeName); + } + return Tuple.Create(errorId, errorMsg); } diff --git a/src/System.Management.Automation/resources/ExtendedTypeSystem.resx b/src/System.Management.Automation/resources/ExtendedTypeSystem.resx index 195fdbb7a1b..f8f9ca714cb 100644 --- a/src/System.Management.Automation/resources/ExtendedTypeSystem.resx +++ b/src/System.Management.Automation/resources/ExtendedTypeSystem.resx @@ -189,6 +189,9 @@ Cannot convert the "{0}" value of type "{1}" to type "{2}". + + Cannot convert the value of type "{0}" to type "{1}". + Cannot convert value "{0}" to type "{1}". Error: "{2}" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/SecureString.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/SecureString.Tests.ps1 index df1fa9294e1..c26689cb7e9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/SecureString.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/SecureString.Tests.ps1 @@ -41,4 +41,14 @@ Describe "SecureString conversion tests" -Tags "CI" { $ss2 = $encodedStr | ConvertTo-SecureString -Key $key $ss2 | ConvertFrom-SecureString -AsPlainText | Should -BeExactly $testString } + + It "Using invalid secure string with ConvertFrom-SecureString produces an exception message without value" { + $ex = { ConvertFrom-SecureString "1234" } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand" -PassThru + $ex.Exception.Message | Should -Not -Match "1234" + } + + It "Using invalid securestring with cast produces an exception message without value" { + $ex = { [securestring]"1234" } | Should -Throw -ErrorId "ConvertToFinalInvalidCastException" -PassThru + $ex.Exception.Message | Should -Not -Match "1234" + } } diff --git a/test/powershell/engine/Basic/Credential.Tests.ps1 b/test/powershell/engine/Basic/Credential.Tests.ps1 index eeccb18879f..850d40c9c52 100644 --- a/test/powershell/engine/Basic/Credential.Tests.ps1 +++ b/test/powershell/engine/Basic/Credential.Tests.ps1 @@ -5,4 +5,9 @@ Describe "Credential tests" -Tags "CI" { # We should explicitly check that the expression returns $null [PSCredential]::Empty.GetNetworkCredential() | Should -BeNullOrEmpty } + + It "Explicit credential cast with string produces an exception message without value" { + $ex = { [pscredential]"1234" } | Should -Throw -ErrorId "ConvertToFinalInvalidCastException" -PassThru + $ex.Exception.Message | Should -Not -Match "1234" + } }