foreach not executing on button click

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 6 years and 3 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
AKoval
Posts: 2
Last visit: Tue Jul 28, 2020 1:23 pm

foreach not executing on button click

Post by AKoval »

I have a little script i'm putting together to help a team take members of a security group and explicitly give them permission to a mailbox with Send-As and FullAccess. To trouble shoot this step i created a label in the GUI and if i try and update from inside the foreach loop it does not update and if i put it right after the foreach it updates no problem.

Code: Select all

$buttonConvert_Click={
	#TODO: Place custom script here
	foreach ($member in $Members)
	{
		$label1.Text = "Members?"
		Add-MailboxPermission -Identity "$Mailbox" -User "$member" -AccessRights FullAccess
		Add-ADPermission -Identity "$Mailbox" -User "$member" -ExtendedRights Send-As
		
	}
	#$label1.Text = "Complete"
}
User avatar
Alexander Riedel
Posts: 8488
Last visit: Mon Apr 15, 2024 3:28 pm
Answers: 20
Been upvoted: 37 times

Re: foreach not executing on button click

Post by Alexander Riedel »

Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
AKoval
Posts: 2
Last visit: Tue Jul 28, 2020 1:23 pm

Re: foreach not executing on button click

Post by AKoval »

forgive me if i am just completely missing the point, but i'm not seeing how that helps. Its currently not even attempting to run the foreach loop at all.
User avatar
Alexander Riedel
Posts: 8488
Last visit: Mon Apr 15, 2024 3:28 pm
Answers: 20
Been upvoted: 37 times

Re: foreach not executing on button click

Post by Alexander Riedel »

The label will not update while you are in that loop unless you call DoEvents. So I would guess at this point you do not know if you are entering the ForEach loop or not.
Why not just set a breakpoint and debug?
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: foreach not executing on button click

Post by jvierra »

Quoting and using the wrong variable will cause bad behaviors
Code: [Select all] [Expand/Collapse] [Download] (Untitled.ps1 expanded)
  1. $buttonConvert_Click={
  2.     #TODO: Place custom script here
  3.     foreach ($member in $Members){
  4.         $label1.Text = $members
  5.                 [application]::DoEvents()
  6.         Add-MailboxPermission -Identity $Mailbox -User $member -AccessRights FullAccess
  7.         Add-ADPermission -Identity $Mailbox -User $member -ExtendedRights Send-As
  8.        
  9.     }
  10.     $label1.Text = 'Complete'
  11. }
Also Alex's suggestion is the best way to understand these things. Use the debugger and breakpoint at the start of the loop then "step".

String variables do not need quoting.
This topic is 6 years and 3 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