Read-Host
本质上只支持一行输入,因此您有以下选择:
或者:
使用 WinForms 创建多行输入框:
- 注意:对于单行输入框,可以使用
[Microsoft.VisualBasic.Interaction]::InputBox()
- 查看这个答案。
下面创建了一个固定大小的示例对话框,带有多行文本框以及确定和取消按钮(PSv5+,但可以适应早期版本):
# Declare implied namespaces, so that types from
# the loaded assemblies can be referred to by mere name
# (e.g., 'Form' instead of 'System.Windows.Forms')
# Requires PSv5+
using namespace System.Windows.Forms
using namespace System.Drawing
# Load the System.Windows.Forms assembly
# which implicitly loads System.Drawing too.
Add-Type -AssemblyName System.Windows.Forms
# Create the form.
($form = [Form] @{
Text = "Enter Email Addresses"
Size = [Size]::new(300,300)
ControlBox = $false
FormBorderStyle = 'FixedDialog'
StartPosition = 'CenterScreen'
}).Controls.AddRange(@(
($textBox = [TextBox] @{
MultiLine = $true
Location = [Point]::new(10, 10)
Size = [Size]::new(260, 200)
})
($okButton = [Button] @{
Location = [Point]::new(100, 220)
Size = [Size]::new(80,30)
Text = '&OK'
DialogResult = 'OK'
Enabled = $false
})
($cancelButton = [Button] @{
Location = [Point]::new(190, 220)
Size = [Size]::new(80,30)
Text = 'Cancel'
})
))
# Make Esc click the Cancel button.
# Note: We do NOT use $form.AcceptButton = $okButton,
# because that would prevent using Enter to enter multiple lines.
$form.CancelButton = $cancelButton
# Make sure that OK can only be clicked if the textbox is non-blank.
$textBox.add_TextChanged({
$okButton.Enabled = $textBox.Text.Trim().Length -gt 0
})
# Display the dialog modally and evaluate the result.
if ($form.ShowDialog() -ne 'OK') {
Throw 'Canceled by user request.'
}
# Parse the multi-line string into an array of individual addresses.
$addressesEntered = -split $textBox.Text
# Diagnostic output.
Write-Verbose -Verbose 'The following addresses were entered:'
$addressesEntered