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 3 weeks 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.
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 (12.11 KiB) Viewed 1517 times
This is what im trying to use and have attached the error in a txt document.
$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
}
}
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
$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
}
}
$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
}
}
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 3 weeks 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.