myITforum and Windows IT Pro Forums

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

Author Message
rohitgolharr

  • Total Posts : 3
  • Scores: 0
  • Reward points : 340
  • Joined: 2/2/2012
  • Status: offline
Need windows powershell script for remote machine to find the members in admin group Thursday, February 02, 2012 1:05 AM (permalink)
0
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
    msmith515

    • Total Posts : 109
    • Scores: 5
    • Reward points : 23270
    • Joined: 11/30/2008
    • Location: Brisbane, QLD, Australia
    • Status: offline
    Re:Need windows powershell script for remote machine to find the members in admin group Thursday, February 02, 2012 1:19 AM (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

      • Total Posts : 3
      • Scores: 0
      • Reward points : 340
      • Joined: 2/2/2012
      • Status: offline
      Re:Need windows powershell script for remote machine to find the members in admin group Thursday, February 02, 2012 2:24 AM (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

        • Total Posts : 73
        • Scores: 0
        • Reward points : 26140
        • Joined: 6/10/2005
        • Location: Colorado, USA
        • Status: offline
        Re:Need windows powershell script for remote machine to find the members in admin group Thursday, February 02, 2012 7:14 AM (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

          • Total Posts : 3
          • Scores: 0
          • Reward points : 340
          • Joined: 2/2/2012
          • Status: offline
          Re:Need windows powershell script for remote machine to find the members in admin group Monday, February 06, 2012 2:06 AM (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

            • Total Posts : 73
            • Scores: 0
            • Reward points : 26140
            • Joined: 6/10/2005
            • Location: Colorado, USA
            • Status: offline
            Re:Need windows powershell script for remote machine to find the members in admin group Sunday, February 12, 2012 6:53 PM (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

              • Total Posts : 73
              • Scores: 0
              • Reward points : 26140
              • Joined: 6/10/2005
              • Location: Colorado, USA
              • Status: offline
              Re:Need windows powershell script for remote machine to find the members in admin group Sunday, February 12, 2012 6:56 PM (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.
               
              <message edited by martylist on Wednesday, February 15, 2012 7:35 AM>
               
              #7
                Damien

                • Total Posts : 6
                • Scores: 0
                • Reward points : 21430
                • Joined: 2/10/2009
                • Status: offline
                Re:Need windows powershell script for remote machine to find the members in admin group Tuesday, February 14, 2012 10:01 PM (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
                  Online Bookmarks Sharing: Share/Bookmark

                  Jump to:

                  Current active users

                  There are 0 members and 2 guests.

                  Icon Legend and Permission

                  • New Messages
                  • No New Messages
                  • Hot Topic w/ New Messages
                  • Hot Topic w/o New Messages
                  • Locked w/ New Messages
                  • Locked w/o New Messages
                  • Read Message
                  • Post New Thread
                  • Reply to message
                  • Post New Poll
                  • Submit Vote
                  • Post reward post
                  • Delete my own posts
                  • Delete my own threads
                  • Rate post

                  2000-2014 ASPPlayground.NET Forum Version 3.9