Modules/NuGetOps/Public/Enable-AzurePowerShell.ps1 (59 lines of code) (raw):
<#
.SYNOPSIS
Sets up the Azure PowerShell Module for accessing a set of subscriptions
.PARAMETER Certificate
A full System.Security.Cryptography.X509Certificates.X509Certificate2 object containing the certificate to use
.PARAMETER CertificateThumbprint
A string containing the thumbprint of the certificate to use (with no spaces, just a string of hex digits)
.PARAMETER Subscriptions
An array of objects, each one having a "Name" and "Id" property representing the Name and ID of a Subscription to use. If run from within the NuGet Operations Console, this is provided by the ambient environment
#>
function Enable-AzurePowerShell {
[CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="Auto")]
param(
[Parameter(Mandatory=$true, ParameterSetName="Certificate")][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
[Parameter(Mandatory=$true, ParameterSetName="Thumbprint")][string]$CertificateThumbprint,
[Parameter(Mandatory=$false)][object[]]$Subscriptions)
if((Get-Command -ErrorAction SilentlyContinue Set-AzureSubscription) -eq $null) {
throw "You must have imported the 'Azure' PowerShell module before running this command. The easiest way to do this is to run inside the NuGet Operations console."
}
if(!$Subscriptions) {
if($Global:Subscriptions) {
Write-Host "Using subscriptions from ambient NuGet Operations environment"
$Subscriptions = $Global:Subscriptions.Values;
} else {
throw "No subscriptions specified. Either run this from INSIDE the NuGet Operations console OR specify an array of objects containing Name and Id properties representing the subscriptions to enable"
}
}
if($PsCmdlet.ParameterSetName -eq "Auto") {
# Look for Certs matching the naming convention
$certs = @(dir cert:\CurrentUser\My | where { $_.Subject -like "CN=Azure-*-on-*-at-*-utc" })
if($certs.Length -eq 0) {
throw "No Azure Management Certificates found. Use the New-AzureManagementCertificate script to generate one or import the PFX file previously generated by that script into your Current User Certificate Store"
} elseif($certs.Length -gt 1) {
Write-Host "Multiple Certificates Found:"
Write-Host $certs
throw "Multiple Azure Management Certificates found. Use the -Thumbprint parameter to specify which one to use"
} else {
$Certificate = $certs[0]
}
} elseif($PsCmdlet.ParameterSetName -eq "Thumbprint") {
$path = "cert:\CurrentUser\My\$CertificateThumbprint";
if(!(Test-Path $path)) {
throw "Certificate not found: $CertificateThumbprint"
}
$Certificate = Get-Item $path
}
if(!($Certificate)) {
throw "No certificate provided or found!"
}
# Do the actual import
Write-Host "Using certificate $($Certificate.Thumbprint)"
$Subscriptions | ForEach-Object {
if(!$_.Name) { throw "Subscription object is missing 'Name' property" }
if(!$_.Id) { throw "Subscription object is missing 'Id' property" }
if($PsCmdlet.ShouldProcess($_.Name, "Set-AzureSubscription")) {
Set-AzureSubscription -SubscriptionName $_.Name -Certificate $Certificate -SubscriptionId $_.Id
}
}
}