Solved

Powershell - How to pull all Local Accounts from multiple machines

Posted on 2013-11-22
6
810 Views
Last Modified: 2013-11-25
Hi EE

I have the script below that Subsun helped me with . It was originally set to pull the member of the Administrators group but I was trying to modify it to pull all Local Accounts and its pulling members of the local Users group only not all Local Accounts .

Any ideas ?  I dont have to use this scrtipt if someone already has something different.

function get-localusers {
        param(
    [Parameter(Mandatory=$true,valuefrompipeline=$true)]
    [string]$strComputer)
    begin {}
    Process {
      $Select = "Name","Class" | %{  
        Invoke-Expression "@{n='$_';e={ `$_.GetType().InvokeMember('$_', 'GetProperty', `$Null, `$_, `$Null) }}"  
      }
      If (Test-Connection $strComputer -Count 2 -Quiet){
        $Users =""
        $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
        $UsersGroup = $computer.psbase.children.find("Users")
        $Usermembers= $UsersGroup.psbase.invoke("Members") | Select $Select
              foreach ($Users in $Usermembers) {
              $Users | Select @{N="ComputerName";E={$strComputer}},@{N="Users";E={$_.Name}},Class
                  }
            }
      Else {
            "" | Select @{N="ComputerName";E={$strComputer}},@{N="Users";E={"Not able to Ping"}},Class
        }
     }
end {}
}
Get-Content "e:\Projects\servers\Servers.txt" | get-localusers | Select ComputerName,Users,Class | Export-Csv "e:\Projects\Servers\LocalTest_$((get-date).toString('MM-dd-yyyy')).csv" -NTI
0
Comment
Question by:MilesLogan
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39669734
If you need only users in report then try..
function get-localusers {
    param(
    [Parameter(Mandatory=$true,valuefrompipeline=$true)]
    [string]$strComputer)
    begin {}
    Process {
      If (Test-Connection $strComputer -Count 2 -Quiet){
	$Users = Get-WmiObject Win32_UserAccount -ComputerName $strComputer
        foreach ($User in $Users) {
         $User | Select @{N="ComputerName";E={$strComputer}},@{N="User";E={$_.Name}}
         }
        }
      Else {
            "" | Select @{N="ComputerName";E={$strComputer}},@{N="User";E={"Not able to Ping"}}
        }
     }
end {}
}
Get-Content "e:\Projects\servers\Servers.txt" | get-localusers | Select ComputerName,User | Export-Csv "e:\Projects\Servers\LocalTest_$((get-date).toString('MM-dd-yyyy')).csv" -NTI 

Open in new window

0
 
LVL 2

Author Comment

by:MilesLogan
ID: 39670752
Hi Subsun , I don't get an error but it just loops and it does not output any data .
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39672067
I just tested the code and I am getting the following result..

"ComputerName","User"
"ServerA","Not able to Ping"
"ServerB","Not able to Ping"
"TestSRV","Administrator"
"TestSRV","Guest"
"TestSRV","TestUser"

Open in new window


Are you getting the user details when you run the following command?

Get-WmiObject Win32_UserAccount -ComputerName ServerA

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 2

Author Comment

by:MilesLogan
ID: 39672246
Below will pull Local Users from an XP machine but if I want the local users from Server 2003/2008 it does not .. same with the full script

Get-WmiObject Win32_UserAccount -ComputerName ServerA
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39672645
Ok I didn't test it.. Try this..
function get-localusers {
    param(
    [Parameter(Mandatory=$true,valuefrompipeline=$true)]
    [string]$strComputer)
    begin {}
    Process {
      $Select = "Name","Class" | %{  
      Invoke-Expression "@{n='$_';e={ `$_.GetType().InvokeMember('$_', 'GetProperty', `$Null, `$_, `$Null) }}"  
       }
        If (Test-Connection $strComputer -Count 2 -Quiet){
         $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
         $Users = $computer.psbase.children | ? {$_.psbase.SchemaClassName -eq "User"}
	 foreach ($User in $Users) {
	  $User | Select @{N="ComputerName";E={$strComputer}},@{N="User";E={$_.Name}},Class
	 }
        }
      Else {
          "" | Select @{N="ComputerName";E={$strComputer}},@{N="User";E={"Not able to Ping"}},Class
        }
      }
end {}
}
Get-Content "e:\Projects\servers\Servers.txt" | get-localusers | Select ComputerName,User | Export-Csv "e:\Projects\Servers\LocalTest_$((get-date).toString('MM-dd-yyyy')).csv" -NTI

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 39676563
WOW Subsun !  you always help out so much ! thanks !
I am opening a new question to modify this same script if it can show if the user is Disabled/Enabled and possibly the Full Name and Description .
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

730 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