Thisisnotme
Thisisnotme

Reputation: 21

Automate a search for members of the local 'Administators' group

I have a PowerShell script named script1.ps1 that work perfectly. Here is the script:

Write-Host Script to display members of the local -ForegroundColor Green
Write-Host Administators group of a remote server. -ForegroundColor Green
Write-Host "`n"

$strComputer = Read-Host "Please enter the computer name"

$computer = [ADSI]("WinNT://" + $strComputer + ",computer")
$group = $computer.PSBase.Children.Find("administrators")

Write-Host ""
Write-Host "Computer Name : "$computer.Name
Write-Host "_____________________________________"
Write-Host ""
Write-Host "Group Name : "$Group.Name
Write-Host "_____________________________________"

$domain = $group.Path.Split("/")[2]
$string1 = "WinNT://" + $domain + "/" + $strComputer + "/"
$string2 = $strComputer + "/"
$string3 = "WinNT://"

$members  = ($group.PSBase.Invoke("Members") | Foreach-Object {$_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)}) -replace ($string1,$string2) -replace ($string3,"") 

Write-Host ""
Write-Host "Members : "
Write-Host ""
$members

But as you can see, I'm obliged to write each time the name of computer I want.

I ask myself if there is a function or other things which take automatically the computers names from a text or CSV file?

Like this at the beginning of the script, it automatically take all the computers names & give me the members of the local 'Administators' group which then gonna be exported in one text file or CSV file too?


Updated script according to the answer given.

Write-Host Script to display members of the local -ForegroundColor Green
Write-Host Administators group of a remote server. -ForegroundColor Green
Write-Host "`n"

Get-Content 'C:\Users\herbautr\Desktop\List1.txt' | ForEach-Object {
  Write-Host "-$_-"
  $computer = [ADSI]("WinNT://" + $_ + ",computer")
  $group = $computer.PSBase.Children.Find("administrators")

  Write-Host ""
  Write-Host "Computer Name : "$computer.Name
  Write-Host "_____________________________________"
  Write-Host ""
  Write-Host "Group Name : "$Group.Name
  Write-Host "_____________________________________"

  $domain = $group.Path.Split("/")[2]
  $string1 = "WinNT://" + $domain + "/" + $_ + "/"
  $string2 = $_ + "/"
  $string3 = "WinNT://"

  $members  = ($group.PSBase.Invoke("Members") | Foreach-Object {$_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)}) -replace ($string1,$string2) -replace ($string3,"") 

  Write-Host ""
  Write-Host "Members : "
  Write-Host ""
  $members
} | Set-Content 'C:\Users\herbautr\Desktop\administrators.txt'

I have add 1 computer name to the List1.txt:

01SPEAI-TEST1
01SPEAI-TEST2
02SPHPV-TEST1
01SLCPTAAP-PROD

And it works (not) perfectly (Unreadable layout)

01SPEAI-PROD1/Administrator
VNF-PRINCIPAL/Admins du domaine
VNF-PRINCIPAL/svceri
01SPEAI-PROD2/Administrator
VNF-PRINCIPAL/Admins du domaine
VNF-PRINCIPAL/svceri
02SPHPV-PROD1/Administrator
VNF-PRINCIPAL/Admins du domaine
01SLCPTAAP-PROD/Administrator
VNF-PRINCIPAL/Admins du domaine
01SLCPTAAP-PROD/maint
VNF-PRINCIPAL/svcoraas
VNF-PRINCIPAL/svcvisionit
VNF-PRINCIPAL/GopOAS
VNF-PRINCIPAL/svcdigora

Note (15:18pm): I have tried with 5 names, it continue to work.

Why when adding just 1 name it "works"?

Upvotes: 0

Views: 75

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

You're looking for Get-Content and Set-Content.

Get-Content 'C:\path\to\computers.txt' | ForEach-Object {
  $computer = [ADSI]("WinNT://" + $_ + ",computer")
  ...
} | Set-Content 'C:\path\to\administrators.txt'

Note that you need to replace all occurrences of $strComputer inside the ForEach-Object loop with the current object automatic variable ($_).

If you want to use CSVs for input and output use the Import-Csv and Export-Csv cmdlets.

Import-Csv 'C:\path\to\computers.csv' | ForEach-Object {
  $computer = [ADSI]("WinNT://" + $_.ComputerName + ",computer")
  ...
  $members | ForEach-Object {
    New-Object -Type PSObject -Property @{
      Member = $_
    }
  }
} | Export-Csv 'C:\path\to\administrators.csv' -NoType

Note that CSVs have some advantages when you need to handle items with multiple properties, but they require somewhat more elaborate handling than simple strings (as you can see in my example above).

Upvotes: 1

Related Questions