Solved

Powershell GUI Cancel

Posted on 2014-01-02
5
978 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 68

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 68

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now