diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs index f5c4550b1..9e0368860 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs @@ -375,7 +375,7 @@ namespace Microsoft.PowerShell break; } else - if (rawInputString.StartsWith(PromptCommandPrefix, StringComparison.Ordinal)) + if (!string.IsNullOrEmpty(desc.Label) && rawInputString.StartsWith(PromptCommandPrefix, StringComparison.Ordinal)) { processedInputString = PromptCommandMode(rawInputString, desc, out inputDone); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index e5af81b01..702b6b059 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -9,28 +9,44 @@ Describe "Read-Host Test" -tag "CI" { $ps.Runspace = $rs $ps.Commands.Clear() } + AfterEach { $ps.Commands.Clear() } + AfterAll { $rs.Close() $rs.Dispose() $ps.Dispose() } + It "Read-Host returns expected string" { $result = $ps.AddCommand("Read-Host").Invoke() $result | Should -Be $th.UI.ReadLineData } + It "Read-Host sets the prompt correctly" { $result = $ps.AddScript("Read-Host -prompt myprompt").Invoke() $prompt = $th.ui.streams.prompt[0] $prompt | Should -Not -BeNullOrEmpty $prompt.split(":")[-1] | Should -Be myprompt } + It "Read-Host returns a secure string when using -AsSecureString parameter" { $result = $ps.AddScript("Read-Host -AsSecureString").Invoke() | select-object -first 1 $result | Should -BeOfType SecureString - [pscredential]::New("foo",$result).GetNEtworkCredential().Password | Should -BeExactly TEST + [pscredential]::New("foo",$result).GetNetworkCredential().Password | Should -BeExactly TEST + } + It "Read-Host doesn't enter command prompt mode" { + $result = "!1" | pwsh -NoProfile -c "Read-host -Prompt 'foo'" + if ($IsWindows) { + # Windows write to console directly so can't capture prompt in stdout + $expected = @('!1','!1') + } + else { + $expected = @('foo: !1','!1') + } + $result | should -BeExactly $expected } }