-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersAndAccounts.psm1
More file actions
441 lines (356 loc) · 12.3 KB
/
UsersAndAccounts.psm1
File metadata and controls
441 lines (356 loc) · 12.3 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#Requires -Version 5.1
Using module .\Logging.psm1
Using module .\Scope.psm1
Using module .\Exit.psm1
#region - Caching
$Script:InitialisedAllGroups = $False;
$Script:CachedGroups = @{};
$Script:InitialisedAllUsers = $False;
$Script:CachedUsers = @{};
#endregion
#region - Private Functions
function Local:Get-ObjectByInputOrName(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ $_ -is [String] -or $_ -is [ADSI] })]
[Object]$InputObject,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$SchemaClassName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ScriptBlock]$GetByName
) {
begin {
Enter-Scope -ArgumentFormatter @{
InputObject = { "$($_.Name) of type $($_.SchemaClassName)" };
};
}
end { Exit-Scope -ReturnValue $Local:Value; }
process {
if ($InputObject -is [String] -and $InputObject -eq '') {
Write-Error 'An empty string was supplied, this is not a valid object.' -Category InvalidArgument;
}
if ($InputObject -is [String]) {
[ADSI]$Local:Value = $GetByName.InvokeReturnAsIs();
}
elseif ($InputObject.SchemaClassName -ne $SchemaClassName) {
Write-Host "$($InputObject.SchemaClassName)"
Write-Error "The supplied object is not a $SchemaClassName." -TargetObject $InputObject -Category InvalidArgument;
}
else {
[ADSI]$Local:Value = $InputObject;
}
return $Local:Value;
}
}
function Get-GroupByInputOrName([Object]$InputObject) {
return Get-ObjectByInputOrName -InputObject $InputObject -SchemaClassName 'Group' -GetByName { Get-Group $InputObject; };
}
function Get-UserByInputOrName([Object]$InputObject) {
return Get-ObjectByInputOrName -InputObject $InputObject -SchemaClassName 'User' -GetByName {
if (-not ($InputObject.Contains('/'))) {
$InputObject = "$env:COMPUTERNAME/$InputObject";
}
Get-User $InputObject;
};
}
#endregion
#region - Group Functions
<#
.SYNOPSIS
Gets the groups on the local machine, returning a list of ADSI objects.
.DESCRIPTION
This function will return a list of groups on the local machine.
If a name is specified then only the group with that name will be returned.
.OUTPUTS
[ADSI[]] - A list of ADSI objects representing the groups on the local machine.
.PARAMETER Name
The name of the group to retrieve, if not specified all groups will be returned.
.EXAMPLE
This will return the Administrators group.
```
Get-Group -Name 'Administrators';
```
.EXAMPLE
This will return all groups on the local machine.
```
Get-Group;
```
.NOTES
This function is designed to work with the ADSI provider for the local machine.
It will not work with remote machines or other providers.
#>
function Get-Group(
[Parameter(HelpMessage = 'The name of the group to retrieve, if not specified all groups will be returned.')]
[ValidateNotNullOrEmpty()]
[String]$Name
) {
begin { Enter-Scope; }
end { Exit-Scope -ReturnValue $Local:Value; }
process {
if (-not $Name) {
Invoke-Debug 'Getting all groups...'
if (-not $Script:InitialisedAllGroups) {
Invoke-Debug 'Initialising all groups...';
$Script:CachedGroups;
[ADSI]$Local:Groups = [ADSI]"WinNT://$env:COMPUTERNAME";
$Local:Groups.Children | Where-Object { $_.SchemaClassName -eq 'Group' } | ForEach-Object { $Script:CachedGroups[$_.Name] = $_; };
$Script:InitialisedAllGroups = $True;
}
$Local:Value = $Script:CachedGroups.Values;
}
else {
Invoke-Debug "Getting group $Name...";
if (-not $Script:InitialisedAllGroups -or -not $Script:CachedGroups[$Name]) {
[ADSI]$Local:Group = [ADSI]"WinNT://$env:COMPUTERNAME/$Name,group";
$Script:CachedGroups[$Name] = $Local:Group;
}
$Local:Value = $Script:CachedGroups[$Name];
}
return $Local:Value;
}
}
<#
.SYNOPSIS
Gets the members of a group.
.DESCRIPTION
This function will return a list of members of a group.
If a group is specified then only the members of that group will be returned.
.OUTPUTS
[ADSI[]] - A list of ADSI objects representing the members of the group.
.PARAMETER Group
The group to retrieve the members of.
.EXAMPLE
This will return the members of the Administrators group.
```
Get-MembersOfGroup -Group (Get-Group -Name 'Administrators');
```
.NOTES
This function is designed to work with the ADSI provider for the local machine.
It will not work with remote machines or other providers.
#>
function Get-MembersOfGroup(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object]$Group
) {
begin { Enter-Scope; }
end { Exit-Scope -ReturnValue $Local:Members; }
process {
[ADSI]$Local:Group = Get-GroupByInputOrName -InputObject:$Group;
$Group.Invoke('Members') `
| ForEach-Object { [ADSI]$_ } `
| Where-Object {
if ($_.Parent.Length -gt 8) {
$_.Parent.Substring(8) -ne 'NT AUTHORITY'
}
else {
# This is a in-built user, skip it.
$False
}
};
}
}
<#
.SYNOPSIS
Tests if a user is a member of a group.
.DESCRIPTION
This function will test if a user is a member of a group.
.OUTPUTS
[Boolean] - $True if the user is a member of the group, otherwise $False.
.PARAMETER Group
The group to test if the user is a member of.
.PARAMETER User
The user to test if they are a member of the group.
.EXAMPLE
This will test if the user localadmin is a member of the Administrators group.
```
Test-MemberOfGroup -Group (Get-Group -Name 'Administrators') -User 'localadmin';
```
.NOTES
This function is designed to work with the ADSI provider for the local machine.
It will not work with remote machines or other providers.
#>
function Test-MemberOfGroup(
[Parameter(Mandatory)]
[Object]$Group,
[Parameter(Mandatory)]
[Object]$User
) {
begin {
Enter-Scope -ArgumentFormatter @{
Group = { $_.Name + $_.SchemaClassName };
User = { $_.Name + $_.SchemaClassName };
};
}
end { Exit-Scope -ReturnValue $Local:User; }
process {
Invoke-Debug 'Getting group';
[ADSI]$Local:Group = Get-GroupByInputOrName -InputObject $Group;
Invoke-Debug 'Getting user';
[ADSI]$Local:User = Get-UserByInputOrName -InputObject $User;
Invoke-Debug 'Testing if user is a member of group...';
return $Local:Group.Invoke('IsMember', $Local:User.Path);
}
}
<#
.SYNOPSIS
Adds a user to a group.
.DESCRIPTION
This function will add a user to a group.
.OUTPUTS
[Boolean] - $True if the user was added to the group, otherwise $False.
.PARAMETER Group
The group to add the user to.
.PARAMETER User
The user to add to the group.
.EXAMPLE
This will add the user localadmin to the Administrators group.
```
Add-MemberToGroup -Group (Get-Group -Name 'Administrators') -User 'localadmin';
```
.NOTES
This function is designed to work with the ADSI provider for the local machine.
It will not work with remote machines or other providers.
#>
function Add-MemberToGroup(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object]$Group,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object]$User
) {
begin { Enter-Scope; }
end { Exit-Scope; }
process {
[ADSI]$Local:Group = Get-GroupByInputOrName -InputObject $Group;
[ADSI]$Local:User = Get-UserByInputOrName -InputObject $User;
if (Test-MemberOfGroup -Group $Local:Group -User $Local:User) {
Invoke-Verbose "User $User is already a member of group $Group.";
return $False;
}
Invoke-Verbose "Adding user $Name to group $Group...";
$Local:Group.Invoke('Add', $Local:User.Path);
return $True;
}
}
<#
.SYNOPSIS
Removes a user from a group.
.DESCRIPTION
This function will remove a user from a group.
.OUTPUTS
#>
function Remove-MemberFromGroup(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object]$Group,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object]$Member
) {
begin { Enter-Scope; }
end { Exit-Scope; }
process {
[ADSI]$Local:Group = Get-GroupByInputOrName -InputObject $Group;
[ADSI]$Local:User = Get-UserByInputOrName -InputObject $Member;
if (-not (Test-MemberOfGroup -Group $Local:Group -User $Local:User)) {
Invoke-Verbose "User $Member is not a member of group $Group.";
return $False;
}
Invoke-Verbose "Removing user $Name from group $Group...";
$Local:Group.Invoke('Remove', $Local:User.Path);
return $True;
}
}
#endregion
#region - User Functions
# FIXME
function Get-User(
[Parameter(HelpMessage = 'The name of the user to retrieve, if not specified all users will be returned.')]
[ValidateNotNullOrEmpty()]
[String]$Name
) {
begin { Enter-Scope; }
# end { Exit-Scope -ReturnValue $Local:Value; }
process {
if (-not $Name) {
if (-not $Script:InitialisedAllUsers) {
$Script:CachedUsers = @{};
[ADSI]$Local:Users = [ADSI]"WinNT://$env:COMPUTERNAME";
$Local:Users.Children | Where-Object { $_.SchemaClassName -eq 'User' } | ForEach-Object { $Script:CachedUsers[$_.Name] = $_; };
$Script:InitialisedAllUsers = $True;
}
$Local:Value = $Script:CachedUsers;
}
else {
$null = Get-User;
if (-not $Script:InitialisedAllUsers -or -not $Script:CachedUsers[$Name]) {
$Script:CachedUsers = @{};
[ADSI]$Local:User = [ADSI]"WinNT://$Name,user";
$Script:CachedUsers[$Name] = $Local:User;
}
$Local:Value = $Script:CachedUsers[$Name];
$Global:CachedUsers = $Script:CachedUsers;
}
return $Local:Value;
}
}
function Get-UserGroups(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object]$User
) {
begin { Enter-Scope; }
end { Exit-Scope -ReturnValue $Local:UserGroups; }
process {
[ADSI]$Local:User = Get-UserByInputOrName -InputObject $User;
[String]$Local:Domain = $Local:User.Path.Split('/')[0];
[String]$Local:Username = $Local:User.Path.Split('/')[1];
Get-WmiObject -Class Win32_GroupUser `
| Where-Object { $_.PartComponent -match "Domain=""$Domain"",Name=""$Username""" } `
| ForEach-Object { [WMI]$_.GroupComponent };
# [ADSI]$Local:User = Get-UserByInputOrName -InputObject $User;
# [ADSI[]]$Local:Groups = Get-Group;
# [ADSI[]]$Local:UserGroups = @();
# foreach ($Local:Group in $Local:Groups) {
# if (Test-MemberOfGroup -Group $Local:Group -User $Local:User) {
# $Local:UserGroups += $Local:Group;
# }
# }
return $Local:UserGroups;
}
}
#endregion
#region - Formatting Functions
function Format-ADSIUser(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ $_ | ForEach-Object { $_.SchemaClassName -eq 'User' } })]
[ADSI[]]$User
) {
begin { Enter-Scope; }
end { Exit-Scope -ReturnValue $Local:Value; }
process {
if ($User -is [Array] -and $User.Count -gt 1) {
$Local:Value = $User | ForEach-Object {
Format-ADSIUser -User $_;
};
return $Local:Value;
}
else {
[String]$Local:Path = $User.Path.Substring(8); # Remove the WinNT:// prefix
[String[]]$Local:PathParts = $Local:Path.Split('/');
# The username is always last followed by the domain.
[HashTable]$Local:Value = @{
Name = $Local:PathParts[$Local:PathParts.Count - 1]
Domain = $Local:PathParts[$Local:PathParts.Count - 2]
};
return $Local:Value;
}
}
}
#endregion
Export-ModuleMember -Function Get-User, Get-UserGroups, Get-Group, Get-MembersOfGroup, Test-MemberOfGroup, Add-MemberToGroup, Remove-MemberFromGroup, Format-ADSIUser, Get-GroupByInputOrName, Get-UserByInputOrName;