I have started learning PowerShell but I am still fairly inexperienced. The other night I was curious about how to display a simple dialog box using PowerShell, and I came across the two articles below--one shows how to do a text box and
the other shows how to do a list box.
http://technet.microsoft.com/en-us/library/ff730941.aspx
http://technet.microsoft.com/en-us/library/ff730949.aspx
Based on these articles I decided to do a little learning project. Essentially, I combined the code from the two articles so that I have one dialog box that contains a list box and a text box. The list box contains descriptions of scripts, and
the text box allows a name to be entered. The idea is that a user could select the script to run, put in the employee name, and get the desired results. Currently, there are only two scripts on the list, and both are single liners, so the code
below might seem like a lot for a few single line commands, but as I say, this is a learning project. I can envision eventually adding some data validation, error trapping, etc. But, first I need to get this basic version working.
Below is my code. As you can see, I'm using Remove-Variable to make sure the values are reset each time, and I'm setting breakpoints for the variables. The breakpoint commands are included for testing purposes and will eventually
be removed. I either disable the breakpoints commands after they are set once, or clear the breakpoints between each test run. I've also temporarily disabled the section that determines what to do when the ENTER or ESC keys are used,
and at the end I write the variables to the screen just for visual confirmation that the values were set correctly (This will be removed when the script is working). When I run the script nothing happens--it behaves as if the variables are not getting set.
The odd thing is, when a variable break point is encountered and I hover over the variable, it shows the correct value.
What am I missing? When I run the script why does nothing happen? It seems to me that the values should at least be written to the screen.
Thanks for any help that you can offer!
--Tom
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
Remove-Variable ScriptToRun
Remove-Variable EmployeeName
Set-PSBreakpoint -script ServiceDeskScripts.ps1 -Variable ScriptToRun
Set-PSBreakpoint -script ServiceDeskScripts.ps1 -Variable EmployeeName
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Script Options"
$objForm.Size = New-Object System.Drawing.Size(300,270)
$objForm.StartPosition = "CenterScreen"
# $objForm.KeyPreview = $True
# $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
# {$ScriptToRun=$objListBox.SelectedItem;$EmployeeName=$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,190)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$ScriptToRun=$objListBox.SelectedItem;$EmployeeName=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,190)
$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 select a script to run:"
$objForm.Controls.Add($objLabel)
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80
[void] $objListBox.Items.Add("Email Archive")
[void] $objListBox.Items.Add("Last Sync")
$objForm.Controls.Add($objListBox)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,130)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter the employee name (Last name, first name):"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,150)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$ScriptToRun
$EmployeeName
if ($ScriptToRun -eq "Email Archive") {get-mailbox $EmployeeName |select arch*}
elseif ($ScriptToRun -eq "Last Sync") {Get-ActiveSyncDeviceStatistics -mailbox $EmployeeName |select las*}