Solved

Powershell GUI Cancel

Posted on 2014-01-02
5
1,111 Views
Last Modified: 2014-01-02
Hi I am building a very basic GUI form in powershell to ask a user for a new password but I am having trouble setting the cancel button to go to the end of the script and not run anything if the cancel button or close icon in the top right is pressed or if no password in entered. Any help and if possible some pointers / explanations would be very welcome. Thanks in advance and see attached script
askEE.txt
0
Comment
Question by:Dead_Eyes
  • 3
  • 2
5 Comments
 

Author Comment

by:Dead_Eyes
ID: 39751824
oh sorry forgot to include the script is running on powershell v4 in windows 8.1
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39751932
  $result =  $form1.ShowDialog()
will store the button text in $result. You can check against that, e.g.
  if ($result -eq 'Cancel') { exit }
0
 

Author Comment

by:Dead_Eyes
ID: 39752008
Thanks I have added that to the end of the script and it exits correctly when cancelled is pressed but
1. when text is not entered and ok is pressed it displays the box again, then continues to run the script after ok is pressed again
2. Box is now displayed twice when entering data
Maybe I got its position wrong here is the end of the script

# Scriptblock to deturmine function of Ok & Cancel buttons
$okb1 = {
$form1.DialogResult = 1
$form1.Hide()
}
$cnb1 = {
$form1.DialogResult = 0
$form1.Hide()
}
$cancelled1 =  $form1.ShowDialog()
if ($cancelled1 -eq 'Cancel') { exit }
#Show Form 1
$form1.ShowDialog()
# Create variable from text input by user in $text1
$result1 = $text1.Text
$result1
Get-ChildItem -Path C:\nso
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 400 total points
ID: 39752054
Yes, you called the form twice. Each ShowDialog will, of course, show the form.
# Scriptblock to deturmine function of Ok & Cancel buttons
$okb1 = {
$form1.DialogResult = 1
$form1.Hide()
}
$cnb1 = {
$form1.DialogResult = 0
$form1.Hide()
}
$btn =  $form1.ShowDialog()
if ($btn -eq 'Cancel') { exit }
# Create variable from text input by user in $text1
$result1 = $text1.Text
$result1
Get-ChildItem -Path C:\nso

Open in new window

0
 

Author Closing Comment

by:Dead_Eyes
ID: 39752751
Thanks think my eyes had lost focus lol
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question