自动搜索本地 'Administators' 组的成员

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

我有一个名为 script1.ps1 的 PowerShell 脚本,它工作得很好。这是脚本:

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

但是如您所见,我不得不每次都写下我想要的计算机名称。 我问自己是否有一种功能或其他东西可以自动从文本或 CSV 文件中获取计算机名称? 像这样在脚本的开头,它会自动获取所有计算机名称并给我本地 'Administators' 组的成员,然后将其导出到一个文本文件或 CSV 文件中吗?


编辑: 根据给出的答案更新了脚本。

我有一个名为 script1.ps1 的 PowerShell 脚本,它不能完美运行。这是脚本:

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'

我已将 1 个计算机名称添加到 List1.txt :

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

它工作(不)完美(不可读的布局)

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

注意 (15:18pm) :我尝试了 5 个名字,它继续工作..

为什么只添加 1 个名称 "works" ?哦'

您正在寻找 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'

请注意,您需要将 ForEach-Object 循环中出现的所有 $strComputer 替换为当前对象 automatic variable ($_).

如果要将 CSV 用于输入和输出,请使用 Import-Csv and Export-Csv cmdlet。

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

请注意,当您需要处理具有多个属性的项目时,CSV 有一些优势,但它们需要比简单字符串更复杂的处理(如您在上面的示例中所见)。