Unhandled Exception "System Error"

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 10 months and 2 days 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
axi0m1989
Posts: 5
Last visit: Wed Oct 18, 2023 1:28 pm

Unhandled Exception "System Error"

Post by axi0m1989 »

I am trying to code a button that will prompt you for credentials and then do an if/else statement on them. It doesnt seem to like -match. If i use -like or -contains then do not get this system error exception but i can't figure out why its throwing the error.
Error.png
Error.png (12.11 KiB) Viewed 1490 times
This is what im trying to use and have attached the error in a txt document.

Code: Select all

	$Cred = Get-Credential
	$ADGroupMembers = Get-ADGroupMember "Name of AD Group"
	if ($ADGroupMembers.samaccountname -match $cred.UserName)
	{
		continue
	}
	Else
	{
		[System.Windows.Forms.MessageBox]::Show("User does not have permissions to load, Please reach out to Device Support", "LTI", 'OK')
		Return
	}
}
Attachments
Exception Text .txt
(2.17 KiB) Downloaded 43 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unhandled Exception "System Error"

Post by jvierra »

"Match" requires a regex expression. "Like" requires a wild card.
User avatar
axi0m1989
Posts: 5
Last visit: Wed Oct 18, 2023 1:28 pm

Re: Unhandled Exception "System Error"

Post by axi0m1989 »

So i changed the code to this and while i no longer get the error it is failing to check properly. This code does work without GUI if i do the checks in powershell ISE

Code: Select all

$button1_Click={
	#TODO: Place custom script here
	$Cred = Get-Credential
	$ADUser = Get-ADUser $Cred.UserName -Properties memberof
	if ($ADUser.memberof -like "*Groupname*")
	{
		continue
	}
	Else
	{
		[System.Windows.Forms.MessageBox]::Show("User does not have permissions to load, Please reach out to Device Support", "MH - LTI", 'OK')
		Return
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unhandled Exception "System Error"

Post by jvierra »

"MemberOf" is a list of items so "like" will not work as expected.

I recommend taking some time to learn basic programming primitives that allow us to match select and filter items and groups.

if(Get-ADUser $Cred.UserName -Properties memberof).memberOf -contains 'Groupname' ){
User avatar
axi0m1989
Posts: 5
Last visit: Wed Oct 18, 2023 1:28 pm

Re: Unhandled Exception "System Error"

Post by axi0m1989 »

Running that code causes the system error.

Code: Select all

$button1_Click={
	#TODO: Place custom script here
	$Cred = Get-Credential
	if ((Get-ADUser $Cred.UserName -Properties memberof).memberof -contains "GroupDistinguishedName")
		{
			continue
		}
	
	else
	{
		[System.Windows.Forms.MessageBox]::Show("User does not have permissions to load, Please reach out to Device Support", "LTI", 'OK')
		Return
	}
}
User avatar
axi0m1989
Posts: 5
Last visit: Wed Oct 18, 2023 1:28 pm

Re: Unhandled Exception "System Error"

Post by axi0m1989 »

Narrowing it down. The system error is related to the "continue" if the check works correctly
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unhandled Exception "System Error"

Post by jvierra »

Likely the user does not exist. Get user separately to be sure you have one.
User avatar
axi0m1989
Posts: 5
Last visit: Wed Oct 18, 2023 1:28 pm

Re: Unhandled Exception "System Error"

Post by axi0m1989 »

User exists, I can test this outside of Powershell Studio without GUI and it works fine. It just doesnt like the word "Continue" for some reason
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unhandled Exception "System Error"

Post by jvierra »

The word I posted is "contains". You really need to take time to learn PowerShell basics and these things wouldn't happen.
if(Get-ADUser $Cred.UserName -Properties memberof).memberOf -contains 'Groupname'){
This topic is 10 months and 2 days 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