0

我有一个名为 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 文件中获取计算机名称?就像在脚本开始时这样,它会自动获取所有计算机名称并给我本地“管理员”组的成员,然后将其导出为一个文本文件或 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-产品

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

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 个名称时它“有效”?哦'

4

1 回答 1

1

您正在寻找Get-ContentSet-Content

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

请注意,您需要用当前对象自动变量( )替换循环$strComputer内所有出现的 。ForEach-Object$_

如果要使用 CSV 进行输入和输出,请使用Import-CsvExport-Csvcmdlet。

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 具有一些优势,但它们需要比简单字符串更精细的处理(如您在上面的示例中所见)。

于 2017-01-10T09:09:06.030 回答