我有一个小脚本,它会获取计算机列表并将一些文件保存到列表中的每台计算机上。如果用户输入选项1,它会将program.exe和Install.ps1文件保存到远程计算机的C:\。 program.exe通常需要一分钟或两分钟,因为它有点大。
截至目前,这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 :(得分: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"
}