简体   繁体   English

VSTS托管代理中的运行空间和作业发布了Azure Powershell脚本

[英]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. 我有一个带有托管代理的VSTS设置,我试图运行Azure Powershell脚本以使用Runspaces异步方法调整经典云服务的大小。 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. 在VSTS中运行时,大约有八个点立即返回。 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. 因此,看起来好像实际上什么都没有发生(特别是Jobs尚未初始化),但是没有错误可以告诉我问题出在哪里。 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. 默认情况下, $ErrorActionPreference设置为Continue因此将其设置为Stop表示所有错误都在“终止”,因此try / catch块捕获了异常。

or second option is to use common parameter -ErrorAction Stop 或第二个选项是使用通用参数-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. 我对AD(活动目录)命令行开关有相同的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure DevOps(VSTS)-在托管代理上使用Azure Az PowerShell - Azure DevOps (VSTS) - using Azure Az PowerShell on a Hosted Agent 使用运行空间的 Powershell 脚本 - Powershell Script Using Runspaces 在 VSTS 托管生成代理上运行自定义 npm 命令 Powershell - Run custom npm command in Powershell on VSTS hosted build agent VSTS版本定义中PowerShell脚本的硬编码参数 - Hardcode arguments to the PowerShell Script in VSTS Release definition 如何使用Powershell更新VSTS发布作业代理程序池 - How to update VSTS release job agent pool using powershell 在 VSO 版本中,Powershell 任务在使用“Azure Pipelines”代理时失败,而在使用“Hosted MSIT Fortify 2017”代理时相同的任务正在通过 - In VSO release Powershell task is failing while using 'Azure Pipelines' agent and same task is passing while using 'Hosted MSIT Fortify 2017' agent “创建管道时出错。” 在 Azure 管道自托管代理中运行 powershell 脚本时 - "An error occurred while creating the pipeline." when running powershell script in Azure pipeline self-hosted agent VSTS 将变量传递到 Azure PowerShell 脚本参数 - VSTS passing variables into Azure PowerShell Script Arguments VSTS:将构建/发布变量传递到 Powershell 脚本任务中 - VSTS: Pass build/release variables into Powershell script task VSTS - 用于添加变量以释放环境的 Powershell 脚本不起作用 - VSTS - Powershell script to add variable to release environment not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM