function Get-ADPrincipalGroupMembershipRecursive( ) {
Param(
[string] $dsn,
[array]$groups = @()
)
$obj = Get-ADObject $dsn -Properties memberOf
foreach( $groupDsn in $obj.memberOf ) {
$tmpGrp = Get-ADObject $groupDsn -Properties memberOf
if( ($groups | Where-Object { $_.DistinguishedName -eq $groupDsn }).Count -eq 0 ) {
$groups += $tmpGrp
$groups = Get-ADPrincipalGroupMembershipRecursive $groupDsn $groups
}
}
return $groups
}
function Get-Member() {
param (
[string] $username,
[string] $groupname
)
# Get the AD groups the user is member of
$groups = Get-ADPrincipalGroupMembershipRecursive -dsn (Get-ADUser $username).DistinguishedName
if ($groups.Name -contains $groupname) {
# user is member of the group
return $true
}
else {
return $false
}
}
# Get the username of the current logged on user
#$username = $env:USERNAME
$username = 'a9316143'
$groupname = 'GAP_VA_OpCon'
Get-Member -username $username -group $groupname