如何在powershell中等待工作

时间:2014-07-16 09:16:17

标签: powershell

我每个人都有以下脚本我正在处理但不知道如何等待拉链完成,而不是继续下一段代码。以下是我正在使用的两个脚本。第一个脚本是一个备份脚本,它正在调用另一个用于压缩的脚本

$Date = Get-Date

$folder_date = $Date.ToString("yyyy-MM-dd_HHmm")

$backup_folder_name = 'c:\Russia\' + $folder_date
$V3_folder_name = 'C:\121RussiaScaled\products'
$Admin_folder_name = 'C:\inetpub\wwwroot\admin'
$Tablet_folder_name = 'C:\inetpub\wwwroot\tabl'
$Service_folder_name = 'C:\Russia\dll'


if (!(Test-Path -path $backup_folder_name)) {
New-Item $backup_folder_name -type directory
} # if (!(Test-Path -path D:Data))

if ((Test-Path -path $V3_folder_name)) {

Start-job -scriptblock {gi $V3_folder_name | .\Library\out-zip.ps1    
$backup_folder_name\V3.zip $_} 

Wait-job -Id $Job.Id

}

if ((Test-Path -path $Service_folder_name)) {
$Job = Start-job -scriptblock {gi $Service_folder_name | .\Library\out-zip.ps1   
$backup_folder_name\Services.zip $_} 

Wait-job -Id $Job.Id


}

if ((Test-Path -path $Admin_folder_name)) {
$Job = Start-job -scriptblock {gi $Admin_folder_name | .\Library\out-zip.ps1    
$backup_folder_name\admin.zip $_} 


Wait-job -Id $Job.Id


}

if ((Test-Path -path $Tablet_folder_name)) {
$Job = Start-job -scriptblock {gi $Tablet_folder_name | .\Library\out-zip.ps1   
$backup_folder_name\tablet.zip $_} 


Wait-job -Id $Job.Id

}

这是我的out.zip脚本

$path = $args[0]
$files = $input

write-output $path

if (-not $path.EndsWith('.zip')) {$path += '.zip'}   

if (-not (test-path $path)) { 
set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($path) 
$files | foreach {$zipfile.CopyHere($_.fullname)}  

使用上面的脚本时,我收到错误“Start-Job缺少paratmeter脚本块的参数。” 还是有另一种方式让我可以等待这些拉链逐一完成

1 个答案:

答案 0 :(得分:1)

尝试

$Job = Start-Job -ScriptBlock .....
Wait-Job -Id $Job.Id

对于ScriptBlock错误,请尝试在同一行指定起始大括号,如:

$Job = Start-Job -ScriptBlock {
# Job code here 
}