# リトライ間隔(秒) $RetryIntervalInSeconds = 120 # リトライ回数 $NumberOfRetryAttempts = 2 # メイン処理完了を表す真偽値 $CmdOk = $False # 処理の開始時間 $StartTime = (Get-Date).ToUniversalTime() # 自動作成された接続資産(実行アカウント)を利用して Azure にログイン $Connection = Get-AutomationConnection -Name "AzureRunAsConnection" $Account = Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $Connection.TenantId ` -ApplicationId $Connection.ApplicationId ` -CertificateThumbprint $Connection.CertificateThumbprint # 行いたい処理 Function Perform-WhatIdLikeToDo { Write-Output (Get-AzureRmVM | Select Name, ResourceGroupName, Location) } do { try { # 実行する処理 $Result = Perform-WhatIdLikeToDo Write-Output $Result $CmdOk = $True } catch { # エラー(例外)の詳細を表示 write-output "Exception Caught..." $ErrorMessage = $_.Exception.Message $StackTrace = $_.Exception.StackTrace Write-Output "Error Occurred: Message: $ErrorMessage, stack: $StackTrace." Write-Output "Retry attempts left: $NumberOfRetryAttempts" Write-Output "---------------------------------------------------------" # 次のリトライに備える $NumberOfRetryAttempts-- Start-Sleep -Seconds $RetryIntervalInSeconds } } while (-not $CmdOk -and $NumberOfRetryAttempts -ge 0) # 実行結果の表示 $EndTime = (Get-Date).ToUniversalTime() Write-Output "---------------------------------------------------------" Write-Output " Process status " Write-Output " - Success? : $CmdOk" Write-OUtput " - Elapsed time : $(($EndTime - $StartTime).TotalSeconds) [sec]" Write-Output "---------------------------------------------------------"