-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-UsersAndGroups.ps1
More file actions
235 lines (213 loc) · 10 KB
/
Get-UsersAndGroups.ps1
File metadata and controls
235 lines (213 loc) · 10 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Get-UsersAndGroups.ps1
# Written by Bill Stewart (bstewart@iname.com)
#requires -version 2
<#
.SYNOPSIS
Retreves users, and group membership for each user, from Active Directory.
.DESCRIPTION
Retreves users, and group membership for each user, from Active Directory. Note that each user's primary group is included in the output, and caching is used to improve performance.
.PARAMETER SearchLocation
Distinnguished name (DN) of where to begin searching for user accounts; e.g. "OU=Information Technology,DC=fabrikam,DC=com". If you omit this parameter, the default is the current domain (e.g., "DC=fabrikam,DC=com").
.PARAMETER SearchScope
Specifies the scope for the Active Directory search. Must be one of the following values: Base (Limit the search to the base object, not used), OneLevel (Searches the immediate child objects of the base object), or Subtree (Searches the whole subtree, including the base object and all its child objects). The default value is Subtree. To search only a location but not its children, specify OneLevel.
.OUTPUTS
PSObjects containing the following properties:
DN The user's distinguished name
CN The user's common name
UserName The user's logon name
Disabled True if the user is disabled; false otherwise
Group The groups the user is a member of (one object per group)
#>
[CmdletBinding()]
param(
[parameter(Position=0,ValueFromPipeline=$TRUE)]
[String[]] $SearchLocation="",
[String][ValidateSet("Base","OneLevel","Subtree")] $SearchScope="Subtree"
)
begin {
$ADS_NAME_INITTYPE_GC = 3
$ADS_SETTYPE_DN = 4
$ADS_NAME_TYPE_1779 = 1
$ADS_NAME_TYPE_NT4 = 3
$ADS_UF_ACCOUNTDISABLE = 2
# Assume pipeline input if SearchLocation is unbound and doesn't exist.
$PIPELINEINPUT = (-not $PSBOUNDPARAMETERS.ContainsKey("SearchLocation")) -and (-not $SearchLocation)
# If -SearchLocation is a single-element array containing an emty string
# (i.e., -SearchLocation not specified and no pipeline), then populate with
# distinguished name of current domain. In this case, input is not coming
# from the pipeline.
if (($SearchLocation.Count -eq 1) -and ($SearchLocation[0] -eq "")) {
try {
$SearchLocation[0] = ([ADSI] "").distinguishedname[0]
}
catch [System.Management.Automation.RuntimeException] {
throw "Unable to retrieve the distinguished name for the current domain."
}
$PIPELINEINPUT = $FALSE
}
# These hash tables cache primary groups and group names for performance.
$PrimaryGroups = @{}
$Groups = @{}
# Create and initialize a NameTranslate object. If it fails, throw an error.
$NameTranslate = new-object -comobject "NameTranslate"
try {
[Void] $NameTranslate.GetType().InvokeMember("Init", "InvokeMethod", $NULL, $NameTranslate, ($ADS_NAME_INITTYPE_GC, $NULL))
}
catch [System.Management.Automation.MethodInvocationException] {
throw $_
}
# Create a Pathname object.
$Pathname = new-object -comobject "Pathname"
# Returns the last two elements of the DN using the Pathname object.
function get-rootname([String] $dn) {
[Void] $Pathname.GetType().InvokeMember("Set", "InvokeMethod", $NULL, $Pathname, ($dn, $ADS_SETTYPE_DN))
$numElements = $Pathname.GetType().InvokeMember("GetNumElements", "InvokeMethod", $NULL, $Pathname, $NULL)
$rootName = ""
($numElements - 2)..($numElements - 1) | foreach-object {
$element = $Pathname.GetType().InvokeMember("GetElement", "InvokeMethod", $NULL, $Pathname, $_)
if ($rootName -eq "") {
$rootName = $element
}
else {
$rootName += ",$element"
}
}
$rootName
}
# Returns an "escaped" copy of the specified DN using the Pathname object.
function get-escaped([String] $dn) {
[Void] $Pathname.GetType().InvokeMember("Set", "InvokeMethod", $NULL, $Pathname, ($dn, $ADS_SETTYPE_DN))
$numElements = $Pathname.GetType().InvokeMember("GetNumElements", "InvokeMethod", $NULL, $Pathname, $NULL)
$escapedDN = ""
for ($n = 0; $n -lt $numElements; $n++) {
$element = $Pathname.GetType().InvokeMember("GetElement", "InvokeMethod", $NULL, $Pathname, $n)
$escapedElement = $Pathname.GetType().InvokeMember("GetEscapedElement", "InvokeMethod", $NULL, $Pathname, (0, $element))
if ($escapedDN -eq "") {
$escapedDN = $escapedElement
}
else {
$escapedDN += ",$escapedElement"
}
}
$escapedDN
}
# Return the primary group name for a user. Algorithm taken from
# http://support.microsoft.com/kb/321360
function get-primarygroupname([String] $dn) {
# Pass DN of user to NameTranslate object.
[Void] $NameTranslate.GetType().InvokeMember("Set", "InvokeMethod", $NULL, $NameTranslate, ($ADS_NAME_TYPE_1779, $dn))
# Get NT4-style name of user from NameTranslate object.
$nt4Name = $NameTranslate.GetType().InvokeMember("Get", "InvokeMethod", $NULL, $NameTranslate, $ADS_NAME_TYPE_NT4)
# Bind to user using ADSI's WinNT provider and get primary group ID.
$user = [ADSI] "WinNT://$($nt4Name.Replace('\', '/')),User"
$primaryGroupID = $user.primaryGroupID[0]
# Retrieve user's groups (primary group is included using WinNT).
$groupNames = $user.Groups() | foreach-object {
$_.GetType().InvokeMember("Name", "GetProperty", $NULL, $_, $NULL)
}
# Query string is sAMAccountName attribute for each group.
$queryFilter = "(|"
$groupNames | foreach-object { $queryFilter += "(sAMAccountName=$($_))" }
$queryFilter += ")"
# Build a DirectorySearcher object.
$searchRootDN = get-escaped (get-rootname $dn)
$searcher = [ADSISearcher] $queryFilter
$searcher.SearchRoot = [ADSI] "LDAP://$searchRootDN"
$searcher.PageSize = 128
$searcher.SearchScope = "Subtree"
[Void] $searcher.PropertiesToLoad.Add("samaccountname")
[Void] $searcher.PropertiesToLoad.Add("primarygrouptoken")
# Find the group whose primaryGroupToken attribute matches user's
# primaryGroupID attribute.
foreach ($searchResult in $searcher.FindAll()) {
$properties = $searchResult.Properties
if ($properties["primarygrouptoken"][0] -eq $primaryGroupID) {
$groupName = $properties["samaccountname"][0]
return $groupName
}
}
}
# Return a DN's sAMAccount name based on the distinguished name.
function get-samaccountname([String] $dn) {
# Pass DN of group to NameTranslate object.
[Void] $NameTranslate.GetType().InvokeMember("Set", "InvokeMethod", $NULL, $NameTranslate, ($ADS_NAME_TYPE_1779, $dn))
# Return the NT4-style name of the group without the domain name.
$nt4Name = $NameTranslate.GetType().InvokeMember("Get", "InvokeMethod", $NULL, $NameTranslate, $ADS_NAME_TYPE_NT4)
$nt4Name.Substring($nt4Name.IndexOf("\") + 1)
}
function get-usersandgroups2($location) {
# Finds user objects.
$searcher = [ADSISearcher] "(&(objectCategory=User)(objectClass=User))"
$searcher.SearchRoot = [ADSI] "LDAP://$(get-escaped $location)"
# Setting the PageSize property prevents limiting of search results.
$searcher.PageSize = 128
$searcher.SearchScope = $SearchScope
# Specify which attributes to retrieve ([Void] prevents output).
[Void] $searcher.PropertiesToLoad.Add("distinguishedname")
[Void] $searcher.PropertiesToLoad.Add("cn")
[Void] $searcher.PropertiesToLoad.Add("samaccountname")
[Void] $searcher.PropertiesToLoad.Add("useraccountcontrol")
[Void] $searcher.PropertiesToLoad.Add("primarygroupid")
[Void] $searcher.PropertiesToLoad.Add("memberof")
# Sort results by CN attribute.
$searcher.Sort = new-object System.DirectoryServices.SortOption
$searcher.Sort.PropertyName = "cn"
foreach ($searchResult in $searcher.FindAll()) {
$properties = $searchResult.Properties
$dn = $properties["distinguishedname"][0]
write-progress "Get-UsersAndGroups" "Searching $location" -currentoperation $dn
$cn = $properties["cn"][0]
$userName = $properties["samaccountname"][0]
$disabled = ($properties["useraccountcontrol"][0] -band $ADS_UF_ACCOUNTDISABLE) -ne 0
# Create an ArrayList containing user's group memberships.
$memberOf = new-object System.Collections.ArrayList
$primaryGroupID = $properties["primarygroupid"][0]
# If primary group is already cached, add the name to the array;
# otherwise, find out the primary group name and cache it.
if ($PrimaryGroups.ContainsKey($primaryGroupID)) {
[Void] $memberOf.Add($PrimaryGroups[$primaryGroupID])
}
else {
$primaryGroupName = get-primarygroupname $dn
$PrimaryGroups.Add($primaryGroupID, $primaryGroupName)
[Void] $memberOf.Add($primaryGroupName)
}
# If the user's memberOf attribute is defined, find the group names.
if ($properties["memberof"]) {
foreach ($groupDN in $properties["memberof"]) {
# If the group name is aleady cached, add it to the array;
# otherwise, find out the group name and cache it.
if ($Groups.ContainsKey($groupDN)) {
[Void] $memberOf.Add($Groups[$groupDN])
}
else {
$groupName = get-samaccountname $groupDN
$Groups.Add($groupDN, $groupName)
[Void] $memberOf.Add($groupName)
}
}
}
# Sort the ArrayList and output one object per group.
$memberOf.Sort()
foreach ($groupName in $memberOf) {
$output = new-object PSObject
$output | add-member NoteProperty "DN" $dn
$output | add-member NoteProperty "CN" $cn
$output | add-member NoteProperty "UserName" $userName
$output | add-member NoteProperty "Disabled" $disabled
$output | add-member NoteProperty "Group" $groupName
$output
}
}
}
}
process {
if ($PIPELINEINPUT) {
get-usersandgroups2 $_
}
else {
$SearchLocation | foreach-object {
get-usersandgroups2 $_
}
}
}