Home > front end >  display memberof with separate parts
display memberof with separate parts

Time:10-07

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

CodePudding user response:

I think you could generate objects which would be of benefit for further processing and also in case of output, e.g.:

$user = get-aduser -Identity $sam -Properties memberof

#Process items and store result in $result
$result = @(
    foreach ($group in $user.memberof){
        If ($group -match "CN=Azure"){
            $type='VPN Security Groups'
        }
        Else {
            $type='Other Security Groups'
        }
        [pscustomobject]@{
            type=$type
            name=(($group -split ',')[0] -replace 'cn=',$null)
        }
    }
)

#Output on screen
$result | ft *

#Output on screen as csv
$result | ConvertTo-Csv

#Output as csv file
$result | export-csv [path]

CodePudding user response:

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
        }
    }
  • Related