2017-11-10 9 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" 
} 
関連する問題