How do I connect to Exchange and SharePoint PNP using Certificates from Azure KeyVault or Azure Automation

Viewed 8

What is the format to connect to exchange and PnPOnline with certificates from azure KeyVault or from azure automation?

There is very little information on how to connect to PnPOnline using CertificateBase64Encoded. I want to know how to accomplish this from both KeyVault and Automation.

1 Answers
#Variable section for all below
#Some call it appid others clientid balls in your court
$appid = '0000-your-guid-for-app' 
$tenantid = 'xxxxyyyyy.onmicrosoft.com'
#Connect to exchange with cert from Azure Automation
$aaCert = Get-AutomationCertificate -Name cert
Connect-ExchangeOnline -Certificate $aaCert -AppID $appID -Organization $tenantID
#Connect to exchange with cert from Azure KeyVault
$kvCert = Get-AzKeyVaultSecret -VaultName akeyvault -Name Cert -AsPlainText
$kvCertRawData = [Convert]::fromBase64String($kvcert)
$kvCertPFX = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($kvCertRawData)
Connect-ExchangeOnline -Certificate $certPFX -AppID $appID -Organization $tenantID
#Connect to PNP with cert from Azure KeyVault, yes you upload your cert to certificates not secrets but you do call the secrets to get it out with private key
$kvCert = Get-AzKeyVaultSecret -VaultName akeyvault -Name Cert -AsPlainText
Connect-PnPOnline -Url $sharePointURLs -ClientId $appid -Tenant $tenantdid -CertificateBase64Encoded $kvCert
Get-PnPGroup -Identity 'yessss'
#Connect to PNP with cert from Azure Automation
$aaCert = Get-AutomationCertificate -Name cert
# Special sauce is the export of 3 which is in pkcs12 format
$aaCertBase64 = [Convert]::toBase64String($aacert.Export(3))
Connect-PnPOnline -Url $sharePointURLs -ClientId $appid -Tenant $tenantdid -CertificateBase64Encoded $aaCertBase64
Get-PnPGroup -Identity 'yessss'
Related