Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Finding Local Group members on servers

$
0
0

Hello,

I found this script Bob wrote and I need to tweak it alittle.

  1. I would like to read a list of servers from a text file.
  2. I would like the server name in the output.

$Computer = get-Content -Path c:\temp\!_listv2.txt # How do I feed servers from a text file.
$Computer = [ADSI]"WinNT://$Computer"
$timestamp = Get-Date -Format yyyy-MM-dd
$outfile = "c:\temp\localgroupmembers.csv"
New-Item -ItemType file -Path $outfile -Force -ErrorAction Stop
$Groups = $Computer.psbase.Children | Where {$_.psbase.schemaClassName -eq "group"}
ForEach ($Group In $Groups)
{   
 "Group: $($Group.Name)"   
 $Members = @($Group.psbase.Invoke("Members"))   
 ForEach ($Member In $Members)   
 {       
  $Class = $Member.GetType().InvokeMember("Class", 'GetProperty', $Null, $Member, $Null)
  $Name = $Member.GetType().InvokeMember("Name", 'GetProperty', $Null, $Member, $Null)
  if ($Name)
  {
   Write-Verbose "-- Member: $Name ($Class)"           
   $memberproperties = [ordered]@{
    Server = $computer.name; # I would like the server name in the output.
    Group = $($Group.Name);
    Member = "$Name ($Class)"}
   $memberObj = New-Object -TypeName psobject -Property $memberproperties
   Export-Csv -Path $outfile -InputObject $MemberObj -Append -NoTypeInformation
  }
 }
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles