Pull local accounts with elevated access from multiple servers

Hi EE

Can someone help me modify the script below or if someone has a script that will do the same ..
The script pulls all the local accounts from a list of servers ... can someone help me modify this so it
only pulls the local accounts that are member of the Administrators group ?
or it add a column that shows which local group the account is a member of ?

function get-localusers {
    param(
    [Parameter(Mandatory=$true,valuefrompipeline=$true)]
    [string]$strComputer)
    begin {}
    Process {
      $Select = "Name","Class" | %{  
      Invoke-Expression "@{n='$_';e={ `$_.GetType().InvokeMember('$_', 'GetProperty', `$Null, `$_, `$Null) }}"  
       }
       If (Test-Connection $strComputer -Count 2 -Quiet){
       $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
       $Users = $computer.psbase.children | ? {$_.psbase.SchemaClassName -eq "User"}
       foreach ($User in $Users) {
	   $stat = Switch ($User.UserFlags.Value -band "2"){"2"{"Disabled"};"0"{"Enabled"}}
	   $User | Select @{N="ComputerName";E={$strComputer}},@{N="User";E={$_.Name}},@{N="Status";E={$stat}},@{N="Description";E={$_.Description}},Class
          }
        }
      Else {
          "" | Select @{N="ComputerName";E={$strComputer}},@{N="User";E={"Not able to Ping"}},Status,Description,Class
        }
      }
end {}
}
Get-Content "C:\PS\Servers.txt" | get-localusers | Select ComputerName,User,Status,Description,Class | Export-Csv "C:\PS\Servers_details_$((get-date).toString('MM-dd-yyyy')).csv" -NTI

Open in new window

LVL 2
MilesLoganAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zac HarrisSystems Administrator Commented:
I modified the script... this should work for you:

function get-localusers {
        param(
    [Parameter(Mandatory=$true,valuefrompipeline=$true)]
    [string]$strComputer)
    begin {}
    Process {
	$Select = "Name","Class","Parent" | %{  
  	Invoke-Expression "@{n='$_';e={ `$_.GetType().InvokeMember('$_', 'GetProperty', `$Null, `$_, `$Null) }}"  
	}
      If (Test-Connection $strComputer -Count 2 -Quiet){
        $adminlist =""
        $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
        $AdminGroup = $computer.psbase.children.find("Administrators")
        $Adminmembers= $AdminGroup.psbase.invoke("Members") | Select $Select
	        foreach ($admin in $Adminmembers) {
	        $admin | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"$(($_.parent) -replace "WinNT://")\$($_.Name)"}},Class,Parent
			}
		}
      Else {
		"" | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"Not able to Ping"}},Class,Parent
        }
     }
end {}
}

Get-Content "C:\ps\Servers.txt" | get-localusers | Select ComputerName,Administrators,Class,Parent | Export-Csv "C:\ps\Results\LocalAdminGroups_$((get-date).toString('MM-dd-yyyy')).csv" -NTI

Open in new window

MilesLoganAuthor Commented:
Hi Zac

Thanks .. but this now pulls all groups and accounts, including Domain accounts/groups in the Local Administrators group. I want to keep the same output format as the current script .

The current script pulls all local accounts, but I need it to only pull local accounts in the local Administrators group or pull all local accounts but tag somehow local accounts that are members
of the Administrators group.
MilesLoganAuthor Commented:
Anyone ??
AmitIT ArchitectCommented:
You can use MS tools also. Where you can use psexec and combine it with net localgroup command.

Something like this:

psexec @file -u username -p password net localgroup Administrators > admin.txt

Note:
@file: Enter server names.
Use domain admin account as username.

Download PSTools from here:

https://technet.microsoft.com/en-in/sysinternals/bb897553.aspx

Let me know, if that solves your purpose.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Hi Amit , thank you for looking into this ..  this helped a lot !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.