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 @@ -97,17 +97,26 @@ public override PSCredential PromptForCredential(
passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName
);

//
// now, prompt for the password
//
WriteToConsole(passwordPrompt, true);
password = ReadLineAsSecureString();
if (password == null)
if (!InternalTestHooks.NoPromptForPassword)
{
return null;
WriteToConsole(passwordPrompt, transcribeResult: true);
password = ReadLineAsSecureString();
if (password == null)
{
return null;
}

WriteLineToConsole();
}
else
{
password = new SecureString();
}

WriteLineToConsole();
if (!string.IsNullOrEmpty(targetName))
{
userName = StringUtil.Format("{0}\\{1}", targetName, userName);
}

cred = new PSCredential(userName, password);

Expand Down
1 change: 1 addition & 0 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,7 @@ public static class InternalTestHooks
internal static bool BypassOnlineHelpRetrieval;
internal static bool ForcePromptForChoiceDefaultOption;
internal static bool BypassOutputRedirectionCheck;
internal static bool NoPromptForPassword;

// Stop/Restart/Rename Computer tests
internal static bool TestStopComputer;
Expand Down
20 changes: 20 additions & 0 deletions test/powershell/Host/HostUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,23 @@ Describe "InvokeOnRunspace method on remote runspace" -tags "Feature","RequireAd
$results[0] | Should -Be "Hello!"
}
}

Describe 'PromptForCredential' {
BeforeAll {
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $true)
}

AfterAll {
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $false)
}

It 'Should accept no targetname' {
$out = $Host.UI.PromptForCredential('caption','message','myUser',$null)
$out.UserName | Should -BeExactly 'myUser'
}

It 'Should accept targetname as domain' {
$out = $Host.UI.PromptForCredential('caption','message','myUser','myDomain')
$out.UserName | Should -BeExactly 'myDomain\myUser'
}
}