Pages

Friday, February 3, 2017

Local Group members from Remote Computers

Fetch list of users from Local Group of Remote computers

Here is a simple PowerShell Script which can be used to fetch all the users who have either Administrative access on any server or are member of any specific Local Group on those servers.

############################################################################################################
# Read List of Server Names from the list input.txt
# Performs a Ping Test first before trying to fetch users from remote computers
# to ensure it attempts only reachable computers.
# Fetch members of local group on every server.
# The Local Group name can be modified according to the need like "Administrators" or "Remote Desktop Users"
############################################################################################################
 

Get-Content "input.txt" |foreach-object {
$Content = $_
# Loop only executed when ping is successful
if (test-connection -computername $Content -count 1 -quiet)
{
#Fetching details from servers
    ([ADSI]"WinNT://$_/Administrators").Members() | Select-Object `
        @{n='Computername';e={ $Content }},
        @{n='User/Group Name';e={ $_.GetType().InvokeMember('Name', 'GetProperty', $Null, $_, $Null) }},
        @{n='ADSPath';e={ $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) }},
        @{n='Type';e={ $_.GetType().InvokeMember('class', 'GetProperty', $Null, $_, $Null) }}
    }}| export-csv Local_Admins.csv –notypeinformation  


Copy the code above to a file and save that file as Get-LocalGroupMembers.ps1, Create another file named input.txt and put in all the servers in there (one server at one line), Save both of these files to same location and execute the script from PowerShell Console.

Points to consider:
*The user who is executing this script must have administrative rights on all the remote computers listed in input.txt.
* Name of Group can be changed according to the need to find out members of any local group on a remote computer like.. "Administrators" or "Remote Desktop Users" and so on..

 

1 comment: