在没有任何运气的情况下进行了大约半天的谷歌搜索后,我决定在此处发布我的问题。
所以我要检查作业状态是否已完成。
到目前为止,这是我的代码:
Start-job -name Copy -Scriptblock {Copy-Item -force -recurse -path
"C:\Test.temp" -destination "D:\"}
$x = 170 $length = $x / 100 While($x -gt 0) {
$min ) [int](([string]($x/60)).Split('.')[0]) $Text = " " + $min + "
mintues " + ($x % 60) + " seconds left" Write-progress "Copying file,
estimated time remaning" -status $text -percentComplete ($x/$length)
Start-sleep -s 1 $x--
if (Get-job -name Copy -status "Completed") { Break } }
如您所见,我首先开始工作,然后运行带有倒数进度条的循环。这只是给用户某种反馈,表明事情仍在发展。请注意,“ Get-job -status“ completed”不起作用,因为它不是Get-job的参数。
现在的问题是我无法获得“如果完成获取作业,请中断循环”,因为复制作业可能在进度条之前完成了。
有人知道一个好的解决方案吗? 预先感谢!
答案 0 :(得分:1)
使用ForEach可能是您最好的选择,一旦完成就可以干净地打破循环。
这会显示一个进度条,但没有计时器,更多的是每个文件的进度,这样它会告诉您它正在做什么以及花些时间。
$srcPath = 'C:\Test.temp'
$destPath = 'D:\'
$files = Get-ChildItem -Path $srcPath -Recurse
$count = $files.count
$i=0
ForEach ($file in $files){
$i++
Write-Progress -activity "Copying from $srcPath to $destPath" -status "$file ($i of $count)" -percentcomplete (($i/$count)*100)
if($file.psiscontainer){
$sourcefilecontainer = $file.parent
} else {
$sourcefilecontainer = $file.directory
}
$relativepath = $sourcefilecontainer.fullname.SubString($srcPath.length)
Copy-Item $file.fullname ($destPath + $relativepath) -force
}
答案 1 :(得分:0)
我认为您可以获得这样的条件:
if ( Get-job -State Completed | Where-Object {$_.Name.Contains("Copy"){ Break }
您的-status
在get-job
中不存在