如何在同一输入框中包含多个输入字段

时间:2017-06-22 14:03:11

标签: powershell

我想让技术人员输入用户名和组名,但是在一个输入框中。有谁愿意告诉我怎么做?

Function add-togroup{

    #Adds members to group in AD

    #$users = Read-Host "Enter a username"
    Add-Type -AssemblyName Microsoft.VisualBasic;
    $value = [Microsoft.VisualBasic.Interaction]::InputBox('Enter username', 
    'Username')
    $value2 = [Microsoft.VisualBasic.Interaction]::InputBox('Enter group 
    name', 'XA Group','')
    $group_membership = Get-ADPrincipalGroupMembership $users | select name | 
    format-table -auto
    foreach($u in $value)
    {
        Add-ADGroupMember $value2 -Members $u
    } 

    Write-Host $group_membership
}

因此,我能够按顺序使用多个对话框,但如果我可以将其转换为单个框/表单,则可以提供更好的用户体验。

1 个答案:

答案 0 :(得分:2)

如果您对可用的基本表单不满意,那么您拥有的一个选项是使用.Net表单在PowerShell中自行编写。只是为了展示一个可以从......构建的例子。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter" -or $_.KeyCode -eq "Escape"){
        $objForm.Close()
    }
})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please enter the information in the space below:"
$objForm.Controls.Add($objLabel) 

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
$objForm.Controls.Add($objTextBox) 

$objTextBox2 = New-Object System.Windows.Forms.TextBox 
$objTextBox2.Location = New-Object System.Drawing.Size(10,70) 
$objTextBox2.Size = New-Object System.Drawing.Size(260,20) 
$objForm.Controls.Add($objTextBox2) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void]$objForm.ShowDialog()

$objTextBox.Text
$objTextBox2.Text

great primer on the subject on TechNet借用重度,你应该阅读它,因为它会更好地引导你。我删除了一些变量填充逻辑,因为它有缺陷并添加了另一个文本框。最后两行返回“user”输入的值。除了添加文本框之外,我还将大部分其他化妆品更改留给您,以帮助您更好地了解此处涉及的内容。

请记住新添加对象的位置和大小,并确保将其实际添加到表单中。

由于没有用于表单构建的GUI,它看起来令人生畏,但实际上并不那么难。你只需要进行实验。如果您如此倾向于有第三方工具可以帮助解决这个问题。