-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-LocalGroup-Members.ps1
57 lines (52 loc) · 1.8 KB
/
Get-LocalGroup-Members.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
Author: Stan Crider
Date: 20Mar2020
Crap:
Gets the members of specified groups from specified computers
Used for validating group memberships against desired configuration, i.e, GPO, AD groups, etc
#>
## Input variables below
$LocalGroups = "Administrators","DHCP Administrators"
$Computers = "DHCPSrvr01","DHCPSrvr02"
## Script below; do not cross
# Prep Arrays
$MemberArray = @()
$ErrorArray = @()
# Run through each computer
ForEach($Computer in $Computers){
Write-Output "Processing $Computer ..." # used for troubleshooting purposes
# Call local groups once and reuse for each group variable
$AllGroupMembers = $null
Try{
$AllGroupMembers = Get-CimInstance Win32_GroupUser -ComputerName $Computer -ErrorAction Stop
}
# Error handling, duh
Catch{
$ErrorArray += [PSCustomObject]@{
"Computer" = $Computer
"Error" = $_.Exception.Message
}
}
# Run through each group variable
ForEach($LocalGroup in $LocalGroups){
# Find matches
$GroupMembers = $AllGroupMembers | Where-Object {$_.GroupComponent.Name -eq $LocalGroup}
# Run through each match
ForEach($GroupMember in $GroupMembers){
# Run through each match object
ForEach($PartComponent in $GroupMember.PartComponent){
# Output matches to custom object
$MemberArray += [PSCustomObject]@{
"Computer" = $GroupMember.PSComputerName
"Member Of" = $GroupMember.GroupComponent.Name
"Full Name" = ("" + $PartComponent.Domain + "\" + $PartComponent.Name)
"Domain" = $PartComponent.Domain
"Name" = $PartComponent.Name
}
}
}
}
}
## Output
$MemberArray
$ErrorArray