-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGet-UnusedGroups.ps1
More file actions
167 lines (145 loc) · 5.27 KB
/
Get-UnusedGroups.ps1
File metadata and controls
167 lines (145 loc) · 5.27 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<#
from https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-use-windows-powershell-2-0-to-find-active-directory-domain-services-groups-not-being-used/
.Synopsis
Queries Active Directory for groups that are unused. It will remove
unused groups, or simply report. In addition, it can create empty
groups for test purposes
.Example
Get-UnusedGroups.ps1 -CreateGroups -path "ou=testou,dc=nwtraders,dc=com" `
-numberGroups 5
Creates 5 empty global groups in the TestOU of nwtraders.com. Groups will
be named test1, test2 … test5
.Example
Get-UnusedGroups.ps1 -CreateGroups -path "ou=testou,dc=nwtraders,dc=com" `
-numberGroups 5 -SearchBase "ou=testou,dc=nwtraders,dc=com"
Creates 5 em
pty global groups in the TestOU of nwtraders.com. Groups will
be named test1, test2 … test5. It then searches the testou in nwtraders.com
for empty groups and produces a report.
.Example
Get-UnusedGroups.ps1 -searchBase "ou=testou,dc=nwtraders,dc=com" -remove
Searches the testou in nwtraders.com for groups with no members. If these
are found they will be removed. A report is produced of groups found, but
no bogus groups are created.
.Parameter numberGroups
The number of groups to be created if the -createGroups switch is used
.Parameter CreateGroups
Causes script to create groups. Must be used with numberGroups and path.
.Parameter path
Location of bogus groups to be created. Must be used with numberGroups
and CreateGroups.
.Parameter SearchBase
Location to search for empty groups.
.Parameter remove
Causes script to remove empty groups. Uses SearchBase parameter.
.Inputs
[psobject]
.Outputs
[psobject]
.Notes
NAME: Get-UnusedGroups.ps1
AUTHOR: Ed Wilson
AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
LASTEDIT: 7/19/2010
HSG: hsg-07-22-10
KEYWORDS: Active Directory, Groups
.Link
Http://www.ScriptingGuys.com
Http://www.bit.ly/HSGBlog
#>
#Requires -Version 2.0
Param(
[int16]$numberGroups,
[switch]$CreateGroups,
[string]$path,
[string]$searchBase,
[switch]$remove
) #end param
Function Get-MyModule {
Param([string]$name)
if (-not(Get-Module -name $name)) {
if (Get-Module -ListAvailable |
Where-Object { $_.name -eq $name }) {
Import-Module -Name $name
$true
} #end if module available then import
else { $false } #module not available
} # end if not module
else { $true } #module already loaded
} #end function get-MyModule
Function New-BogusTestGroups {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[int16]$numberGroups,
[Parameter(Mandatory = $True)]
[string]$path
)
1..$numberGroups |
ForEach-Object { New-ADGroup -name "test$_" -groupScope global -path $path }
$numberGroups = $path = $null
} #end function New-BogusTestGroups
Function Get-UnusedGroups {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[string]$searchBase
)
Get-ADGroup -Filter * -Properties members, isCriticalSystemObject `
-SearchBase $searchBase |
Where-Object { ($_.members.count -eq 0 -AND !($_.IsCriticalSystemObject) `
-AND $_.DistinguishedName -notMatch ‘Exchange Security’ -AND `
$_.DistinguishedName -notMatch ‘Dns’)
}
$searchBase = $null
} #end function Get-UnusedGroups
Function Remove-UnusedGroups {
$input |
Remove-ADGroup
} #end function remove-unusedGroups
Function Format-Output {
$input |
Sort-Object -Property groupscope |
Format-Table -Property groupscope, name, distinguishedName -AutoSize -Wrap
} # end function format-output
# *** Entry point to script ***
If (-not (Get-MyModule -name "ActiveDirectory")) { exit }
if ($CreateGroups) { New-BogusTestGroups -numberGroups $numberGroups -path $path }
If ($searchBase) { Get-UnusedGroups -SearchBase $searchBase | Format-Output }
if ($remove) { Get-UnusedGroups -SearchBase $searchBase | Remove-UnusedGroups }
Function New-BogusTestGroups {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[int16]$numberGroups,
[Parameter(Mandatory = $True)]
[string]$path
)
1..$numberGroups |
ForEach-Object { New-ADGroup -name "test$_" -groupScope global -path $path }
$numberGroups = $path = $null
} #end function New-BogusTestGroups
Function Get-UnusedGroups {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[string]$searchBase
)
Get-ADGroup -Filter * -Properties members, isCriticalSystemObject `
-SearchBase $searchBase |
Where-Object { ($_.members.count -eq 0 -AND !($_.IsCriticalSystemObject) `
-AND $_.DistinguishedName -notMatch ‘Exchange Security’ -AND `
$_.DistinguishedName -notMatch ‘Dns’)
}
$searchBase = $null
} #end function Get-UnusedGroups
If ($searchBase) { Get-UnusedGroups -SearchBase $searchBase | Format-Output }
Function Format-Output {
$input |
Sort-Object -Property groupscope |
Format-Table -Property groupscope, name, distinguishedName -AutoSize -Wrap
} # end function format-output
Function Remove-UnusedGroups {
$input |
Remove-ADGroup
} #end function remove-unusedGroups