Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Using Forms with .net object to input multilple fields in powershell script

$
0
0
Hello,
Please forgive if this question seems basic but I am new to powershell and .net. I want to write a little script that takes as input from the user several pieces of info on
how they want their computer setup as regards sleeptime,screensaver timeout,hibernating etc. I have a simple batch file that I run and change the parameters for timeouts as needed,but that is no good for the end user. I researched online and found a piece of code for one input field but I want the script to take in four input values and then I will have the script issue the appropriate commands to the registry. Can someone tell me how to achieve this in the script? I also noticed when I run the script as is the value for $x isnt output in the shell window only the $x is echoed in the window?? Any ideas most appreciated. Here is the code I started using:
# see url http://technet.microsoft.com/en-us/library/ff730941.aspx for reference


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

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Screen Saver & Power Settings Changer"
$objForm.Size = New-Object System.Drawing.Size(650,375)
$objForm.StartPosition = "CenterScreen"

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

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

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,250)
$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(20,40)
$objLabel.Size = New-Object System.Drawing.Size(380,20)
$objLabel.Text = "Change Turn Off Display Value (In Minutes) :"
$objForm.Controls.Add($objLabel)

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

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(20,90)
$objLabel.Size = New-Object System.Drawing.Size(380,20)
$objLabel.Text = "Please enter Screensaver Timeout :"
$objForm.Controls.Add($objLabel)

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

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(20,130)
$objLabel.Size = New-Object System.Drawing.Size(680,20)
$objLabel.Text = "Change sleep time in minutes (Should be longer then Screen Saver timeout):"
$objForm.Controls.Add($objLabel)

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

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(20,170)
$objLabel.Size = New-Object System.Drawing.Size(380,20)
$objLabel.Text = "Lastly turnoff Hibernate:"
$objForm.Controls.Add($objLabel)

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

$objForm.Topmost = $True

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

$x

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>