Powershell面具密码

时间:2014-08-19 19:07:58

标签: powershell passwords

我想提示用户输入密码列表,一次一行。当用户输入密码时,它应显示为 *

我有一个功能

function Read-MultiLineInputBoxDialogPwd([string]$Message, [string]$WindowTitle, [string]$DefaultText){

    Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.Windows.Forms

    # Create the label
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Size(10,10)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.AutoSize = $true
    $label.Text = $Message

    # Create the TextBox used to capture the user's text
    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Size(10,40)
    $textBox.Size = New-Object System.Drawing.Size(575,200)     
    $textBox.AcceptsReturn = $true    
    $textBox.AcceptsTab = $false    
    $textBox.Multiline = $true    
    $textBox.ScrollBars = 'Both'    
    $textBox.Text = $DefaultText
    $textBox.UseSystemPasswordChar = $True

    # Create the OK button.     
    $okButton = New-Object System.Windows.Forms.Button     
    $okButton.Location = New-Object System.Drawing.Size(415,250)     
    $okButton.Size = New-Object System.Drawing.Size(75,25)     
    $okButton.Text = "OK"    
    $okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() }) 

    # Create the Cancel button.     
    $cancelButton = New-Object System.Windows.Forms.Button     
    $cancelButton.Location = New-Object System.Drawing.Size(510,250)     
    $cancelButton.Size = New-Object System.Drawing.Size(75,25)     
    $cancelButton.Text = "Cancel"    
    $cancelButton.Add_Click({ $form.Tag = $null; $form.Close() }) 

    # Create the form.     
    $form = New-Object System.Windows.Forms.Form      
    $form.Text = $WindowTitle    
    $form.Size = New-Object System.Drawing.Size(610,320)     
    $form.FormBorderStyle = 'FixedSingle'    
    $form.StartPosition = "CenterScreen"    
    $form.AutoSizeMode = 'GrowAndShrink'    
    $form.Topmost = $True    
    $form.AcceptButton = $okButton    
    $form.CancelButton = $cancelButton    
    $form.ShowInTaskbar = $true

    # Add all of the controls to the form.     
    $form.Controls.Add($label)     
    $form.Controls.Add($textBox)     
    $form.Controls.Add($okButton)     
    $form.Controls.Add($cancelButton) 

    # Initialize and show the form.     
    $form.Add_Shown({$form.Activate()})     
    $form.ShowDialog() > $null   # Trash the text of the button that was clicked.           

    # Return the text that the user entered.     
    return $form.Tag 
}

我调用函数

$multiLineTextPwd = Read-MultiLineInputBoxDialogPwd -Message "All possible passwords" -WindowTitle "Passwords" -DefaultText "Please enter all possible passwords, one line at a time..."

但是当它弹出时,即使我设置了以下

,文本仍会以纯文本显示
$textBox.UseSystemPasswordChar = $True

如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

老实说,我觉得通过单行文本框和“添加另一个密码”可以更好地实现这一目标。用户可以输入密码的按钮,然后单击按钮添加另一个密码。您只需将它们添加到数组中,并且必须确保在提交时它检查该框中的任何内容并在执行操作之前向数组添加任何内容。

当我去查看Textbox类的MSDN列表时,所有密码屏蔽引用都明确指出单行文本框,因此您可能无法使用多行文本框进行屏蔽。

答案 1 :(得分:1)

如果您阅读文档here,您会看到对于多行文字框,UseSystemPasswordChar无效。

答案 2 :(得分:-1)

实现多行文本框的keydown事件,将*附加到TB中,并将键码字符串追加到字符串变量中。