?
Solved

Powershell - How to pull all Local Accounts from multiple machines

Posted on 2013-11-22
6
Medium Priority
?
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

765 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