[英]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.