Azure Powershell Automation to Create or Delete Job

  1. workflow CreateorDeleteJob  
  2. {  
  3.    # Specify input parameters here  
  4.    param (  
  5.       [parameter(Mandatory=$true)]  
  6.       [boolean]$IsCreate,  
  7.       [parameter(Mandatory=$true)]  
  8.       [string]$SubscriptionName="Windows Azure Enterprise",  
  9.       [parameter(Mandatory=$true)]  
  10.       [string]$StorageAccount = "storageName",  
  11.       [parameter(Mandatory=$true)]  
  12.       [string]$ConfigFileName = "ServiceConfiguration.cscfg",  
  13.       [parameter(Mandatory=$true)]  
  14.       [string]$BlobContainer = "package",  
  15.       [parameter(Mandatory=$true)]  
  16.       [string]$service = "DevService",  
  17.       [parameter(Mandatory=$true)]  
  18.       [string]$slot = "production" ,  
  19.       [parameter(Mandatory=$true)]  
  20.       [string]$packageFileName = "DevService.cspkg"  
  21.    )  
  22.    $TempFileLocation = "C:\temp\ServiceConfiguration.cscfg"  
  23.    $package = "https://$StorageAccount.blob.core.windows.net/$BlobContainer/$packageFileName"  
  24.    $timeStampFormat = "g"  
  25.    $deploymentLabel = "PowerShell Deploy to $service"  
  26.    Write-Output "Slot: $slot"  
  27.    Write-Output "Service: $service"  
  28.    Write-Output "Package: $package"  
  29.    Write-Output "TempFileLocation: $TempFileLocation"  
  30.    Write-Output "create or delete : $IsCreate"  
  31.    function Copy-FileFromAzureStorageToSandbox($ConfigFileName, $BlobContainer, $TempFileLocation){  
  32. $blob = Get-AzureStorageBlobContent -Blob $ConfigFileName -Container $BlobContainer -Destination $TempFileLocation -Force  
  33.    Write-Output $TempFileLocation  
  34.    return $TempFileLocation  
  35. }  
  36. function CreateNewDeployment($slot, $package, $TempFileLocation, $deploymentLabel, $service, $timeStampFormat)  
  37. {  
  38.    write-progress -id 3 -activity "Creating New Deployment" -Status "In progress"  
  39.    Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: In progress"  
  40. $opstat = New-AzureDeployment -Slot $slot -Package $package -Configuration $TempFileLocation -label $deploymentLabel -ServiceName $service  
  41.    $completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot  
  42.    $completeDeploymentID = $completeDeployment.deploymentid  
  43.    write-progress -id 3 -activity "Creating New Deployment" -completed -Status "Complete"  
  44.    Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: Complete, Deployment ID: $completeDeploymentID"  
  45.    write-progress -id 4 -activity "Starting Instances" -status $opstat  
  46.    Write-Output "$(Get-Date -f $timeStampFormat) - Starting Instances: $opstat"  
  47. }  
  48. function UpgradeDeployment($slot, $package, $TempFileLocation, $deploymentLabel, $service, $timeStampFormat)  
  49. {  
  50.    write-progress -id 3 -activity "Upgrading Deployment" -Status "In progress"  
  51.    Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: In progress"  
  52.    # perform Update-Deployment  
  53. $setdeployment = Set-AzureDeployment -Upgrade -Slot $slot -Package $package -Configuration $TempFileLocation -label $deploymentLabel -ServiceName $service -Force  
  54.    $completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot  
  55.    $completeDeploymentID = $completeDeployment.deploymentid  
  56.    write-progress -id 3 -activity "Upgrading Deployment" -completed -Status "Complete"  
  57.    Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: Complete, Deployment ID: $completeDeploymentID"  
  58. }  
  59. function SuspendDeployment($slot, $service)  
  60. {  
  61.    write-progress -id 1 -activity "Suspending Deployment" -status "In progress"  
  62.    Write-Output "$(Get-Date -f $timeStampFormat) - Suspending Deployment: In progress"  
  63.    $suspend = Set-AzureDeployment -Slot $slot -ServiceName $service -Status Suspended  
  64.    write-progress -id 1 -activity "Suspending Deployment" -status "Suspended"  
  65.    Write-Output "$(Get-Date -f $timeStampFormat) - Suspending Deployment"  
  66. }  
  67. # START starting point of code ------------------------------------------------------------------  
  68. # do not run deployment on saturday or sunday  
  69. if($(Get-Date).DayOfWeek.ToString() -ne "Saturday" -and $(Get-Date).DayOfWeek.ToString() -ne "Sunday")  
  70. {  
  71.    $AzureConn = Get-AutomationConnection -Name "Test"  
  72.    if ($AzureConn -eq $null)  
  73.    {  
  74.       throw "Could not retrieve '$AzureConnectionName' connection asset. Check that you created this first in the Automation service."  
  75.    }  
  76.    # Get the Azure management certificate that is used to connect to this subscription  
  77.    $Certificate = Get-AutomationCertificate -Name $AzureConn.AutomationCertificateName  
  78.    if ($Certificate -eq $null)  
  79.    {  
  80.       throw "Could not retrieve '$AzureConn.AutomationCertificateName' certificate asset. Check that you created this first in the Automation service."  
  81.    }  
  82.    # $cred = Get-AutomationPSCredential -Name $UserName  
  83.    # Add-AzureAccount -Credential $cred  
  84. Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $AzureConn.SubscriptionID -Certificate $Certificate -CurrentStorageAccount $StorageAccount  
  85.    # Set-AzureSubscription –SubscriptionName $SubscriptionName -CurrentStorageAccount $StorageAccount  
  86. Select-AzureSubscription -SubscriptionName $SubscriptionName  
  87.    if($IsCreate)  
  88.    {  
  89.       Write-Output "Create Azure Deployment"  
  90. #Publish $slot $package $TempFileLocation $deploymentLabel $service $timeStampFormat $ConfigFileName $BlobContainer  
  91.       $deployment = Get-AzureDeployment -ServiceName $service -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue  
  92.       Copy-FileFromAzureStorageToSandbox -ConfigFileName $ConfigFileName -BlobContainer $BlobContainer -TempFileLocation $TempFileLocation  
  93.       if ($deployment.Name -ne $null) {  
  94.          #Update deployment inplace (usually faster, cheaper, won't destroy VIP)  
  95.          Write-Output "$(Get-Date -f $timeStampFormat) - Deployment exists in $service. Upgrading deployment."  
  96.          UpgradeDeployment $slot $package $TempFileLocation $deploymentLabel $service $timeStampFormat  
  97.       } else {  
  98.          Write-Output "$(Get-Date -f $timeStampFormat) - No deployment is detected. Creating a new deployment. "  
  99.          CreateNewDeployment $slot $package $TempFileLocation $deploymentLabel $service $timeStampFormat  
  100.       }  
  101.       # remove the config file from temp sandbox location  
  102.       remove-item -path $TempFileLocation -force  
  103.       #update-BlobFiles -BlobContainer $BlobContainer -FileName $packageFileName  
  104.       #update-BlobFiles -BlobContainer $BlobContainer -FileName $ConfigFileName  
  105.    }  
  106.    else  
  107.    {  
  108.       # SuspendDeployment $slot $service  
  109.       Write-Output "$(Get-Date -f $timeStampFormat) - Deleting the $slot deployment "  
  110.       Remove-AzureDeployment -ServiceName $service -Slot $slot -Force  
  111.       write-progress -id 3 -activity "Deleting Deployment" -completed -Status "Complete"  
  112.    }  
  113. }  
  114. # END ending point of code ------------------------------------------------------------------  
  115. }