Solved

Powershell script to get a local adminitrator account remotely

Posted on 2011-03-15
13
1,547 Views
Last Modified: 2012-05-11
hi,

I need to get remotely all local administrator account name.

All my server is member of my Active directory domain.

The script must check a txt file contain a list of server like this:

Server1
server2
server3
......

i need a csv file with result for every server and a name of a local administratif account.

Thanks for your help
0
Comment
Question by:cawasaki
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 14

Expert Comment

by:Shabarinath Ramadasan
ID: 35137400
Here we go..

Though result is a simple txt file.

Cheerio
Shaba
Get-QADComputer -LdapFilter "(operatingSystem=Windows Server*)" |foreach {$_.name} >serverlist.txt
$filename = Get-Date -Format yyyy-MM-dd
$filename = "LocalAdminAudit-" + $filename + ".txt"
$Result = @()
foreach($server in (gc .\serverlist.txt)){
 
$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)
 
	function getAdmins
		{$members = $Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}
		$members}
 
$Result += $server
$Result += ( getAdmins )
$Result += "------------------------------------------------------------"
}
 
$Result > $filename

Open in new window

0
 
LVL 14

Expert Comment

by:Shabarinath Ramadasan
ID: 35137411
If you have your own custom input file, delete/comment the first line.
#Get-QADComputer -LdapFilter "(operatingSystem=Windows Server*)" |foreach {$_.name} >serverlist.txt

and you can convert the result to csv by replacing the last line with GC $result |Export-csv result.csv

Cheeiro
Shaba
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35137582
Try this. This code collects from computers listed in the nameofservers.txt file the name of the built in local admin accounts (the account with the SID ending with 500).
$servers = get-content c:\namesofservers.txt

filter get-localadmin 
{
    $server = $_
    $pl = [ADSI]"WinNT://$server,computer"
    $users = $pl.children |where{$_.schemaclassname -eq "user"}
        
    $users | ?{
        $username = $_.name.tostring()
        $_.objectsid[0][-4] -eq 244 -and
        $_.objectsid[0][-3] -eq 1 -and
        $_.objectsid[0][-2] -eq 0 -and
        $_.objectsid[0][-1] -eq 0
    } | %{
        new-object -TypeName PSObject -Property @{
            NameOfLocalAdmin = $username
            Server = $server
        }
    }
}

$servers | get-localadmin | export-Csv c:\localadmins.csv -NoTypeInformation

Open in new window

0
 

Author Comment

by:cawasaki
ID: 35137722
soostibi:

Your script work, but is not export all local administrators (members of local administrators group).

Your script only export Administrator account.
0
 
LVL 16

Expert Comment

by:Learnctx
ID: 35137854
Here's an older one. If you try adapt this one to your needs.

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_24754216.html

Don't have a way to test it now, as I'm working mobile, but it should work. Might not be very pretty though :) Let me know if you would like the output refined, formatted differently, etc.
$inputfile = "c:\servers.txt"
$outfile = "c:\results.txt"

Foreach ($server in @(Get-Content $inputfile))
{
    ([ADSI]"WinNT://$server/Administrators,group").PsBase.Invoke("Members") | `
    Foreach-Object {
        "$server,$($_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null))"
    } | out-file $outfile -Append
}

Open in new window

0
 

Author Comment

by:cawasaki
ID: 35137881
shabarinath:

ok if i understand, i need to put a txt file "serverlist.txt" with this form:

name
server1
server2
.....

ok i have test this and the result is an error:

[PS] C:\Temp\PLOP>& '.\get admin account.ps1'
Exception calling "Find" with "1" argument(s): "The network path was not found.
"
At C:\Temp\PLOP\get admin account.ps1:7 char:40
+ $Group = $computer.psbase.children.find( <<<< "Administrators")
You cannot call a method on a null-valued expression.
At C:\Temp\PLOP\get admin account.ps1:10 char:35
+         {$members = $Group.psbase.invoke( <<<< "Members") | %{$_.GetType().InvokeMember("Adspath", 'GetProperty', $nu
ll, $_, $null)}
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Temp\PLOP\get admin account.ps1:18 char:20
+ $result |Export-csv  <<<< result.csv

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:cawasaki
ID: 35137992
Learnctx:

Your script dont work, i have an error:

[PS] C:\Temp\PLOP>.\plip.ps1
Unexpected token 'Name,'GetProperty',$null,$_,$null))' in expression or statement.
At C:\Temp\PLOP\plip.ps1:8 char:84
+         "$server,$($_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null))" <<<<
0
 

Author Comment

by:cawasaki
ID: 35138104
Learnctx:

Ok its work in powershell version 2.0 not in 1.0.

so its good for me.

Its possible to add a line after the result like this:

server1,membera
server1,memberb
------------------------------------------
server2,membera
server2,memberb
------------------------------------------
......
0
 
LVL 16

Expert Comment

by:Learnctx
ID: 35138307
Ah yes, I should have remembered I'm using PowerShell v2 :)

Yes, you could do this.
$inputfile = "c:\servers.txt"
$outfile = "c:\results.txt"

Foreach ($server in @(Get-Content $inputfile))
{
    ([ADSI]"WinNT://$server/Administrators,group").PsBase.Invoke("Members") | `
    Foreach-Object {
        "$server,$($_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null))"
    } | out-file $outfile -Append
    write-output "-------------------------------" | Out-File $outfile -Append
}

Open in new window

Here is another version with a little bit of error checking and exports to a CSV. Maybe more useful if you work with filters in Excel, etc.
$inputfile = "c:\servers.txt"
$outfile = "c:\results.csv"
$arrData = @()

Foreach ($server in @(Get-Content $inputfile))
{
    if ($server)
    {
        ([ADSI]"WinNT://$server/Administrators,group").PsBase.Invoke("Members") | `
        Foreach-Object {
            $objData = New-Object PSObject
            $objData | Add-Member -MemberType NoteProperty -Name "Device" -Value $server
            $objData | Add-Member -MemberType NoteProperty -Name "User" -Value $_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)
            $arrData += $objData
        }
    }
    
    trap
    {
        "$($server): ERROR"
        Continue
    }
}

$arrData | Export-Csv -NoTypeInformation $outfile

Open in new window

0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 35138459
Hope that is perfect.

$servers = get-content c:\serverlist.txt
$localadminssid = "1 2 0 0 0 0 0 5 32 0 0 0 32 2 0 0"

function getlocaladmingroup ($separator = "----------------")
{
begin {
    $currentmachine = $null
}
process{
    $machine = $_
    if($separator -and $currentmachine -and $currentmachine -ne $machine){
        New-Object -TypeName psobject -Property @{server = $separator; member = $separator}
    }
    $currentmachine = $machine
    $pl = [ADSI]"WinNT://$machine,computer"
    $group = $pl.children |where{$_.schemaclassname -eq "group" -and $_.objectsid.tostring() -eq $localadminssid}
    $g = $group.name[0]
    $m = $group.members() | %{
            $_.GetType().InvokeMember("ADSPath", 'GetProperty', $null, $_, $null)
        }
    
    $m | Select-Object -Property @{n="domain"; e={($_ -split "/+")[-2]}}, 
            @{n="user"; e={($_ -split "/+")[-1]}} | %{"$($_.domain)/$($_.user)"} | %{
        New-Object -TypeName psobject -Property @{server = $machine; member = $_}
    }
}    
}

$servers | getlocaladmingroup | export-Csv c:\localadmins.csv -NoTypeInformation

Open in new window

0
 

Author Comment

by:cawasaki
ID: 35138462
ok good.

in the second script, its possible to take blank line like this:

server1,membera
server1,memberb

server2,membera
server2,memberb
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35138497
My solution works even on non-English windows, where the local admin group have different name.
0
 

Author Comment

by:cawasaki
ID: 35138646
yes soostibi, your solution is the best for now, and your script export the account in the good format:

domain\account
or
computer\account

:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This article will help you understand what HashTables are and how to use them in PowerShell.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now