Skip to content

Instantly share code, notes, and snippets.

@lazywinadmin
Created October 22, 2014 02:43
Show Gist options
  • Save lazywinadmin/fda58c848e9482dcfcfa to your computer and use it in GitHub Desktop.
Save lazywinadmin/fda58c848e9482dcfcfa to your computer and use it in GitHub Desktop.
Get-NestedMember
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