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 670 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'){