2017-01-10 1 views
0

완벽하게 작동하는 script1.ps1이라는 PowerShell 스크립트가 있습니다. 다음은 스크립트입니다.로컬 'Administators'그룹의 구성원 검색 자동화

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' 

나는 List1.txt 1 컴퓨터 이름을 추가해야 :

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시 18 분) : 나는 5 개의 이름으로 시도했다. 그것은 계속 작동한다 ..

단지 하나의 이름을 추가 할 때 "작동"하는 이유는 무엇입니까? Oo '

+0

'$ _'이 (으)로 생각되는 항목인지 확인하십시오. 'Write-Host '의 출력은 무엇입니까 - $ _- "'? –

+0

@Ansgar 알았어, 나는이 수표를 $ _ = "lol"이라고했다. Write-Host "$ _"(나를 보여 주었다) 나는 이해한다. 그러나 Get-Content 전에 $ _을 정의해야합니까? 아니면 $ _을 내가 원하는 것으로 다시 작성할 수 있습니까? – Skyxia

+0

'Write-Host "- $ _-"'를 루프 안에 넣고 결과가 무엇인지보고해야합니다. * 여기에 적어 둔대로 * 정확하게 * 사용하십시오. 아무것도 남겨 두지 마십시오. 아무것도 추가하지 마십시오. –

답변

1

당신은 Get-ContentSet-Content을 찾고 있습니다. 현재 개체 automatic variable ($_)로 ForEach-Object 루프 내에서 $strComputer의 모든 항목을 교체해야

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

참고.

입력 및 출력에 CSV를 사용하려면 Import-CsvExport-Csv cmdlet을 사용하십시오. 여러 개의 속성 항목을 처리해야 할 때 CSV를 몇 가지 장점을 가지고 있지만 (위 제의 예에서 볼 수 있듯이)가 단순한 문자열보다 다소 더 정교한 처리가 필요합니다

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 

참고.