-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-RemoteForestMembers.ps1
39 lines (39 loc) · 1.52 KB
/
Get-RemoteForestMembers.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<#
.NOTES
Written by Nathan Borup
#>
if (Get-Module -ListAvailable -Name ImportExcel) {
write-host "ImportExcel module installed. Ready for action."
}
else {
Import-Module ImportExcel
}
$groups = Get-ADGroup -Filter { GroupScope -eq "DomainLocal"} | Select *
$groupCount = $groups.Count
$count = 0
$groupObj = @()
$groupErr = @()
foreach ($group in $groups) {
$name = $group.Name
$percentComplete = ($count / $groupCount) * 100
Write-Progress -Activity "Searching for Remote Forest Group Members" -Status 'Progress->' -PercentComplete $percentComplete -CurrentOperation "$count of $groupCount - $name"
try {
$members = Get-ADGroupMember $group.SID -recursive |
Where-Object { $_.distinguishedName -notlike "*DC=dmba*"} | Select Name
}
catch {
$groupErr += $Error
$error.Clear()
}
$count ++
if ($members -ne $null) {
foreach ($member in $members) {
$objMember = New-Object System.Object
$objMember | Add-Member -Type NoteProperty -Name Group -Value $name
$objMember | Add-Member -Type NoteProperty -Name Member -Value $member.Name
$groupObj += $objMember
}
}
}
$groupObj | Export-Excel -WorkSheetname "Remote Forest Members" -Path $env:USERPROFILE\Desktop\members.xlsx
$groupErr | Export-Excel -WorkSheetname "failed Groups" -Path $env:USERPROFILE\Desktop\members.xlsx