2

I'm trying to have an input box to prompt the user to enter the group instead of hard coding it. Thanks in advance.

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

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

I found this snipit of someone elses code but I'm not sure how to adapt it for what I want.

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()
1
  • here's a technet article that shows you the basics of how to use a winform from powershell. Your code above looks similar: I think all you need to so is change $form.ShowDialog() to [void] $form.ShowDialog() and add a new line like $x to save the user's input into a variable ($x in this case) Commented Aug 18, 2015 at 15:13

2 Answers 2

2

I suggest you avoid using a custom form, it requires a lot of code, just use a default InputBox. Here is a fast example

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

In $value you will find text user entered

Sign up to request clarification or add additional context in comments.

2 Comments

This does what I want. Thanks.
Can you tell me how to user the sample code I posted?
0

Ie.

    $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
    } 

1 Comment

That is one option but I would really like to know how to present the user with a popup box. There is no reason why your answer wont work. I am fairly new to Powershell and would like to add a little something to the scripts I create.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.