【问题标题】:Wait while task running? / Jump next line when task done?等待任务运行? / 任务完成后跳转下一行?
【发布时间】:2012-04-22 20:06:18
【问题描述】:

我看到了很多关于 powershell + 等待进程结束的主题,但不知何故它对我不起作用。我有这个:

Add-PsSnapin Microsoft.SharePoint.Powershell

Add-SPSolution sol.wsp

Install-SPSolution -identity $sol -GACDeployment

Install-SPFeature "feature"

我想做的是添加一个新的 Sharepoint (2010) 解决方案,然后我尝试安装一个解决方案,最后我尝试安装功能。

最后一个失败了,因为安装解决方案需要更长的时间,但他已经在尝试安装该功能。如果我再次启动脚本,我会收到错误Install-SPFeature : Failed to find the XML file at location,可以安装该功能。我怎样才能改变我的脚本来处理这个问题?好的,当然我可以使用Start-Sleep -s 2 或其他东西,但这不是最好的方法。 | Out-Null-Wait 也不起作用。我认为那是因为该过程或已经完成的任何事情,但是 Windows 需要几秒钟才能意识到解决方案已安装。有任何想法吗?谢谢

【问题讨论】:

    标签: windows sharepoint powershell powershell-2.0


    【解决方案1】:

    这是我使用的部署脚本中的一个 sn-p:

    Write-Host "Deploying solution: $SolutionPackageName"
    $Solution = Get-SPSolution | ? {($_.Name -eq $SolutionPackageName) -and ($_.Deployed -eq $false)}
    Install-SPSolution -Identity $SolutionPackageName -GACDeployment -Confirm:$false -force
    $index = 0 
    [DateTime] $startingTime = [DateTime]::Now.AddMinutes(2) 
    while($Solution.JobExists) 
    { 
        $index++ 
        if($startingTime -lt [DateTime]::Now) 
        { 
            Write-Host "Deployment job: $SolutionPackageName failed. Deployed = $Solution.Deployed, Index = $index"
            break 
        } 
        Write-Host "Deployment job: $SolutionPackageName is still running. Deployed = $Solution.Deployed, Index = $index"
        Start-Sleep -s 5 
        $Solution = Get-SPSolution | ? {$_.Name -eq $SolutionPackageName}
    } 
    Write-Host "Deploying solution: $SolutionPackageName - Done."
    

    是的,它使用Start-Sleep,但我认为脚本通过检查解决方案何时实际部署来以一种聪明的方式使用它。就个人而言,我喜欢屏幕上的反馈,知道脚本没有挂起。我从来没有遇到过超过 2 分钟的情况,但我肯定让它显示多达 10 条“仍在运行”消息。

    【讨论】:

      【解决方案2】:

      您是否尝试使用 Start-Job?

      $sb = { Install-SPSolution -identity $sol -GACDeployment }
      $job = start-job -scriptblock $sb
      Wait-Job $job  | Out-Null
      $retMsg = Receive-Job $job
      

      【讨论】:

      • 嗯,这个我什么都做不了,我收到消息:The term 'Install-SPSolution' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was inclu ded, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (Install-SPSolution:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
      • 这只是因为您必须在作为工作开始的脚本块中添加 snappin。您调用还将参数传递给作业。
      【解决方案3】:

      您可以将Start-Process-Wait 选项一起使用:

      Start-Process Powershell "Install-SPSolution -identity $sol -GACDeployment" -Wait
      
      Install-SPFeature "feature"
      

      【讨论】:

      • 刚刚意识到这可能不适合你的情况,因为你需要做一个Add-PSSnapin。哦,好吧...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多