无需等待即可复制项目

时间:2017-09-12 09:10:18

标签: powershell

我想通过网络复制一个包,这个包有几GB,我不能复制完成以继续执行我的PS脚本和剩余的任务,因为它们不依赖。最好的方法是什么?

目前我认为最好的方法是调用另一个脚本来执行副本而不等待。非常感谢您的想法。

2 个答案:

答案 0 :(得分:3)

Powershell有很多选择。一种最简单的方法是将 copy 用作 PSJob ,如:

#Put you script here which you want to do before copying
$source_path = "\\path\to\source\file"
$destination_path = "path\to\destination\file"
Start-Job -ScriptBlock {param($source_path,$destination_path) Copy-item $source_path $destination_path} -ArgumentList $source_path,$destination_path

# Keep Your remaining script here 

或者你可以这样做

$copyJob = Start-Job –ScriptBlock {  
   $source = "\\path\to\source\file"  
   $target = "path\to\destination\file"   
   Copy-Item -Path $source -Destination $target -Recurse Verbose  
} 

您可以使用后台智能传输服务(BITS)Cmdlet,但为此你需要有模块,那么你应该

Import-Module BitsTransfer

答案 1 :(得分:1)

一种可能的选择是将Start-BitsTransfer与-asynchronous标志一起使用。 An article explaining how that works is available here