powershellget-adusermemberof

display memberof with separate parts


I created a powershell script below about Get-ADUser MemberOf to display VPN Security Groups and the Other Security Groups groups and I would like to eliminate the redundancy on this

Code below first part where it only shows the VPN Security Group of a user

Write-Output ""
Write-Output "memberof"
Write-Output "-------------------"
$User4 = Get-ADUser $sam -Properties MemberOf
$SG2 = $user4.MemberOf
$VPNcount = 0;


    Foreach($group in $SG2)

    {
        $thisgroup = $group.split(",")[0].split("=")[1]
        if($thisgroup -imatch 'Azure') 
        {
            $thisgroup
            $VPNcount++
        }
    }

Now the second part is it displays Other Security Groups such as License, other users privileges access etc

Write-Output ''
Write-Output "Other Security Groups"
Write-Output "---------------"
$User2 = Get-ADUser $sam -Properties MemberOf
$SG = $user2.MemberOf


    Foreach($group in $SG)
    {
        $thisgroup = $group.split(",")[0].split("=")[1]
        $thisgroup
    }

My problem is, VPN Security Groups still showing up Other Security Groups.

VPN Security Groups shouldn't be displaying at the Other Security Groups to eliminate redundancy.

Picture attached to show the result sample of code above

A picture is attached above also for better explanation of vpn security group still shows up on the second part of code, some details in the picture are cropped because of security purposes. Thank you in advance who will help on my problem


Solution

  • What about this?

    Write-Output ''
    Write-Output "Other Security Groups"
    Write-Output "---------------"
    $User2 = Get-ADUser $sam -Properties MemberOf
    $SG = $user2.MemberOf
    
    
        Foreach($group in $SG)
        {
            $thisgroup = $group.split(",")[0].split("=")[1]
            if($thisgroup -notmatch 'Azure') 
            {
                $thisgroup
            }
        }