Solved

Powershell GUI Cancel

Posted on 2014-01-02
5
1,228 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 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

Suggested Solutions

Title # Comments Views Activity
Arrays & Loops in PowerShell 4 34
Matching variables and Compare-Object 24 54
Deleting folders and files using Powershell 4 39
Script to Ping/Resovle names given IP 4 18
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Set OWA language and time zone in Exchange for individuals, all users or per database.
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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