Using Power shell to query administrator group on server.

I have been requested to provide a list of "Who is an admin on server X"  

Can power shell be used to query ldap to where I can specify: Tell me who on server X is a member of the administrator group.  Then that information is dumped to a .txt or .csv where I can then make a nice and pretty report.  

If so what does that query look like, my knowledge of power shell is that I know it is out there and it can do a lot of great things when properly employed.  If not power shell is there another way to go about this?

The servers I need to do this for live in our domain and on an domain hosted by at&t.  Can this all be done from one machine?

Thanks for your help.
Who is Participating?
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.

Neil RussellTechnical Development LeadCommented:
Are you talking about LOCAL ADMINISTRATORS group?

IF SO have a look here for a handy script

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
NMHGADMAuthor Commented:
Mike KlineCommented:
There is no query against AD to find that info.  You would need to remote to each machine to get it   This is where restricted groups is nice because you can define local admins via GPO.

I'll add a few scripting zones to the question.


Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

NMHGADMAuthor Commented:
The script neilsr linked is really helpful.  

What tweaking would it take to have the script dump the results to a .txt or .csv?

If you like PowerShell and you have Quest's Active Server Roles fo Active Directory PowerShell snapin, then you can use this.
$strComputer = "remoteserver"

$computer = [ADSI]("WinNT://" + $strComputer + ",computer") 
$Group = $computer.psbase.children.find("Administrators") 
$members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("ADSPath", 'GetProperty', $null, $_, $null)} 
$members = $members | Select-Object -Property @{n="domain"; e={($_ -split "/+")[-2]}}, 
                @{n="user"; e={($_ -split "/+")[-1]}}, 
                @{n="fullname"; e={"$(($_ -split '/+')[-2])\$(($_ -split '/+')[-1])"}}
$members | %{
        $domain = $_.domain
        if($_.domain -eq $strcomputer) {$_.fullname}
            Get-QADObject $_.fullname | %{
                if($_.type -eq "group"){ Get-QADGroupMember $_ -Indirect | %{"$domain\$($_.samaccountname)"}}

Open in new window

Here is a simple function (and usage saving data to file) for getting administrator group members based on machine name:

function GetMachineAdministrators($machineName)
    ([ADSI]"WinNT://$machineName/Administrators").invoke("Members") | foreach {$_.getType().InvokeMember("ADSPath","GetProperty",$null,$_,$null)}

GetMachineAdministrators MyLaptop | Out-File "C:\Downloads\test.csv"

Open in new window


Yes, this is the base of the solution, but it gives only the direct members of the local Administrators group, most times they are also groups, who also have nested groups in them. My solution recursively list all single user members of all groups that are members of this local group.
Chris DentPowerShell DeveloperCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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
Active Directory

From novice to tech pro — start learning today.