How to completely stop execution of script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

How to completely stop execution of script

Post by Chris_Ediger »

I have a GUI tool that I need to stop executing if a certain condition is met.

Code: Select all

	if ($doesUserExist)
	{
		$distName = (Get-ADUser -Filter { UserPrincipalName -eq $upn } -Server MYSERVER).DistinguishedName
		[System.Windows.Forms.MessageBox]::Show("User already exists in Active Directory. `n$distName", "User Exists", 'OK', 'Error')
		$formUserCreationAndDepro.Close()

	}
If the script finds that a user exists I want to display that message and then stop execution of the script. Apparently .Close() does not close the form or stop execution.

How do I completely stop script execution and close the form?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to completely stop execution of script

Post by jvierra »

"Close" closes a form. Why is that something you don't wnat to do?

Here is a good place to start. It will give you some idea of how this works.

https://www.sapien.com/books_training/W ... werShell-4
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: How to completely stop execution of script

Post by Chris_Ediger »

I don't care what it does I just need it to stop executing if the condition is true and .Close() does not do this.
And thanks but that link isn't particularly helpful to me. I asked a specific question and you gave a very general answer.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to completely stop execution of script

Post by jvierra »

"Close" will not terminate an event in progress. YOu will have to return from teh event after posting the close message or code after the Close method will continue to execute.

Abruptly exiting an event after posting a close message may cause a deadlock or an exception which is dependent on the code you are executing.

In th ecaase of the code you posted the form should close with no issues. There is no need to do anything else.
This topic is 2 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked