?
Solved

Powershell GUI Cancel

Posted on 2014-01-02
5
Medium Priority
?
1,789 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 71

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 71

Accepted Solution

by:
Qlemo earned 1600 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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

862 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