Need windows powershell script for remote machine to find the members in admin group

Author
rohitgolharr
New Member
  • Total Posts : 3
  • Scores: 0
  • Reward points: 340
  • Joined: 2012/02/02 00:33:25
  • Status: offline
2012/02/02 01:05:04 (permalink)
0

Need windows powershell script for remote machine to find the members in admin group

Hi All,
 
I am looking for a powershell script for :- I have 20 servers in a domain. every week i need to find out how many members are there in Admin group in each of server.since logging on each server is bit difficuilt , i need a script which i can execute remotely by changing the hostname in the script and get the members details contained in admin group on server  in excel format .
 
 
#1

7 Replies Related Threads

    msmith515
    Expert Member
    • Total Posts : 109
    • Scores: 5
    • Reward points: 23270
    • Joined: 2008/11/30 22:20:10
    • Location: Brisbane, QLD, Australia
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/02 01:19:58 (permalink)
    0
    This script should do the trick
    http://gallery.technet.microsoft.com/scriptcenter/190aadd4-01ef-4e26-8ff0-9862439f5328
    (not my script, Mohamed Garrana is the generousness author of the script)
     
    You need to feed it a list of computers from a file called computers.txt
     
    You should be able to set it up as a scheduled task if you need to run it every week.
    If you do go with the scheduled task I would modify the script to emails you the results.
     

    Martin Smith 
    Website: www.IndustrialArcServices.com.au 
    SCCM Engineer - going balder by the day
    #2
    rohitgolharr
    New Member
    • Total Posts : 3
    • Scores: 0
    • Reward points: 340
    • Joined: 2012/02/02 00:33:25
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/02 02:24:58 (permalink)
    0
    i tried the script but it is not giving correct results .i seperated the computer names in the file with ";" still not getting correct result .Also i want only the administrators members.I got this output :-
     
    #TYPE System.Management.Automation.PSCustomObject
    "ComputerName","Administrators","PowerUsers"
    #3
    martylist
    Moderator
    • Total Posts : 76
    • Scores: 0
    • Reward points: 40370
    • Joined: 2005/06/10 11:34:31
    • Location: Colorado, USA
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/02 07:14:13 (permalink)
    0
    Your computer names should be separated by new lines rather than ";", read the help for "get-content -?".
     
    If you don't want Power Users, then comment all of the lines that contain $powergroup and $powerlist.  Like this:
        # $powerGroup = $computer.psbase.children.find("Power Users") 
    Post your script if you need more help.
     
    #4
    rohitgolharr
    New Member
    • Total Posts : 3
    • Scores: 0
    • Reward points: 340
    • Joined: 2012/02/02 00:33:25
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/06 02:06:59 (permalink)
    0
    hi
     
    i am running this script
    function get-localusers {
            param(
        [Parameter(Mandatory=$true,valuefrompipeline=$true)]
        [string]$strComputer)
        begin {}
        Process {
            $adminlist =""
            $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
            $AdminGroup = $computer.psbase.children.find("Administrators")
            $Adminmembers= $AdminGroup.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
            foreach ($admin in $Adminmembers) { $adminlist = $adminlist + $admin + "," }
            foreach ($poweruser in $Powermembers) { $powerlist = $powerlist + $poweruser + "," }
            $Computer = New-Object psobject
            $computer | Add-Member noteproperty ComputerName $strComputer
            $computer | Add-Member noteproperty Administrators $adminlist
             Write-Output $computer
     
     
            }
    end {}
    }
     
    Get-Content D:\script.txt | get-localusers | Export-Csv D:\usersnow.txt
    -------------------------------------------
     
    the problem is that if the script is unable to find one server ,it reapets the output of the last result ..and it is unable to find the servers using hostname .i tried giving the ip address too but still it doesnot work ..
     
    #5
    martylist
    Moderator
    • Total Posts : 76
    • Scores: 0
    • Reward points: 40370
    • Joined: 2005/06/10 11:34:31
    • Location: Colorado, USA
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/12 18:53:23 (permalink)
    0
     
    To start with, you don't need this line any more, so remove it:
                foreach ($poweruser in $Powermembers) { $powerlist = $powerlist + $poweruser + "," }
     
    Your script isn't doing any error checking, it just adds everything it finds and if an error happens the results from the last computer have not been erased and get added again.  When your script is doing something in a loop, start by erasing the values before you get started, like this:
            $adminList = ""
            $adminGroup = $null
            $adminMembers = $null
     
     
    There are many methods and places to add error checking, but to start with try this:
     
    function get-localusers {
        param(
            [Parameter(Mandatory=$true,valuefrompipeline=$true)]
            [string]$computerName
            )
        begin   {}
        Process {
            $adminList = ""
            $adminGroup = $null
            $adminMembers = $null
            $computer = [ADSI]("WinNT://" + $computerName + ",computer")
            $resultsOutput = New-Object psobject
            $resultsOutput | Add-Member noteproperty ComputerName $computerName
           
            Write-Host "Contacting $computerName... " -NoNewline
            $adminGroup = $computer.psbase.children.find("Administrators")
            if ($? -eq $True) {
                $adminMembers = $adminGroup.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
                foreach ($adminName in $adminMembers) {$adminList = $adminList + $adminName + ","}
                $resultsOutput | Add-Member noteproperty Administrators $adminList
                Write-Host "Success."
               }
            Write-Output $resultsOutput
           }
        end     {}
    }
    Get-Content D:\script.txt | get-localusers | Export-Csv D:\usersnow.txt -NoTypeInformation
     
    #6
    martylist
    Moderator
    • Total Posts : 76
    • Scores: 0
    • Reward points: 40370
    • Joined: 2005/06/10 11:34:31
    • Location: Colorado, USA
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/12 18:56:17 (permalink)
    0
    Now as far as your other comment: "unable to find the servers using hostname .i tried giving the ip address too but still it doesnot work"
    that is most likely a firewall issue, since the IP address doesn't work either it's not a name resolution issue.
    Don't troubleshoot this from a PowerShell perspective, troubleshoot by trying to connect to those same computers with other tools (like Computer Management) and see if you can get the Administrators group.
     
    post edited by martylist - 2012/02/15 07:35:56
    #7
    Damien
    New Member
    • Total Posts : 6
    • Scores: 0
    • Reward points: 21430
    • Joined: 2009/02/10 00:18:22
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group 2012/02/14 22:01:28 (permalink)
    0
    I'm coming into this late, but here's what I use in my environment. You may want to be sure to check connectivity as MartyList mentioned. Just be sure to poke only the holes needed if your environment requires the builtin firewall to be enabled.  I can't take credit for the code, but I don't remember where I got the guts of this from.  I hope it helps
     
    $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
    $computer.name
    function ListAdministrators
    {
    $members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
    $members}
    $Group = $computer.psbase.children.find("administrators")
    ListAdministrators
    #8
    Jump to:
    © 2018 APG vNext Commercial Version 5.5