【问题标题】:Runspaces and Jobs in VSTS hosted agent release Azure Powershell scriptVSTS 托管代理版本中的运行空间和作业 Azure Powershell 脚本
【发布时间】:2017-03-23 20:58:55
【问题描述】:

我有一个带有托管代理的 VSTS 设置,我正在尝试运行 Azure Powershell 脚本以使用 Runspaces 异步方法调整经典云服务的大小。当我在本地机器上运行此脚本时,它运行良好。

[hashtable]$myServices = @{}
$myServices.Add('serviceA',3)
$myServices.Add('serviceB',1)
$myServices.Add('serviceC',2)
$myServices.Add('serviceD',1)
$myServices.Add('serviceE',3)

$Throttle = 5 #threads

$ScriptBlock = {
   Param (
      [string]$serviceName,
      [int]$instanceCount
   )
    Try{
        $roles = Get-AzureRole -ServiceName $serviceName -Slot Production -ErrorAction Stop
        foreach($role in $roles){
            Write-Output 'Currently on ' + $role.RoleName  
            $result = Set-AzureRole -ServiceName $serviceName -Slot Production -RoleName $role.RoleName -Count $instanceCount -ErrorAction Stop
            $thisRunResult = New-Object PSObject -Property @{
                Service = $serviceName            
                Role = $role.RoleName            
                Description = $result.OperationDescription
                Status = $result.OperationStatus            
            }
            $RunResult += $thisRunResult
        }
        Return $RunResult   
    } Catch {
        write-output "An error occurred in the script block."
        write-output $_.Exception.Message       
    }
}

Try{
    $ErrorActionPreference = "Stop"
    $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle)
    $RunspacePool.Open()
    $Jobs = @()
} Catch {
    write-output "An error occurred while instantiating Runspaces."
    write-output $_.Exception.Message       
} Finally {
    $ErrorActionPreference = "Continue"
}

foreach ($service in $qaServices.GetEnumerator()){   
    Write-Output 'Currently on ' + $service.Key
    Try{
        $ErrorActionPreference = "Stop"
        $Job = [powershell]::Create().AddScript($ScriptBlock).AddArgument($service.Key).AddArgument($service.Value)
        write-output "Line after the Job is created gets executed."
        $Job.RunspacePool = $RunspacePool
        $Jobs += New-Object PSObject -Property @{
            RunNum = $service.Key
            Pipe = $Job
            Result = $Job.BeginInvoke()
        }
    } Catch {
        write-output "An error occurred creating job."
        write-output $_.Exception.Message       
    } Finally {
        $ErrorActionPreference = "Continue"
    }

}

Write-Host "Waiting.." -NoNewline
Do {
   Write-Host "." -NoNewline
   Start-Sleep -Seconds 1
} While ( $Jobs.Result.IsCompleted -contains $false)
Write-Host "All jobs completed!"

$Results = @()
ForEach ($Job in $Jobs)
{   $Results += $Job.Pipe.EndInvoke($Job.Result)
}

$Results | Select-Object -Property Service,Role,Description,Status | Sort-Object -Property Service | Out-Host

我的 Try/Catch 内容都不会在日志中输出任何内容。当我在本地运行它时,我会得到一个新的“。”,就像我期望的那样。每秒直到完成:

Waiting.........................All jobs completed!

在本地,它是几百个 ...,因为缩放事物需要几分钟。在 VSTS 中运行时,立即返回大约 8 个点。所以看起来实际上没有发生任何事情(特别是作业没有初始化),但没有错误告诉我问题出在哪里。提前致谢。

【问题讨论】:

  • 以提升模式运行?
  • 您使用哪个任务来运行此脚本?您可以在“System.Debug”变量设置为“True”的情况下运行构建,然后共享日志吗?

标签: powershell azure azure-pipelines-release-pipeline


【解决方案1】:

(未经测试) 试试这个:

try {    
   $ErrorActionPreference = "Stop"
   # Your code
} catch{
   Write-Host $_.Exception
}finally{
   $ErrorActionPreference = "Continue"
}

默认情况下,$ErrorActionPreference 设置为 Continue,因此通过将其设置为 Stop,您表示所有错误都在“终止”,因此异常被 try/catch 块捕获。

或者第二种选择是使用公共参数-ErrorAction Stop

   try {               
       $result = Set-AzureRole -ServiceName $serviceName -Slot Production -RoleName $role.RoleName -Count $instanceCount -ErrorAction Stop
   } catch{
           Write-Host $_.Exception
   }

我对 AD(活动目录)命令行开关也有同样的问题。

【讨论】:

  • 谢谢。我按照您的建议尝试并更新了我的代码示例,但不幸的是,结果相同。
  • 发现了有关 Azure PS github.com/Azure/azure-powershell/issues/1135 的讨论。如果 Azure cmdlet 不处理异常,则唯一的选择是检查 $result 是否包含任何内容。如果否,则输出一般错误消息,如“出现问题”。
猜你喜欢
  • 1970-01-01
  • 2017-06-05
  • 2023-03-05
  • 2019-07-09
  • 2018-09-13
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多