用于等待一个进程完成的Powershell命令

时间:2013-02-04 11:58:44

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v1.0

我有power shell脚本,它将执行多个任务,如卸载,删除和再次安装wsp解决方案。我遇到的问题就像第一个进程需要花费太长时间来卸载解决方案,以便其他进程必须等到第一个动作必须完全完成。现在我正在给睡眠时间,但它有一些问题,而不同的机器速度变化..我也知道调用记事本像外部功能,但我不希望这必须发生在这里。除此之外解决方案可用,我需要在开始第二个过程之前等待第一个过程完成。

$InstallDIR = "F:\new\source\UpdatedWSPFiles"
$Dir = get-childitem $InstallDIR -Recurse
$WSPList = $Dir | where {$_.Name -like "*.wsp*"}
Foreach ($wsp in $WSPList )
{
$WSPFullFileName = $wsp.FullName
$WSPFileName = $wsp.Name
try
{
Write-Host -ForegroundColor White -BackgroundColor Blue "Working on $WSPFileName"
Write-Host -ForegroundColor Green "Retracting Solution"
Uninstall-SPSolution -AllWebApplications -Identity "$WSPFileName" -Confirm:$false 
sleep 100

Write-Host -ForegroundColor Green "Removing Solution from farm"
Remove-SPSolution -Identity "$WSPFileName" -Confirm:$false -Force
sleep 60

Write-Host -ForegroundColor Green "Adding solution to farm"
Add-SPSolution "$WSPFullFileName"  -Confirm:$false 
sleep 60
}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用-wait开关启动进程:

    PS> $p="C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    PS> $params="-command &{ if ((Get-PSSnapin `"Microsoft.SharePoint.PowerShell`" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin `"Microsoft.SharePoint.PowerShell`"
}
Uninstall-SPSolution -AllWebApplications -Identity `"$WSPFileName`" -Confirm:$false}"
    PS> Start-Process $p $params -Wait

答案 1 :(得分:0)

猜猜你可以试试

Start-Job -Name "jobname" -ScriptBlock { Uninstall-SPSolution -AllWebApplications -Identity "$WSPFileName" -Confirm:$false }

Wait-Job -Name "jobname" -Timeout "maximum wait time"