-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-ComputerLocalGroupMembers.ps1
38 lines (31 loc) · 1.07 KB
/
Get-ComputerLocalGroupMembers.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
Param(
[parameter(Mandatory=$true)]
[String]
$ComputerName
)
$ListofGroupsandMembers = @()
#attempt to query WMI on remote server
try {
$WMIClassMembers = Get-WmiObject -class win32_groupuser -ComputerName $ComputerName -ErrorAction Stop
}
catch {
Write-Warning -Message "Problem encountered with access to WMI on $ComputerName"
Write-Warning -Message "$($_.Exception.Message)"
}
#Itereate through the WMI results and parse out groups and group members
foreach ($Member in $WMIClassMembers) {
$MemberName = ($Member.PartComponent).split(',')[1]
$MemberName = ($MemberName).split('=')[1]
$MemberName = ($MemberName).Trim('"')
$LocalGroupName = ($Member.GroupComponent).split(',')[1]
$LocalGroupName = ($LocalGroupName).split('=')[1]
$LocalGroupName = ($LocalGroupName).Trim('"')
$ListofGroupsandMembers += $Member | Select-Object (
@{ Label="Computer"; Expression={$_.PSComputerName} },
@{ Label="LocalGroupName"; Expression={$LocalGroupName} },
@{ Label="MemberName"; Expression={$MemberName} }
)
}
#print the list of groups and group members
#test
$ListofGroupsandMembers