PoshCode Archive  Artifact [5bd615c2ea]

Artifact 5bd615c2ea802402c6902782f7e1b826d2a34becfe513b95554d46b29d3832fe:

  • File Find-Local-Group-Members.ps1 — part of check-in [7d9e70c212] at 2018-06-10 13:30:50 on branch trunk — Find matching members in a local group (user: Hal Rottenberg size: 1468)

# encoding: ascii
# api: powershell
# title: Find Local Group Members
# description: Find matching members in a local group
# version: 0.1
# author: Hal Rottenberg
# license: CC0
# x-poshcode-id: 3771
# x-archived: 2014-03-03T19:09:02
# x-published: 2014-11-19T12:21:00
#
# Updated to clear MemberNames between servers
#
# Author: Hal Rottenberg
# Purpose: Find matching members in a local group
# Used tip from RichS here: http://powershellcommunity.org/Forums/tabid/54/view/topic/postid/1528/Default.aspx

# Change these two to suit your needs
$ChildGroups = "Domain Admins", "Group Two"
$LocalGroup = "Administrators"

$MemberNames = @()
# uncomment this line to grab list of computers from a file
# $Servers = Get-Content serverlist.txt
$Servers = $env:computername # for testing on local computer
foreach ( $Server in $Servers ) {
# Put $MemberNames = @() here to clear out the MemberNames variable between servers, otherwise we start to get incorrect results
        $MemberNames = @()
	$Group= [ADSI]"WinNT://$Server/$LocalGroup,group"
	$Members = @($Group.psbase.Invoke("Members"))
	$Members | ForEach-Object {
		$MemberNames += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
	} 
	$ChildGroups | ForEach-Object {
		$output = "" | Select-Object Server, Group, InLocalAdmin
		$output.Server = $Server
		$output.Group = $_
		$output.InLocalAdmin = $MemberNames -contains $_
		Write-Output $output
	}
}