简体   繁体   中英

Runspaces and Jobs in VSTS hosted agent release Azure Powershell script

I have a VSTS setup with hosted agents and I'm trying to run an Azure Powershell script to resize classic cloud services using a Runspaces async method. This script works fine when I run it on my local machine.

[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

None of my Try/Catch stuff outputs anything in the log. When I run this locally, I get the resulting output like I expect with a new "." every second until it's complete:

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

Locally it's a few hundred ...'s because it takes a few minutes to scale things. When run in VSTS, it's about eight dots returned immediately. So it looks like nothing is actually happening (specifically that the Jobs are not initializing), but there's no error to tell me what the problem is. Thanks in advance.

(untested) try this:

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

by default the $ErrorActionPreference is set to Continue so by setting it to Stop you are saying that all error are "terminating" thus the exception is catched by try/catch block.

or second option is to use common parameter -ErrorAction Stop

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

I had the same issue with AD(active directory) commandlets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM