You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell并行将本地文件复制至多台远程计算机?

问题解决:ForEach-Object -Parallel 参数集冲突错误

错误原因

  1. 参数集不兼容:ForEach-Object的-Parallel和-AsJob参数属于不同参数集,不能同时使用,这是触发第一个错误的直接原因。
  2. 版本不支持:-Parallel是PowerShell 7及以上版本新增的特性,若使用PowerShell 5.1或更低版本,该参数不存在,也会引发类似错误。

解决方案

方案一:使用PowerShell 7+的原生并行执行(移除-AsJob)

直接用-Parallel实现并行,无需额外指定-AsJob:

$Computers = Get-Content 'C:\temp\Inputfiles\computers.txt'
$SourcePath = 'C:\Temp\Inputfiles\VMTools\Setup64.exe'

$Computers | ForEach-Object -Parallel {
  Copy-Item -Path $using:SourcePath -Destination "\\$_\C$\Temp\" -Force
  Start-Sleep -Seconds 1
} -ThrottleLimit 5

方案二:兼容PowerShell 5.1的作业并行方式

如果无法升级PowerShell版本,改用Start-Job实现并行:

$Computers = Get-Content 'C:\temp\Inputfiles\computers.txt'
$SourcePath = 'C:\Temp\Inputfiles\VMTools\Setup64.exe'

$jobs = foreach ($computer in $Computers) {
  Start-Job -ScriptBlock {
    Copy-Item -Path $using:SourcePath -Destination "\\$using:computer\C$\Temp\" -Force
    Start-Sleep -Seconds 1
  }
}

$jobs | Wait-Job | Receive-Job
$jobs | Remove-Job # 执行完成后清理作业

额外注意事项

  • 检查PowerShell版本:执行$PSVersionTable.PSVersion确认版本,7+才能使用-Parallel参数。
  • 权限验证:确保执行脚本的账号拥有目标计算机的管理员权限,可访问C$共享目录。
  • 网络检查:确认目标计算机可连通,且SMB服务(445端口)正常开放。

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 11:52:20