nav2567
asked on
list local administrator group members.
I need a command to list members in the local administrator group for a list of servers.
Please advise.
Thanks.
Please advise.
Thanks.
You can use the PSEXEC.exe (Windows SysInternals) tool to run the net command on multiple servers:
psexec @serverfile.txt cmd "/c net localgroup administrators"
Just put a list of servernames in the serverfile.txt
If you are looking for a PowerShell script then, Similar question is answered here.. Check and let me know if you have any trouble in getting report..
https://www.experts-exchange.com/questions/28505177/Output-local-administrators-from-multiple-servers-Output-file.html
https://www.experts-exchange.com/questions/28505177/Output-local-administrators-from-multiple-servers-Output-file.html
ASKER
I tried to pipe the results to a file by putting a >>c:\result.txt at the end.
Is there a way I can display the server name before each result in the result file?
Is there a way I can display the server name before each result in the result file?
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c hostname & net localgroup Administrators >> \\computer\c$\gmembers.txt
computer would be the name of the computer you are running the command from.
ASKER
becraig, I ran that line but still do not see the hostname in the gmembers.txt file.
ASKER
By the way, you will need %% instead of %.
hmm that sounds weird:
You only need %% if you run as a bat or cmd file, from the command line % is what you need (I did not see a need to save a one liner as a cmd or bat)
You only need %% if you run as a bat or cmd file, from the command line % is what you need (I did not see a need to save a one liner as a cmd or bat)
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c echo ============================== & hostname &echo ============================== & net localgroup administrators >> \\computer\c$\gmembers.txt
Did you try the PS script? It will give you a neat csv file which you can open in excel.
ASKER
sorry, becraig, I tried but still do not see hostname is being displayed in my results.txt. Here is what I use:
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c hostname & net localgroup Administrators >> c:\results.txt
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c hostname & net localgroup Administrators >> c:\results.txt
Then we can just repeat the variable, though I also see Subsun's report is a nice csv:
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c echo %a & net localgroup Administrators >> c:\results.txt
ASKER
becraig,
It is still not good and same result. The "echo %a" result did not get piped into the results.txt file but only being shown at the command prompt when I run it.
Subsun, I tried the PS script and the result file only contains information on the last server name in the list.
It is still not good and same result. The "echo %a" result did not get piped into the results.txt file but only being shown at the command prompt when I run it.
Subsun, I tried the PS script and the result file only contains information on the last server name in the list.
Try..
function get-localusers {
param(
[Parameter(Mandatory=$true,valuefrompipeline=$true)]
[string]$strComputer
)
$Select = "Name","Class","Parent" | %{
Invoke-Expression "@{n='$_';e={ `$_.GetType().InvokeMember('$_', 'GetProperty', `$Null, `$_, `$Null) }}"
}
If (Test-Connection $strComputer -Count 2 -Quiet){
try{
$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 -SPLIT "/")[-1])\$($_.Name)"}},Class,@{N="Parent";E={($_.parent -SPLIT "/")[-1]}}
}
}catch{
"" | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"Access Denied"}},Class,Parent
}
}
Else {
"" | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"Not able to Ping"}},Class,Parent
}
}
Get-Content "C:\Powershell\Servers.txt" | %{get-localusers $_} | Select ComputerName,Administrators,Class,Parent | Export-Csv "C:\Powershell\LocalAdm$((get-date).toString('MM-dd-yyyy')).csv" -NTI
Lol my bad
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c echo %a \\%a\c$\result.txt & net localgroup Administrators >>
\\%a\c$\result.txt
I suggest saving to the computer you're running from. \\%a\c$\result.txt
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c echo %a \\%a\c$\result.txt & net localgroup Administrators >>
\\%a\c$\result.txt
I suggest saving to the computer you're running from. \\%a\c$\result.txt
ASKER
becraig,
You script is still not working as expected. Can you please test it?
You script is still not working as expected. Can you please test it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for /f %a in (c:\serverlist.txt) do psexec \\%a cmd /c echo %a >> \\%a\c$\result.txt & net localgroup Administrators >>
\\%a\c$\result.txt
ASKER
Sorry, becraig. It is still not working. I do not want to pipe the result to the servers C drive.
I am going to have to use Subsun's script as it has the display I need.
Thanks for your help.
I am going to have to use Subsun's script as it has the display I need.
Thanks for your help.
Open in new window