Azure Powershell Create Or Delete Job

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