2017-11-10 4 views
0

컴퓨터 목록을 가져와 목록에있는 각 컴퓨터에 몇 개의 파일을 저장하는 작은 스크립트가 있습니다. 사용자가 옵션 1을 입력하면 program.exe 및 Install.ps1 파일이 원격 컴퓨터의 C : \에 저장됩니다. program.exe는 보통 종류가 크기 때문에 1 분에서 2 분 정도 걸립니다.파일을 복사 할 때 Powershell이 ​​컴퓨터 목록을 반복하지 않는 이유는 무엇입니까?

지금 현재 2 개의 파일은 내 목록의 첫 번째 컴퓨터에만 저장됩니다. 첫 번째 작업이 완료된 후 다음 컴퓨터로 이동하지 않습니다. 왜 이렇게하고 있는거야? PowerShell에 첫 번째 컴퓨터가 완료 될 때까지 기다려야합니까?

CopyFiles 함수는 문제의 함수입니다.

여기 내 코드입니다.

cls #clear screen 

function CopyFiles { 

    # Get .exe from source and place at destination workstation 
    $source2="\\mainserver\program.exe" 
    $destination2="\\$line\c$\program.exe"   # place .exe on C:\ directory of worstation 
    Copy-Item -Recurse -Filter *.* -path $source2 -destination $destination2 -Force 

    # Get .bat from source and place at destination workstation 
    $source3="\\fileserver\Install.ps1" 
    $destination3="\\$line\c$\Install.ps1" # place .bat on C:\ directory of worstation 
    Copy-Item -Recurse -Filter *.* -path $source3 -destination $destination3 -Force 
} 

$a = Get-Content "C:\myscript\computers.txt" 
foreach($line in $a) 
{ 

    "These options must run in numbered order." 
    " " 
    " " 
    "1. Copy installer to remote computer(s)." 
    "2. Remove application from remote computer(s)." 
    "3. Install application from remote computer(s)." 
    "4. Quit." 
    " " 
    "Type number and press Enter." 
    $UI = Read-Host -Prompt ' ' 

    If ($UI -eq 1) { 
    CopyFiles 
    } ELSEIF ($UI -eq 2) { 
    psexec @C:\myscript\computers.txt -c "\\fileserver\Remove.bat" 
    } ELSEIF ($UI -eq 3) { 
    psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1"" 
    } ELSEIF ($UI -eq 4) { 
    "Good Bye" 
    } 
} 
+0

각 컴퓨터에서 동일한 작업을 수행 하시겠습니까? 루프에서 "UI"로직을 이동해야합니다. 지금 작성되었으므로 각 컴퓨터에서 작업을 수행하기 전에 입력하라는 메시지가 나타납니다. –

+0

귀하의 권리. 고맙습니다. –

답변

0

당신은 당신이 foreach의 메시지 외부 사용자가 모든 컴퓨터에 대해 묻는 메시지가 표시됩니다 단 한 번 밖에 호출 할 이동해야하므로 모든 원격 컴퓨터에있는 파일을 복사 할. 함수에있는 컴퓨터 목록을 전달할 수도 있습니다.

cls #clear screen 

function Copy-Files { 
    Param(
    [Parameter(Mandatory=$true)] 
    [string[]] 
    $Hostnames 
) 

    foreach ($computer in $Hostnames) { 
    # Get .exe from source and place at destination workstation 
    $source2 = "\\mainserver\program.exe" 
    $destination2 = "\\$computer\c$\program.exe"   # place .exe on C:\ directory of worstation 
    Copy-Item -Recurse -Filter *.* -path $source2 -destination $destination2 -Force 

    # Get .bat from source and place at destination workstation 
    $source3 = "\\fileserver\Install.ps1" 
    $destination3 = "\\$computer\c$\Install.ps1" # place .bat on C:\ directory of worstation 
    Copy-Item -Recurse -Filter *.* -path $source3 -destination $destination3 -Force 
    } 
} 

"These options must run in numbered order." 
" " 
" " 
"1. Copy installer to remote computer(s)." 
"2. Remove application from remote computer(s)." 
"3. Install application from remote computer(s)." 
"4. Quit." 
" " 
"Type number and press Enter." 

$listComputers = Get-Content "C:\myscript\computers.txt" 
$UI = Read-Host -Prompt ' ' 

If ($UI -eq 1) { 
    Copy-Files -Hostnames $listComputers 
} 
ELSEIF ($UI -eq 2) { 
    psexec @C:\myscript\computers.txt -c "\\fileserver\Remove.bat" 
} 
ELSEIF ($UI -eq 3) { 
    psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1"" 
} 
ELSEIF ($UI -eq 4) { 
    "Good Bye" 
} 
관련 문제