2017-03-23 86 views
0

我有託管代理的VSTS設置,我試圖運行Azure Powershell腳本以使用Runspaces異步方法調整經典雲服務的大小。這個腳本在我的本地機器上運行時工作正常。VSTS託管代理版本中的運行空間和作業Azure Powershell腳本

[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個點。所以看起來沒有任何事情發生(特別是喬布斯沒有初始化),但沒有錯誤告訴我問題是什麼。提前致謝。

+0

與提升模式? –

+0

您使用哪個任務來運行此腳本?你可以使用設置爲「True」的「System.Debug」變量運行構建,然後共享日誌嗎? –

回答

0

(未經測試) 試試這個:

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

默認$ErrorActionPreference設置爲Continue所以將其設置爲Stop你是說所有的錯誤都「終止」這樣的異常被試逮住/ catch塊。

或第二選項是使用通用參數-ErrorAction Stop

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

我曾與AD(活動目錄)命令開關同樣的問題。

+0

謝謝。我嘗試了你的建議並更新了我的代碼示例,但不幸的是,結果相同。 –

+0

發現有關Azure PS的討論https://github.com/Azure/azure-powershell/issues/1135。如果Azure cmdlet不處理異常處理,唯一的選擇是檢查'$ result'是否包含任何內容。如果不是,則輸出通用錯誤消息,如「出錯了」。 – pandemic

相關問題