|
| 1 | + |
| 2 | +param( |
| 3 | + [Parameter(Mandatory)] [string] $ResourceGroupName, |
| 4 | + [Parameter(Mandatory)] [string] $AutomationAccountName, |
| 5 | + [Parameter(Mandatory)] [string] $RunbookName, |
| 6 | + [string] $JobId = "", |
| 7 | + [ValidateSet('Output','Verbose','Warning','Error','Progress','Any')] [string] $Streams = 'Output', |
| 8 | + [int] $PollSeconds = 5 |
| 9 | +) |
| 10 | + |
| 11 | +if (-not (Get-AzContext)) { Connect-AzAccount | Out-Null } |
| 12 | + |
| 13 | +# Resolve JobId if not supplied |
| 14 | +if ([string]::IsNullOrWhiteSpace($JobId)) { |
| 15 | + $job = Get-AzAutomationJob -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -RunbookName $RunbookName | |
| 16 | + Sort-Object StartTime -Descending | Select-Object -First 1 |
| 17 | + if (-not $job) { Write-Error "No jobs found for runbook '$RunbookName'."; return } |
| 18 | + $JobId = $job.JobId |
| 19 | +} |
| 20 | + |
| 21 | +Write-Host "Watching JobId: $JobId for runbook '$RunbookName' in '$AutomationAccountName' (RG: $ResourceGroupName)..." -ForegroundColor Cyan |
| 22 | + |
| 23 | +# Dedupe per stream |
| 24 | +$seen = @{ |
| 25 | + Output = [System.Collections.Generic.HashSet[string]]::new() |
| 26 | + Verbose = [System.Collections.Generic.HashSet[string]]::new() |
| 27 | + Warning = [System.Collections.Generic.HashSet[string]]::new() |
| 28 | + Error = [System.Collections.Generic.HashSet[string]]::new() |
| 29 | + Progress= [System.Collections.Generic.HashSet[string]]::new() |
| 30 | +} |
| 31 | + |
| 32 | +function Print-Stream { |
| 33 | + param([string]$stream) |
| 34 | + |
| 35 | + # 1) List job-level stream entries (summary) |
| 36 | + $records = Get-AzAutomationJobOutput -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Id $JobId -Stream $stream |
| 37 | + |
| 38 | + foreach ($r in $records) { |
| 39 | + $rid = $r.Id |
| 40 | + # Use rid when present; fall back to Summary otherwise |
| 41 | + if ($rid) { |
| 42 | + if (-not $seen[$stream].Contains($rid)) { |
| 43 | + $seen[$stream].Add($rid) | Out-Null |
| 44 | + $rec = Get-AzAutomationJobOutputRecord -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Id $rid |
| 45 | + Write-Host ("[{0}] [{1}] {2}" -f (Get-Date), $stream, $rid) -ForegroundColor Green |
| 46 | + if ($rec.Value) { |
| 47 | + try { ($rec.Value | ConvertFrom-Json) | ConvertTo-Json -Depth 8 } catch { $rec.Value } |
| 48 | + } else { |
| 49 | + $r.Summary |
| 50 | + } |
| 51 | + } |
| 52 | + } else { |
| 53 | + # Summary-only entry with no record id |
| 54 | + Write-Host ("[{0}] [{1}] (no Id)" -f (Get-Date), $stream) -ForegroundColor Yellow |
| 55 | + $r.Summary |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +# Main polling loop |
| 61 | +while ($true) { |
| 62 | + $j = Get-AzAutomationJob -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Id $JobId |
| 63 | + |
| 64 | + $j = Get-AzAutomationJob -ResourceGroupName $ResourceGroupName ` |
| 65 | + -AutomationAccountName $AutomationAccountName ` |
| 66 | + -Id $JobId |
| 67 | + |
| 68 | + if (-not $j) { |
| 69 | + Write-Host ("[{0}] Status: (not yet available) JobId:{1}" -f (Get-Date), $JobId) -ForegroundColor Yellow |
| 70 | + Start-Sleep -Seconds $PollSeconds |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + Write-Host ("[{0}] Status: {1} Started:{2} LastChange:{3}" -f (Get-Date), $j.Status, $j.StartTime, $j.LastModifiedTime) |
| 75 | + |
| 76 | + if ($Streams -eq 'Any') { foreach ($s in @('Output','Verbose','Warning','Error','Progress')) { Print-Stream -stream $s } } |
| 77 | + else { Print-Stream -stream $Streams } |
| 78 | + |
| 79 | + if ($j.Status -in 'Completed','Failed','Stopped','Suspended') { |
| 80 | + Write-Host "Job reached terminal state: $($j.Status)" -ForegroundColor Cyan |
| 81 | + # one last pass |
| 82 | + if ($Streams -eq 'Any') { foreach ($s in @('Output','Verbose','Warning','Error','Progress')) { Print-Stream -stream $s } } |
| 83 | + else { Print-Stream -stream $Streams } |
| 84 | + break |
| 85 | + } |
| 86 | + |
| 87 | + Start-Sleep -Seconds $PollSeconds |
| 88 | +} |
0 commit comments