Created
February 15, 2017 15:09
-
-
Save davoodharun/41d466436efea724d4c3f721faf40f65 to your computer and use it in GitHub Desktop.
Execute Azure Automation Job from Octopus Deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$clientId = $OctopusParameters['AzureGovernment.Account.ClientId'] | |
$tenantId = $OctopusParameters['AzureGovernment.Account.TenantId'] | |
$environmentName = $OctopusParameters['AzureGovernment.Account.EnvironmentName'] | |
$key = $OctopusParameters['AzureGovernment.Account.KeyValue'] | |
$subscriptionId = $OctopusParameters['AzureGovernment.SubscriptionId'] | |
$automationAccountResourceGroupName = $OctopusParameters['AzureGovernment.Automation.AutomationAccountResourceGroupName'] | |
$automationAccountName = $OctopusParameters['AzureGovernment.Automation.AutomationAccountName'] | |
$runbookName = $OctopusParameters['AzureGovernment.AutomationRunbookName'] | |
$targetResourceGroupName = $OctopusParameters['AzureGovernment.TargetResourceGroupName'] | |
$templateResourceGroupName = $OctopusParameters['AzureGovernment.TemplateResourceGroupName'] | |
$templateStorageAccountName = $OctopusParameters['AzureGovernment.TemplateStorageAccountName'] | |
$templateStorageContainer = $OctopusParameters['AzureGovernment.TemplateStorageContainer'] | |
$scriptStorageContainer = $OctopusParameters['AzureGovernment.ScriptStorageContainer'] | |
$templateFileName = $OctopusParameters['AzureGovernment.TemplateFileName'] | |
$requireSasToken = $OctopusParameters['AzureGovernment.requireSasToken'] | |
$templateParametersFileName = $OctopusParameters['AzureGovernment.TemplateParametersFileName'] | |
$JobPollingIntervalInSeconds = $OctopusParameters['AzureGovernment.AutomationJobPollingInterval'] | |
$SecurePassword = $key | ConvertTo-SecureString -AsPlainText -Force | |
$cred = new-object -typename System.Management.Automation.PSCredential ` | |
-argumentlist $clientID, $SecurePassword | |
Add-AzureRmAccount -Credential $cred -Tenant $tenantId -ServicePrincipal -EnvironmentName $environmentName | |
Select-AzureRmSubscription -SubscriptionId $subscriptionId | |
$params = @{'subscriptionId'=$subscriptionId; | |
'targetResourceGroupName'=$targetResourceGroupName; | |
'templateResourceGroupName'=$templateResourceGroupName; | |
'templateStorageAccountName'=$templateStorageAccountName; | |
'templateStorageContainer'=$templateStorageContainer; | |
'templateFileName'=$templateFileName; | |
'templateParametersFileName'=$templateParametersFileName; | |
'requireSasToken'=$requireSasToken; | |
'scriptStorageContainer'=$scriptStorageContainer; | |
} | |
$job = Start-AzureRmAutomationRunbook -ResourceGroupName $automationAccountResourceGroupName -AutomationAccountName $automationAccountName -Name $runbookName -Parameters $params | |
if ($job -eq $null) { | |
# No job was created, so throw an exception | |
throw ("No job was created for runbook: $runbookName.") | |
} | |
else { | |
$doLoop = $true | |
while($doLoop) { | |
Write-Output "Automation job ($($job.jobId) / $($job.RunbookName)) status: $($job.Status) " | |
Start-Sleep -s $JobPollingIntervalInSeconds | |
$job = Get-AzureRmAutomationJob -Id $job.JobId -ResourceGroupName $automationAccountResourceGroupName -AutomationAccountName $automationAccountName | |
$doLoop = (($job.Status -notmatch "Completed") ` | |
-and ($job.Status -notmatch "Failed") ` | |
-and ($job.Status -notmatch "Suspended") ` | |
-and ($job.Status -notmatch "Stopped")) | |
} | |
Write-Output "Automation job ($($job.jobId) / $($job.RunbookName)) status: $($job.Status) " | |
} | |
$jobOutput = Get-AzureRmAutomationJobOutput -Id $job.JobId -ResourceGroupName $automationAccountResourceGroupName -AutomationAccountName $automationAccountName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment