Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PowerShell: Getting all local groups that are member of 'Administrators' and 'Users`

Posted on 2011-05-13
4
Medium Priority
?
787 Views
Last Modified: 2012-06-27
Hello,

I am a Powershell noob. Nevertheless, I need to collect all local groups members of 'Administrators' and 'Users'. I need to do this for a lot of servers.
Somehow my code does nog work well. I hope some one can help me.

# Author:
# Purpose : Find matching members in local groups
# 

# Local groups to search in:
$LocalGroup = "Administrators", "Users"

$MemberNames = @()

# Get servers from txt file
#$Servers = Get-Content serverlist.txt

# Uncomment for testing on local computer
$Servers = $env:computername 

Foreach ($Server in $Servers) {
            $Groups = [ADSI]"WinNT://$Server/$LocalGroup.group"
            $Members = @($Group.psbase.Invoke("Members"))
            $Members | ForEach-Object {
                         $MemberName += $_.GetType().IvokeMember("Name", 'GetProperty', $null, $_, $null)
            }
            $ChildGroups | ForEach-Object {
                        $output = "" | Select-Object Server, Group, InLocalAdmin, InUsers
                        $output.Server = $Server
                        $output.Group = $_
                        $output.InLocalAdmin = $MemberNames -contains $_
                        $output.InUsers = $MemberNames -contains $_
                        Write-Output $output
            }
}

Open in new window


This is the Output I get when I use it on the local server and don't use the server:
PS C:\temp\scripts> .\Get-LocalGroup.ps1
You cannot call a method on a null-valued expression.
At C:\temp\scripts\Get-LocalGroup.ps1:18 char:35
+     $Members = @($Group.psbase.Invoke( <<<< "Members"))

Server              Group                      InLocalAdmin             InUsers
       ------              -----                      ------------             -------
STS19010                                              False               False


PS C:\temp\scripts>

Open in new window

0
Comment
Question by:sayadi
  • 3
4 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35753685
Here is a working script. Yours seems to be cut and copied from different sources, so the script does not make a whole.
# Local groups to search in:  
$LocalGroup = "Administrators", "Users"  
  
$MemberNames = @()  
  
# Get servers from txt file  
#$Servers = Get-Content serverlist.txt  
  
# Uncomment for testing on local computer  
$Servers = $env:computername
  
$Servers | ForEach-Object {
    $server = $_
    $LocalGroup | ForEach-Object {  
        $groupstr = $_
        $Group = [ADSI]"WinNT://$Server/$Groupstr,group"  
        $Members = $Group.psbase.Invoke("Members")
        $Members | ForEach-Object {  
            $membername = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
            $membertype = $_.GetType().InvokeMember("Class", 'GetProperty', $Null, $_, $Null)
            New-Object -TypeName PSObject -Property @{
                Server = $server
                Group = $groupstr
                Membername = $membername
                Membertype = $membertype
            }
        } | ?{$_.membertype -eq "Group"}
    } | Group-Object -Property membername | ForEach-Object {
        New-Object -TypeName PSObject -Property @{
            Server = $_.group[0].server
            Group = $_.name
            InLocalAdmin = $_.group[0].group -eq "Administrators" -or ($_.group.count -eq 2 -and $_.group[1].group -eq "Administrators")
            InUsers = $_.group[0].group -eq "Users" -or ($_.group.count -eq 2 -and $_.group[1].group -eq "Users")
        }
    }
} | Select-Object server, group, InLocalAdmin, InUsers

Open in new window

0
 
LVL 2

Accepted Solution

by:
sayadi earned 0 total points
ID: 35753797
This is the awnser:
gc C:\servers.txt | % { ([ADSI]"WinNT://$_/Administrators,group").psbase.Invoke("Members") | % {$_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)} }

Needs some editing
0
 
LVL 2

Author Comment

by:sayadi
ID: 35768416
@soostibi
Thank you so much
0
 
LVL 2

Author Closing Comment

by:sayadi
ID: 35783732
This solves the issue
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question