Powershell输入框

时间:2015-08-18 13:33:58

标签: winforms powershell

我试图让输入框提示用户输入群组而不是硬编码。提前谢谢。

$users = gc "C:\Users\raw.admin\Documents\PowerShell Scripts\users_list.txt"

foreach($u in $users)
{
   Add-ADGroupMember XA-MS_Products -Members $u
} 

我发现了其他人的代码,但我不知道如何根据我的需要调整它。

function point ($x,$y)
{
   New-Object Drawing.Point $x,$y
}

[reflection.assembly]::LoadWithPartialName("System.Drawing") > $null
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") > $null

$form = New-Object Windows.forms.form
$form.text = "Drive Space Results"
$form.size = point 600 400

$label1 = New-Object Windows.forms.Label
$label1.location = point 225 25
$label1.size = point 300 25
$label1.text = "MASTER DOMAIN ONLY"


$label2 = New-Object Windows.forms.Label
$label2.location = point 25 75
$label2.size = point 120 100
$label2.text = "Enter Server Name:"

$input1 = New-Object Windows.forms.TextBox
$input1.location = point 150 75
$input1.size = point 350 75


$run = New-Object Windows.forms.Button
$run.text="RUN"
$run.Location = point 150 120
$run.size = point 100 50
$run.add_click({
   runCall($input1.text)
})

$exit = New-Object Windows.forms.Button
$exit.text="EXIT"
$exit.Location = point 300 120
$exit.size = point 100 50
$exit.add_click({
   $form.close()
})

$out = New-Object Windows.forms.TextBox
$out.location = point 25 200
 $out.size = point 525 150
$out.Anchor = "bottom"
$out.Multiline = $true

$form.controls.addrange(($label1,$label2,$input1,$run,$exit,$out))
$form.add_shown({$form.Activate()})
$form.ShowDialog()

2 个答案:

答案 0 :(得分:1)

我建议你避免使用自定义表单,它需要大量代码,只需使用默认的InputBox。这是一个快速的例子

Add-Type -AssemblyName Microsoft.VisualBasic;
$value = [Microsoft.VisualBasic.Interaction]::InputBox('Enter group name', 'XA Group', '')

$value中,您会找到用户输入的文字

答案 1 :(得分:0)

    $users = gc "C:\Users\raw.admin\Documents\PowerShell Scripts\users_list.txt"
    $group = Read-Host "Enter Group To Display"

    foreach($u in $users)
    {
       Add-ADGroupMember -Identity $group -Members $u
    }