Clicky

I'm looking for a PS script that I can run against a txt file with a list of servers. This script will output a list of all the local admin members on each server into a csv/xls file.

I've found several online, but all of them are for one server and don't pull from a list.

Any help would be appreciated!



Example of script found:

# List local group members on the local or a remote computer

$computerName = Read-Host 'Enter computer name or press <Enter> for localhost'
$localgroupName = Read-Host "Enter local group name"
if ($computerName -eq "") {$computerName = "$env:computername"}

if([ADSI]::Exists("WinNT://$computerName/$localGroupName,group")) {

      $group = [ADSI]("WinNT://$computerName/$localGroupName,group")

      $members = @()
      $Group.Members() |
      % {
            $AdsPath = $_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)
            # Domain members will have an ADSPath like WinNT://DomainName/UserName.
        # Local accounts will have a value like WinNT://DomainName/ComputerName/UserName.
            $a = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)
            $name = $a[-1]
            $domain = $a[-2]
            $class = $_.GetType().InvokeMember("Class", 'GetProperty', $null, $_, $null)

            $member = New-Object PSObject
            $member | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
            $member | Add-Member -MemberType NoteProperty -Name "Domain" -Value $domain
            $member | Add-Member -MemberType NoteProperty -Name "Class" -Value $class

            $members += $member
      }
      if ($members.count -eq 0) {
            Write-Host "Group '$computerName\$localGroupName' is empty."
      }
      else {
            Write-Host "Group '$computerName\$localGroupName' contains these members:"
            $members | Format-Table Name,Domain,Class -autosize
      }
}
else {
      Write-Warning "Local group '$localGroupName' doesn't exist on computer '$computerName'"
}

asked 11/16/2011 11:13

MMRNLA's gravatar image

MMRNLA ♦♦


4 Answers:
Well, we just need to embed the script into a loop processing all entries of the file, and fixing the local group used:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
# List local admin group members on a list of computers taken from a file

get-content "C:serverlist.txt" | foreach-object {
  $computerName = $_
  $localgroupName = "administrators"

  if([ADSI]::Exists("WinNT://$computerName/$localGroupName,group")) {

      $group = [ADSI]("WinNT://$computerName/$localGroupName,group")

      $members = @()
      $Group.Members() |
      % {
            $AdsPath = $_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)
            # Domain members will have an ADSPath like WinNT://DomainName/UserName.
        # Local accounts will have a value like WinNT://DomainName/ComputerName/UserName.
            $a = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)
            $name = $a[-1]
            $domain = $a[-2]
            $class = $_.GetType().InvokeMember("Class", 'GetProperty', $null, $_, $null)

            $member = New-Object PSObject
            $member | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
            $member | Add-Member -MemberType NoteProperty -Name "Domain" -Value $domain
            $member | Add-Member -MemberType NoteProperty -Name "Class" -Value $class

            $members += $member
      }
      if ($members.count -eq 0) {
            Write-Host "Group '$computerName$localGroupName' is empty."
      }
      else {
            Write-Host "Group '$computerName$localGroupName' contains these members:"
            $members | Format-Table Name,Domain,Class -autosize
      }
  }
  else {
      Write-Warning "Local group '$localGroupName' doesn't exist on computer '$computerName'"
  }
}

I intentionally only changed a very small fraction, so you see the difference, and should be able to do the change yourself next time.
link

answered

Qlemo's gravatar image

Qlemo

Thats working great!

What about the output file? At the moment, it displays the results within the PS window. If it's going to be too much work, I can live with this!

Thanks for the fast response!
link

answered 2011-11-17 at 07:46:29

MMRNLA's gravatar image

MMRNLA

We need to replace write-host by write-output each time, and replace the last } with
1:
} | out-file "C:YourResultFile.txt" -Append
or
1:
} >> "C:YourResultFile.txt"

Both will append to an existing file, or create it if it does not exist.
link

answered 2011-11-17 at 07:51:00

Qlemo's gravatar image

Qlemo

Many thanks again! It worked out perfectly!
link

answered 2011-11-17 at 08:06:10

MMRNLA's gravatar image

MMRNLA

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

Asked: 11/16/2011 11:13

Seen: 188 times

Last updated: 11/17/2011 12:24