?
Solved

Powershell GUI Cancel

Posted on 2014-01-02
5
Medium Priority
?
1,513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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 70

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

Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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 antispam), the admini…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

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