Skip to content

Instantly share code, notes, and snippets.

@tekmaven
Created June 17, 2015 02:43
Show Gist options
  • Save tekmaven/2daa41a25717de9d62e9 to your computer and use it in GitHub Desktop.
Save tekmaven/2daa41a25717de9d62e9 to your computer and use it in GitHub Desktop.
Octopus Deploy 3.0: Use system Azure Powershell module if it exists
## Octopus Azure Context script, version 1.0
## --------------------------------------------------------------------------------------
##
## This script is used to load the Azure Powershell module and select the Azure subscription
##
## The sript is passed the following parameters.
##
## $OctopusAzureModulePath = "....\Calamari\AzurePowershell\Azure.psd1"
## $OctopusAzureCertificateFileName = "...."
## $OctopusAzureCertificatePassword = "...."
## $OctopusAzureSubscriptionId = "..."
## $OctopusAzureSubscriptionName = "..."
## $OctopusAzureTargetScript = "..."
Write-Verbose "Azure context parameters: "
Write-Verbose " Subscription ID: $OctopusAzureSubscriptionId"
Write-Verbose " Subscription name: $OctopusAzureSubscriptionName"
Write-Verbose "Importing Windows Azure modules"
### Modification Start
$OverrideAzureModulePath = "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1"
if(Test-Path $OverrideAzureModulePath) {
Write-Host "Using System Azure Module - $OverrideAzureModulePath"
Import-Module $OverrideAzureModulePath
}
else {
Write-Host "Using Supplied Octopus Azure Module - $OctopusAzureModulePath"
Import-Module $OctopusAzureModulePath
}
### Modification End
if (!(Get-AzureSubscription -SubscriptionName $OctopusAzureSubscriptionName -ErrorAction SilentlyContinue)) {
Write-Verbose "Loading the management certificate"
Add-Type -AssemblyName "System"
$certificate = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @($OctopusAzureCertificateFileName, $OctopusAzureCertificatePassword, ([System.Security.Cryptography.X509Certificates.X509KeyStorageFlags] "PersistKeySet", "Exportable"))
Write-Verbose "Setting up the Azure subscription"
Set-AzureSubscription -SubscriptionName $OctopusAzureSubscriptionName -SubscriptionId $OctopusAzureSubscriptionId -Certificate $certificate
}
Select-AzureSubscription -SubscriptionName $OctopusAzureSubscriptionName
Write-Verbose "Invoking target script $OctopusAzureTargetScript"
. $OctopusAzureTargetScript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment