Skip to content

Instantly share code, notes, and snippets.

@codingoutloud
Created November 21, 2017 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingoutloud/2ee5cc42317c017e60c8c1cd1058d578 to your computer and use it in GitHub Desktop.
Save codingoutloud/2ee5cc42317c017e60c8c1cd1058d578 to your computer and use it in GitHub Desktop.
Get an SSL Cert from Key Vault and push it to a Web App
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName AzureFAQ # subscription where the Web App resides
$webAppName = "pageofphotos" # Web App resource Name, (Get-AzureRmWebApp)[0].Name
$fqdn = "pageofphotos.com" # what is the DNS address? could be admin.pageofphotos.com if that's what's mapped
$vaultName = "GlobalOpsVault"
$certificateName = "MySSL"
$passwordLength = 50
$minNumberOfNonAlphanumericCharacters = 1
# password is not remembered beyond the life of this script
$pfxPassword = [System.Web.Security.Membership]::GeneratePassword($passwordLength, $minNumberOfNonAlphanumericCharacters)
# following incantation via Subrata Sen
$pfxSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
$pfxUnprotectedBytes = [Convert]::FromBase64String($pfxSecret.SecretValueText)
$pfx = New-Object Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import($pfxUnprotectedBytes, $null, [Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$pfxProtectedBytes = $pfx.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $pfxPassword)
$pfxPath = $env:TEMP + "\$certificateName.pfx"
[IO.File]::WriteAllBytes($pfxPath, $pfxProtectedBytes)
$webAppId = (Get-AzureRmWebApp -Name $webAppName).id
$webAppResourceGroupName = (Get-AzureRmWebApp -Name $webAppName).ResourceGroup
New-AzureRmWebAppSSLBinding `
-CertificateFilePath $pfxPath `
-CertificatePassword $pfxPassword `
-Name $fqdn `
-ResourceGroupName $webAppResourceGroupName `
-WebAppName $webAppName `
-SslState SniEnabled
Remove-Item $pfxPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment