Forms and input boxes

Hi,

I have a script which works. What I would like is a user friendly form, which requires input from a user, which will then edit the content of the script before running it.

Ideally I need about 3 input boxes, each one will edit a variable in the script.

Input box 1 will edit $x in the script

Input box 2 will edit $y in the script

Input box 3 will edit $z in the script

Then an 'enter' button to run script.

Thanks in advance for any assistance.

May 24th, 2015 6:00pm

What have you tried?  Have you looked at any of the tutorials for PowerShell ort explored and of the many books. If you search you will find many examples.

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2015 6:38pm

Here is a teaser to get you started.  You will have to do the research to understand how to use this.

Add-Type -AssemblyName System.Windows.forms
$form=New-Object System.Windows.Forms.Form
$b=New-Object System.Windows.Forms.Textbox
$b.Location='10,10'
$form.Controls.Add($b)
$b=New-Object System.Windows.Forms.Textbox
$b.Location='10,40'
$form.Controls.Add($b)
$b=New-Object System.Windows.Forms.Textbox
$b.Location='10,70'
$form.Controls.Add($b)
$form.ShowDialog()

May 24th, 2015 6:48pm

Hi ASif,

You can also refer to the script below to start input box:

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

$form = New-Object System.Windows.Forms.Form;
 $form.Width = 500;
 $form.Height = 150;
 $form.Text = $title;
 $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

##############Define text label1
 $textLabel1 = New-Object System.Windows.Forms.Label;
 $textLabel1.Left = 25;
 $textLabel1.Top = 15;

$textLabel1.Text = '$x';

##############Define text label2

$textLabel2 = New-Object System.Windows.Forms.Label;
 $textLabel2.Left = 25;
 $textLabel2.Top = 50;

$textLabel2.Text = '$y';

##############Define text label3

$textLabel3 = New-Object System.Windows.Forms.Label;
 $textLabel3.Left = 25;
 $textLabel3.Top = 85;

$textLabel3.Text = '$z';

############Define text box1 for input
 $textBox1 = New-Object System.Windows.Forms.TextBox;
 $textBox1.Left = 150;
 $textBox1.Top = 10;
 $textBox1.width = 200;

############Define text box2 for input

$textBox2 = New-Object System.Windows.Forms.TextBox;
 $textBox2.Left = 150;
 $textBox2.Top = 50;
 $textBox2.width = 200;

############Define text box3 for input

$textBox3 = New-Object System.Windows.Forms.TextBox;
 $textBox3.Left = 150;
 $textBox3.Top = 90;
 $textBox3.width = 200;

#############Define default values for the input boxes
 $defaultValue = 
$textBox1.Text = $defaultValue;
 $textBox2.Text = $defaultValue;
 $textBox3.Text = $defaultValue;

#############define button
 $button = New-Object System.Windows.Forms.Button;
 $button.Left = 360;
 $button.Top = 85;
 $button.Width = 100;
 $button.Text = Enter;

############# This is when you have to close the form after getting values
 $eventHandler = [System.EventHandler]{
 $textBox1.Text;
 $textBox2.Text;
 $textBox3.Text;
 $form.Close();};

$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
 $form.Controls.Add($button);
 $form.Controls.Add($textLabel1);
 $form.Controls.Add($textLabel2);
 $form.Controls.Add($textLabel3);
 $form.Controls.Add($textBox1);
 $form.Controls.Add($textBox2);
 $form.Controls.Add($textBox3);
 $ret = $form.ShowDialog();

#################return values
$textBox1.Text, $textBox2.Text, $textBox3.Text

These articles are for your reference:

Creating a Custom Input Box

Powershell Custom GUI input box for passing values to Variables

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

Free Windows Admin Tool Kit Click here and download it now
May 25th, 2015 1:58am

PowerShell forms 101:

http://blogs.technet.com/b/stephap/archive/2012/04/23/building-forms-with-powershell-part-1-the-form.aspx

May 25th, 2015 5:31pm

Hi All, Building a form is not the issue. Getting the output into $x, $y, $z is also not an issue. What i would like to understand is how i can then use those 3 values to immediately edit a script and run it.Hope that makes more sense.

Thanks

Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 5:22am

Hi All, Building a form is not the issue. Getting the output into $x, $y, $z is also not an issue. What i would like to understand is how i can then use those 3 values to immediately edit a script and run it.Hope that makes more sense.

Thanks

You are asking a very badly put question.  What is it that you are asking? What doe edit a variable mean?

Start by posting your script.

May 26th, 2015 5:36am

for example, if i had the below script.

c:\scripts\test.txt

--- write-host "today is $x"-----

I would like the user to enter 'monday' on the form. it will then edit $x and run the script. Result should be

'today is monday'

Apologies if my example is confusing.

Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 5:54am

You have to write your own script we will not write it for you.  The information you have provided is not useful for understanding what you are trying to do.

May 26th, 2015 6:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics