Created
October 22, 2014 02:43
-
-
Save lazywinadmin/fda58c848e9482dcfcfa to your computer and use it in GitHub Desktop.
Get-NestedMember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-NestedMember | |
{ | |
<# | |
.SYNOPSIS | |
Find all Nested members of a group | |
.DESCRIPTION | |
Find all Nested members of a group | |
.PARAMETER GroupName | |
Specify one or more GroupName to audit | |
.Example | |
Get-NestedMember -GroupName TESTGROUP | |
.Example | |
Get-NestedMember -GroupName TESTGROUP,TESTGROUP2 | |
#> | |
[CmdletBinding()] | |
PARAM( | |
[String[]]$GroupName) | |
BEGIN | |
{ | |
TRY{ | |
if(-not(Get-Module Activedirectory -ErrorAction Stop)){ | |
Write-Verbose -Message "[BEGIN] Loading ActiveDirectory Module" | |
Import-Module ActiveDirectory -ErrorAction Stop} | |
} | |
CATCH | |
{ | |
Write-Warning -Message "[BEGIN] An Error occured" | |
Write-Warning -Message $error[0].exception.message | |
} | |
} | |
PROCESS | |
{ | |
TRY | |
{ | |
FOREACH ($Group in $GroupName) | |
{ | |
$GroupObject = Get-ADGroup -Identity $Group -ErrorAction Stop | |
IF($GroupObject) | |
{ | |
$GroupObject | Get-ADGroupMember -ErrorAction Stop | ForEach-Object -Process { | |
$ParentGroup = $GroupObject.Name | |
Write-Verbose -Message "[PROCESS] Name:$($_.name) | ObjectClass:$($_.ObjectClass)" | |
$CurrentObject = $_ | |
switch ($_.ObjectClass) | |
{ | |
"user" { $CurrentObject | Select-Object Name,SamAccountName,ObjectClass,DistinguishedName,@{L="ParentGroup";E={$ParentGroup}}} | |
"computer" { $CurrentObject | Select-Object Name,SamAccountName,ObjectClass,DistinguishedName, @{L="ParentGroup";E={$ParentGroup}}} | |
"contact" { $CurrentObject | Select-Object Name,SamAccountName,ObjectClass,DistinguishedName, @{L="ParentGroup";E={$ParentGroup}}} | |
"group" { | |
# Output Object | |
$CurrentObject | Select-Object Name,SamAccountName,ObjectClass,DistinguishedName, @{L="ParentGroup";E={$ParentGroup}} | |
# Find Child | |
Get-NestedMember -GroupName $CurrentObject.Name | |
}#Group | |
}#Switch | |
}#ForeachObject | |
}#IF($GroupObject) | |
ELSE { | |
Write-Warning -Message "[PROCESS] Can't find the group $Group" | |
}#ELSE | |
}#FOREACH ($Group in $GroupName) | |
}#TRY | |
CATCH{ | |
Write-Warning -Message "[PROCESS] An Error occured" | |
Write-Warning -Message $error[0].exception.message } | |
}#PROCESS | |
END | |
{ | |
Write-Verbose -Message "[END] Get-NestedMember" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment