Skip to content

Instantly share code, notes, and snippets.

@jkbryan
Last active January 30, 2019 23:14
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 jkbryan/d391a15226a5cb08d86c1dac451a3538 to your computer and use it in GitHub Desktop.
Save jkbryan/d391a15226a5cb08d86c1dac451a3538 to your computer and use it in GitHub Desktop.
Creates an Azure Service Principle named <AppName> in the Subscription
$Subscription = "<Subscription-GUID>"
$PathToPFXCertificate = "C:\<PATH>\<CertName>.pfx"
$PFXPassword = "<Password>"
$CertPassword = ConvertTo-SecureString $PFXPassword -AsPlainText -Force
$ApplicationName = "<AppName>"
Import-Module AzureRM.Resources
Connect-AzureRmAccount
Set-AzureRmContext -Subscription $Subscription
$PFXCert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @($PathToPFXCertificate, $CertPassword)
$KeyValue = [System.Convert]::ToBase64String($PFXCert.GetRawCertData())
$ServicePrincipal = New-AzureRMADServicePrincipal -DisplayName $ApplicationName
New-AzureRmADSpCredential -ObjectId $ServicePrincipal.Id -CertValue $KeyValue -StartDate $PFXCert.NotBefore -EndDate $PFXCert.NotAfter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment