header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Local Group and Membership Query and Formatting
Last Post 29 Jun 2010 10:44 AM by GWHowarth88. 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
TooTallUser is Offline
New Member
New Member
Posts:3
Avatar

--
28 Jun 2010 08:51 PM  

I have to write a script to query Local Groups and their Memberships on a list of servers.  The output format ends up being Group name then members.  I would like the Server Name, Group Name and then members returned on one line in three different columns.  Below is the script I am working with.  The other option would be to output the results to a Access Database if possible.  Any help is greatly appreciated.

$serverlist = gc timserverlist.txt

foreach ($server in $serverlist) {
 $computer = [ADSI]"WinNT://$server,computer"
 write-host $server

 $computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach {
      write-host $_.name
      $group =[ADSI]$_.psbase.Path
      $group.psbase.Invoke("Members") | foreach {$_.GetType().InvokeMember("Name", 'GetProperty',  

$null, $_, $null)}
     write-host
 }
}

 

Cruisader03User is Offline
Basic Member
Basic Member
Posts:245
Avatar

--
28 Jun 2010 09:27 PM  
Create an object with your member list, then create your object including the total list instead of a single line for each member:
   if($list -ne $null)
        {
        $members = $list | ForEach-Object
            {
            $_.trim().ToUpper()
            }
        $obj=New-Object PSObject
        $obj | Add-Member Noteproperty -Name "ServerName" -Value (($computer).trim()).ToUpper()
        $obj | Add-Member Noteproperty -Name "GroupName" -Value (($result.name).trim()).ToUpper()
        $obj | Add-Member Noteproperty -Name "Member" -Value $members
        $ArrayGroup += $obj
        }

Output to a CSV.  Doing out-file you'll end up with truncated data:
$ArrayGroup | select ServerName, GroupName, Member | Export-Csv "$pathFolder\CRLLocalGroups.csv"
When at first you don't succeed Step-Into

http://theposherlife.blogspot.com
http://www.jandctravels.com

TooTallUser is Offline
New Member
New Member
Posts:3
Avatar

--
28 Jun 2010 09:44 PM  

Thank You Cruisader. I updated post per recommendation of a co-worker.  I am reposting script with your suggested updates.  I am now receiving

cmdlet ForEach-Object at command pipeline postion 1
Supply values for the following parameters:
Process[0]:
$_.trim().ToUpper()

Any suggestions?


########################
#Functions
########################
$arrExclude = "NT AUTHORITY\LocalService",
            "NT AUTHORITY\Local Service",
            "NT AUTHORITY\NETWORK SERVICE",
            "NT AUTHORITY\NetworkService",
            "LocalSystem",
            ".\ASPNET"

function checkExclusions([string]$strval)
    {
    foreach ($val in $arrExclude)
        {if ($val.ToLower() -eq $strval){return $true}  }
    return $false
    }

function Ping (  [string] $strComputer )
{
  $timeout=120;
  trap { continue; }

  $ping = new-object System.Net.NetworkInformation.Ping
  $reply = new-object System.Net.NetworkInformation.PingReply

  $reply = $ping.Send($strComputer, $timeout);
  if( $reply.Status -eq "Success"  )
  {
     return $true;
  }
  return $false;
}

########################
#Script
########################
$pathFolder = "D:\ServerBiYearlyScan\CRL\Group"
$computersList = get-content "$pathFolder\CRLServerList.txt"
$ArrayUser = @()
$ArrayGroup = @()
$ArrayKey = @()
$ArrayService = @()
$ArrayShare = @()
$ArrayAccess = @()

foreach($computer in $computersList)
{

#################################################################################################
$retPing = Ping $computer
if($retPing -eq $true)
    {
    #Disabling the error on the screen
    $errorActionPreference="SilentlyContinue"
    $testAccss = get-wmiobject Win32_OperatingSystem -computername $computer -ErrorVariable ERR
        If($ERR)
            {$Access = $false}
        else{$Access = $true}
    }
else{$Access = $false}

if($Access -eq $false)
    {
    #Srv not ping or denied
    $obj=New-Object PSObject
    $obj | Add-Member Noteproperty -Name "ServerName" -Value (($computer).trim()).ToUpper()
    $obj | Add-Member Noteproperty -Name "PING" -Value $retPing
    $obj | Add-Member Noteproperty -Name "ACCESS" -Value $Access
    $ArrayAccess += $obj
    }
else{
    #Working on it

#################################################################################################

$namespace = "root\CIMV2"
$results = Get-WmiObject -class Win32_Group -computername $computer -namespace $namespace -filter "localaccount=true"
foreach($result in $results)
    {
  
    $GroupName = $result.name
    $group =[ADSI]"WinNT://$computer/$GroupName"
    $members = @($group.psbase.Invoke("Members"))
    $list = $members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
     if($list -ne $null)
        {
        $members = $list | ForEach-Object
            {
            $_.trim().ToUpper()
            }
        $obj=New-Object PSObject
        $obj | Add-Member Noteproperty -Name "ServerName" -Value (($computer).trim()).ToUpper()
        $obj | Add-Member Noteproperty -Name "GroupName" -Value (($result.name).trim()).ToUpper()
        $obj | Add-Member Noteproperty -Name "Member" -Value $members
        $ArrayGroup += $obj
        }

    }

#################################################################################################

    }

}
$ArrayGroup | select ServerName, GroupName, Member | Export-Csv "$pathFolder\CRLLocalGroups.csv"

 

Cruisader03User is Offline
Basic Member
Basic Member
Posts:245
Avatar

--
28 Jun 2010 09:52 PM  
actually, it looks like $list is what you're looking for and don't need to do anything else

$obj | Add-Member Noteproperty -Name "Member" -Value $list
When at first you don't succeed Step-Into

http://theposherlife.blogspot.com
http://www.jandctravels.com

GWHowarth88User is Offline
Basic Member
Basic Member
Posts:348
Avatar

--
29 Jun 2010 10:44 AM  
Just a suggestion: if you're only testing whether or not you get a reply from a computer, then the Ping function isn't necessary as you can use the Test-Connection cmdlet.

Test-Connection -ComputerName $computer -Count 1 -Delay 1 -Quiet

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer