Getting Started with Azure Shell and PnP PowerShell with Certificates

Published 05-14-2021 03:48 AM 1,239 Views

I recently encountered a scenario recently where I was looking to do a quick technical check on some advice I was giving to another member of the community regarding PowerShell – especially capturing the output of PnP PowerShell commands and store in a text file.  When I responded to the tweet, I was away from my computer, but I did have my iPad (with keyboard) with me and wanted to see if I could use PnP PowerShell with Azure Shell – you may have guessed the result by – YES you can.  

 

I was not using best security practice at the time to log in (username/password), which would not work if I had MFA on the account (which I should have ☹). I will include better practice configuration to include certificates and an Azure AD App in this article.

 

What you will need

So, let's get started; you will need a few things to get going,

  • Azure Subscription - this is necessary for a storage account that preserves your files between sessions and is mandatory for Azure cloud shell to work.
  • Azure Resource Group – the grouping of resources in Azure
  • Azure KeyVault - to store a certificate for a secure connection to M365 services.
  • PnP PowerShell - the newest PnP power shell module, this article uses v1.5.0
  • Permissions to consent an app as this uses an Azure AD app for the authentication.
  • Windows Machine – for creating the app and certificate.
  • If setting up services locally by PowerShell, then install the Az module.

Setting Up

There are a few tasks we are going to do to set up working with Azure Shell, Azure AD App and Certificates.

 

Activate Azure Shell

First, let's go ahead and activate the Azure Shell; we will use this to set up the required resources as well:

Navigate to https://shell.azure.com or click on the icon in the Azure Portal.

 

Then run through the first setup of the Azure Shell:

Welcome screen Azure ShellWelcome screen Azure Shell

Select PowerShell. Then run through the first setup of the Azure Shell:

Simple setup screen for storageSimple setup screen for storage

If you choose "Create storage" at this point, then this will set up the storage account and resource group for you, using Azure's naming standards and region. If you want to specify:

  • Subscription
  • Region
  • Resource Group
  • Storage account
  • File share name

Azure Shell Advanced SetupAzure Shell Advanced Setup

Then select Advanced settings. I like to name resources myself in a standard manner. For this article, I am going to use the advanced options and specify:

  • Region: "North Europe"
  • Resource Group Name: "azure-cloud-shell"
  • Storage Account: “pkbtenantcloudshell”
  • File Share: "pkbtenantcloudshellfiles"

Note: the naming of some of these resources is very strict, e.g., 3 -24 characters, no spaces, lowercase.

Click "Create Storage"

Azure Shell - Setup CompleteAzure Shell - Setup Complete

Now the Azure Shell is ready to use.

 

Installing PnP-PowerShell

Next, we need to install the PnP PowerShell module in Azure Cloud shell, enter:

 

Install-Module -Name PnP.PowerShell

 

The great thing about Azure Cloud Shell is the installation is persistent between sessions, so you will only have to do this once. However, you will need to upgrade the module periodically.

 

Azure Shell - Install PnP PowerShellAzure Shell - Install PnP PowerShell

For information on installing PnP-PowerShell, check out the documentation site for more details: https://pnp.github.io/powershell/

 

Create a KeyVault

Next, we want somewhere secure to store a certificate – Azure KeyVault.

A KeyVault is a secure method of storing keys, secrets and certificates. We intend to keep the certificate used for this setup in the vault. Another benefit of using this method, in larger organisations, where other teams manage apps or the security aspect of the tenant, they can set up this vault and retain control over the certificate and app creation.

You can, if you prefer, navigate to the Azure Portal and use the marketplace to create a new KeyVault resource; check out this quick start guide.

OR set this up in Azure Cloud Shell run the following commands:

Firstly, to check that the service is available in your preferred region, run the following:

 

New-AzKeyVault -VaultName 'pkbtenant-keyvault' -ResourceGroupName 'azure-cloud-shell' -Location 'North Europe'

 

Then, provision a new KeyVault (if you have one already, then skip this step) and give yourself access to Secrets and Certificates.

 

Set-AzKeyVaultAccessPolicy -VaultName 'pkbtenant-keyvault' -UserPrincipalName 'paul.bullock@tenant.co.uk' -PermissionsToCertificates All -PermissionsToSecrets All

 

 

Azure Shell - Create KeyVaultAzure Shell - Create KeyVault

Please refer to the documentation if you want to be more specific around the KeyVault – Azure Portal | PowerShell.

We will be importing a certificate in a later section.

 

Create Azure AD App with Register-PnPAzureApp

There are a few options when setting up the authentication to connect with PnP PowerShell:

  • Azure AD App – using your app (recommended), which will use the APPLICATION permissions, meaning the connection will use the permissions the app does.
  • PnP Management Shell – the multi-tenant app PnP provides – this uses DELEGATE permissions meaning the connection will need to log in as the user and will only have access to services THE user has access to.

If you need clarification on the difference between the types of permissions, I highly recommend checking out an awesome community call demo from Philippe Signoret, Program Manager of the Microsoft Identity team: https://youtu.be/_BfI4L7j1Po

 

So why are use an Azure AD app? Using the Azure Shell restricts some authentication options when connecting to services with PnP PowerShell, such as interactive login, because it cannot display a pop-up window.

 

Azure Shell - doesn't support interactive loginAzure Shell - doesn't support interactive login

The "-PnPManagementShell" parameter is an option; this uses the device login method BUT will require you to navigate to another site/page to authenticate, including going back to grab the code – then enter your login credentials further steps if MFA is enabled. IMHO feels a bit cumbersome to do this each time I want to do a task in the Shell, especially on an iPad/phone.


Create an Azure AD App

To set up the app quickly with PnP PowerShell, you need to use a Windows machine to run the cmdlet "Register-PnPAzureApp" which generates the certificate, creates the Azure AD app, sets API permissions, will pop up to consent to the app permissions.

Note: you may have to install the PnP PowerShell locally if you do not already have it.

 

To set up the app, run the following command using the PnP PowerShell cmdlet:

 

$result = Register-PnPAzureADApp -ApplicationName "PnP PowerShell Azure Shell Access" -Tenant yourtenant.co.uk -OutPath .\ -DeviceLogin -ValidYears 2 -CertificatePassword (ConvertTo-SecureString -String "yourpassword" -AsPlainText -Force)

$result #output the result – Specifically grab the AzureAppId/ClientId – you will need this later

 

 

PowerShell - Registering Azure AppPowerShell - Registering Azure App

During this operation, two windows will pop-up to authenticate with the device login method and consent to the app permissions.

 

If you want to check the app in Azure AD, navigate to: https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps and find the app called "PnP PowerShell Azure Shell Access"

 

Note: This app uses the minimum API permissions (APPLICATION) as the default; if you want to perform operations with groups or flow as an example, you will need to add these permissions to the app.

 

Azure App - API permissionsAzure App - API permissions

For more details on generating the app, check out the authentication section of the documentation.

 

Adding Certificate to KeyVault

Now that we have the app setup, we need to upload the certificate to the KeyVault.

Navigate to the KeyVault in the portal

 

Azure KeyVault - Navigating to CertificatesAzure KeyVault - Navigating to Certificates

  • Select "Import"
  • Enter a name "azureshell-pnpps-connection"
  • Select the generated certificate "PnP PowerShell Azure Shell Access.pfx"
  • Enter the certificate password

Click Create

 

Azure Key Vault - Upload CertificateAzure Key Vault - Upload Certificate

This is ready to use in a later section.

 

Reducing the time to get going on new sessions

When I start the Azure Shell, I want to minimize the number of lines, to connect to the services securely and get going quickly.

Parameter Splatting

Parameter splatting is a method to pass a collection of parameters for a PowerShell command into a variable and apply this to the cmdlet you intend to run. Example:

 

Get-NiceMessage -Param1 "good" -Param2 "morning" -Param3 "community"

# Splatting alternative
$MyParams = @{
	Param1 = "good",
	Param2 = "morning",
	Param3 = "community"	
}
Get-NiceMessage @MyParams

 

This will save time if you repeatedly apply the same parameters on the cmdlets, reducing the time to write the command and your scripts cleaner to read.  To read more about this PowerShell feature, check out the documentation.

 

PowerShell Profiles

Interestingly, I did not know about this feature until the MOTD message appeared on the Azure Shell. I wanted to understand this further so dug deeper and I have managed to find a way to create a startup script that will make it easier to connect to Office 365.

With profiles, we can setup a script to run when the Azure Shell is started, so this is an awesome opportunity to add all of the connection information when the Shell starts including retrieval, of the certificates from the KeyVault.

 

To create a profile that is used across sessions but for your user account use:

 

# Create Profile
mkdir (Split-Path $profile.CurrentUserAllHosts)
New-Item -ItemType File -Path $PROFILE.CurrentUserAllHosts -Force

 

 

Azure Shell - Setup ProfilesAzure Shell - Setup Profiles

Once created, you can open an editor in the path above to the new profile script location.

 

Then using the following example, using a combination of parameter splatting and profiles, you can setup everything you need to connect to the service:

 

# Connect to KeyVault Data
try{

    $vaultName = "pkbtenant-keyvault" # Replace with your KeyVault name
    $appName = "PnP PowerShell Azure Shell Access" 
    $appId = (Get-AzADApplication -DisplayName $appName).ApplicationId
    $tenantId = (Get-AzContext).Tenant.Id
    $certName = "azureshell-pnpps-connection"
    $baseSite = "https://tenant.sharepoint.com" # Replace with your tenant
    $base64Cert = Get-AzKeyVaultSecret -VaultName $vaultName -Name $certName -AsPlainText

    # Use of parameter splatting
    $ConnectInfo = @{
        ClientId = $appId
        CertificateBase64Encoded = $base64Cert
        Tenant = $tenantId
    }

    Write-Host "Ready to connect to M365/SharePoint" -ForegroundColor Green
}catch{
    Write-Host "Failed to get the KeyVault data" -ForegroundColor Yellow
}

 

Editor – Ctrl-S (Save) then Ctrl-Q (Quit)

Restart Shell

After setting up the profile, reboot your Shell and you should see a prompt to indicate the prerequisites are ready to use for a connection (green text).

 

To learn more about profiles check out the documentation for profiles.

 

Connecting to the service

Once you have all these elements setup, you can connect with PnP PowerShell with one short line:

 

Connect-PnPOnline @ConnectInfo https://tenant.sharepoint.com

# -OR-

Connect-PnPOnline @ConnectInfo $baseSite

 

Azure Shell - connecting with PnP PowerShellAzure Shell - connecting with PnP PowerShell

The settings are persistent across sessions/devices (like an Azure app on iPad), so once setup, you can open Azure Shell and reconnect to the service and make the changes in the Shell quickly.

 

Btw, at the beginning I mentioned about capturing the output to the text file, this is particularly useful if you are grabbing a lot of info in your session or need to show the changes you made, here is how to do it:

 

Start-Transcript "log.txt"
Get-PnPWeb
Stop-Transcript

 

This will save the file to the current location in the Azure Shell. Use Export-File to download it, or the UI button.

Enjoy!

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2315482%22%20slang%3D%22en-US%22%3EGetting%20Started%20with%20Azure%20Shell%20and%20PnP%20PowerShell%20with%20Certificates%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2315482%22%20slang%3D%22en-US%22%3E%3CP%3EI%20recently%20encountered%20a%20scenario%20recently%20where%20I%20was%20looking%20to%20do%20a%20quick%20technical%20check%20on%20some%20advice%20I%20was%20giving%20to%20another%20member%20of%20the%20community%20regarding%20PowerShell%20%E2%80%93%20especially%20capturing%20the%20output%20of%20PnP%20PowerShell%20commands%20and%20store%20in%20a%20text%20file.%26nbsp%3B%20When%20I%20responded%20to%20the%20tweet%2C%20I%20was%20away%20from%20my%20computer%2C%20but%20I%20did%20have%20my%20iPad%20(with%20keyboard)%20with%20me%20and%20wanted%20to%20see%20if%20I%20could%20use%20PnP%20PowerShell%20with%20Azure%20Shell%20%E2%80%93%20you%20may%20have%20guessed%20the%20result%20by%20%E2%80%93%20YES%20you%20can.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20was%20not%20using%20best%20security%20practice%20at%20the%20time%20to%20log%20in%20(username%2Fpassword)%2C%20which%20would%20not%20work%20if%20I%20had%20MFA%20on%20the%20account%20(which%20I%20should%20have%20%E2%98%B9).%20I%20will%20include%20better%20practice%20configuration%20to%20include%20certificates%20and%20an%20Azure%20AD%20App%20in%20this%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--466963695%22%20id%3D%22toc-hId--470627831%22%3EWhat%20you%20will%20need%3C%2FH2%3E%0A%3CP%3ESo%2C%20let's%20get%20started%3B%20you%20will%20need%20a%20few%20things%20to%20get%20going%2C%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAzure%20Subscription%3C%2FSTRONG%3E%20-%20this%20is%20necessary%20for%20a%20storage%20account%20that%20preserves%20your%20files%20between%20sessions%20and%20is%20mandatory%20for%20Azure%20cloud%20shell%20to%20work.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EAzure%20Resource%20Group%20%3C%2FSTRONG%3E%E2%80%93%20the%20grouping%20of%20resources%20in%20Azure%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EAzure%20KeyVault%3C%2FSTRONG%3E%20-%20to%20store%20a%20certificate%20for%20a%20secure%20connection%20to%20M365%20services.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EPnP%20PowerShell%3C%2FSTRONG%3E%20-%20the%20newest%20PnP%20power%20shell%20module%2C%20this%20article%20uses%20v1.5.0%3C%2FLI%3E%0A%3CLI%3EPermissions%20to%20%3CSTRONG%3E%3CEM%3Econsent%3C%2FEM%3E%3C%2FSTRONG%3E%20an%20app%20as%20this%20uses%20an%20Azure%20AD%20app%20for%20the%20authentication.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EWindows%20Machine%3C%2FSTRONG%3E%20%E2%80%93%20for%20creating%20the%20app%20and%20certificate.%3C%2FLI%3E%0A%3CLI%3EIf%20setting%20up%20services%20locally%20by%20PowerShell%2C%20then%20%3CA%20href%3D%22https%3A%2F%2Fwww.powershellgallery.com%2Fpackages%2FAz%2F5.8.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Einstall%20the%20Az%20module%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId-2020549138%22%20id%3D%22toc-hId-2016885002%22%3ESetting%20Up%3C%2FH2%3E%0A%3CP%3EThere%20are%20a%20few%20tasks%20we%20are%20going%20to%20do%20to%20set%20up%20working%20with%20Azure%20Shell%2C%20Azure%20AD%20App%20and%20Certificates.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1583856684%22%20id%3D%22toc-hId--1587520820%22%3EActivate%20Azure%20Shell%3C%2FH3%3E%0A%3CP%3EFirst%2C%20let's%20go%20ahead%20and%20activate%20the%20Azure%20Shell%3B%20we%20will%20use%20this%20to%20set%20up%20the%20required%20resources%20as%20well%3A%3C%2FP%3E%0A%3CP%3ENavigate%20to%20%3CA%20href%3D%22https%3A%2F%2Fshell.azure.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fshell.azure.com%3C%2FA%3E%20or%20click%20on%20the%20icon%20in%20the%20Azure%20Portal.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorPaul%20Bullock_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EThen%20run%20through%20the%20first%20setup%20of%20the%20Azure%20Shell%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20Shell%20-%20First%20Welcome.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280437iFD9F0ECE9A07CDCD%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20Shell%20-%20First%20Welcome.png%22%20alt%3D%22Welcome%20screen%20Azure%20Shell%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EWelcome%20screen%20Azure%20Shell%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESelect%20PowerShell.%26nbsp%3BThen%20run%20through%20the%20first%20setup%20of%20the%20Azure%20Shell%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20Shell%20-%20Simple%20Setup.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280438i3656B18BEB0A9634%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20Shell%20-%20Simple%20Setup.png%22%20alt%3D%22Simple%20setup%20screen%20for%20storage%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ESimple%20setup%20screen%20for%20storage%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIf%20you%20choose%20%22Create%20storage%22%20at%20this%20point%2C%20then%20this%20will%20set%20up%20the%20storage%20account%20and%20resource%20group%20for%20you%2C%20using%20Azure's%20naming%20standards%20and%20region.%20If%20you%20want%20to%20specify%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESubscription%3C%2FLI%3E%0A%3CLI%3ERegion%3C%2FLI%3E%0A%3CLI%3EResource%20Group%3C%2FLI%3E%0A%3CLI%3EStorage%20account%3C%2FLI%3E%0A%3CLI%3EFile%20share%20name%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20Shell%20Advanced%20Settings.png%22%20style%3D%22width%3A%20981px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280439i150A0689B838C458%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20Shell%20Advanced%20Settings.png%22%20alt%3D%22Azure%20Shell%20Advanced%20Setup%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20Advanced%20Setup%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThen%20select%20Advanced%20settings.%20I%20like%20to%20name%20resources%20myself%20in%20a%20standard%20manner.%20For%20this%20article%2C%20I%20am%20going%20to%20use%20the%20advanced%20options%20and%20specify%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ERegion%3A%20%22North%20Europe%22%3C%2FLI%3E%0A%3CLI%3EResource%20Group%20Name%3A%20%22azure-cloud-shell%22%3C%2FLI%3E%0A%3CLI%3EStorage%20Account%3A%20%E2%80%9Cpkbtenantcloudshell%E2%80%9D%3C%2FLI%3E%0A%3CLI%3EFile%20Share%3A%20%22pkbtenantcloudshellfiles%22%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ENote%3A%20the%20naming%20of%20some%20of%20these%20resources%20is%20very%20strict%2C%20e.g.%2C%203%20-24%20characters%2C%20no%20spaces%2C%20lowercase.%3C%2FP%3E%0A%3CP%3EClick%20%22Create%20Storage%22%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20Shell%20-%20Setup%20Complete.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280440i048CB9009640E9C7%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20Shell%20-%20Setup%20Complete.png%22%20alt%3D%22Azure%20Shell%20-%20Setup%20Complete%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20-%20Setup%20Complete%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ENow%20the%20Azure%20Shell%20is%20ready%20to%20use.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-903656149%22%20id%3D%22toc-hId-899992013%22%3EInstalling%20PnP-PowerShell%3C%2FH3%3E%0A%3CP%3ENext%2C%20we%20need%20to%20install%20the%20PnP%20PowerShell%20module%20in%20Azure%20Cloud%20shell%2C%20enter%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EInstall-Module%20-Name%20PnP.PowerShell%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20great%20thing%20about%20Azure%20Cloud%20Shell%20is%20the%20installation%20is%20persistent%20between%20sessions%2C%20so%20you%20will%20only%20have%20to%20do%20this%20once.%20However%2C%20you%20will%20need%20to%20upgrade%20the%20module%20periodically.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Installing%20PnP%20PowerShell%20module.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280441iF46A15B70C052DDA%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Installing%20PnP%20PowerShell%20module.png%22%20alt%3D%22Azure%20Shell%20-%20Install%20PnP%20PowerShell%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20-%20Install%20PnP%20PowerShell%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EFor%20information%20on%20installing%20PnP-PowerShell%2C%20check%20out%20the%20documentation%20site%20for%20more%20details%3A%20%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fpnp.github.io%2Fpowershell%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--903798314%22%20id%3D%22toc-hId--907462450%22%3ECreate%20a%20KeyVault%3C%2FH3%3E%0A%3CP%3ENext%2C%20we%20want%20somewhere%20secure%20to%20store%20a%20certificate%20%E2%80%93%20Azure%20KeyVault.%3CBR%20%2F%3E%3CBR%20%2F%3EA%20KeyVault%20is%20a%20secure%20method%20of%20storing%20keys%2C%20secrets%20and%20certificates.%20We%20intend%20to%20keep%20the%20certificate%20used%20for%20this%20setup%20in%20the%20vault.%20Another%20benefit%20of%20using%20this%20method%2C%20in%20larger%20organisations%2C%20where%20other%20teams%20manage%20apps%20or%20the%20security%20aspect%20of%20the%20tenant%2C%20they%20can%20set%20up%20this%20vault%20and%20retain%20control%20over%20the%20certificate%20and%20app%20creation.%3C%2FP%3E%0A%3CP%3EYou%20can%2C%20if%20you%20prefer%2C%20navigate%20to%20the%20Azure%20Portal%20and%20use%20the%20marketplace%20to%20create%20a%20new%20KeyVault%20resource%3B%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Fazure%2Fkey-vault%2Fgeneral%2Fquick-create-portal%23sign-in-to-azure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Echeck%20out%20this%20quick%20start%20guide%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EOR%20set%20this%20up%20in%20Azure%20Cloud%20Shell%20run%20the%20following%20commands%3A%3C%2FP%3E%0A%3CP%3EFirstly%2C%20to%20check%20that%20the%20service%20is%20available%20in%20your%20preferred%20region%2C%20run%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ENew-AzKeyVault%20-VaultName%20'pkbtenant-keyvault'%20-ResourceGroupName%20'azure-cloud-shell'%20-Location%20'North%20Europe'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%2C%20provision%20a%20new%20KeyVault%20(if%20you%20have%20one%20already%2C%20then%20skip%20this%20step)%20and%20give%20yourself%20access%20to%20Secrets%20and%20Certificates.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ESet-AzKeyVaultAccessPolicy%20-VaultName%20'pkbtenant-keyvault'%20-UserPrincipalName%20'paul.bullock%40tenant.co.uk'%20-PermissionsToCertificates%20All%20-PermissionsToSecrets%20All%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22New%20KeyVault%20-%20AzShell.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280442i5F85D789B10B2B5D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22New%20KeyVault%20-%20AzShell.png%22%20alt%3D%22Azure%20Shell%20-%20Create%20KeyVault%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20-%20Create%20KeyVault%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EPlease%20refer%20to%20the%20documentation%20if%20you%20want%20to%20be%20more%20specific%20around%20the%20KeyVault%20%E2%80%93%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Fazure%2Fkey-vault%2Fgeneral%2Fquick-create-portal%23create-a-vault%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Portal%3C%2FA%3E%20%7C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Faz.keyvault%2F%3Fview%3Dazps-5.9.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EPowerShell%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EWe%20will%20be%20importing%20a%20certificate%20in%20a%20later%20section.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1583714519%22%20id%3D%22toc-hId-1580050383%22%3ECreate%20Azure%20AD%20App%20with%20Register-PnPAzureApp%3C%2FH3%3E%0A%3CP%3EThere%20are%20a%20few%20options%20when%20setting%20up%20the%20authentication%20to%20connect%20with%20PnP%20PowerShell%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAzure%20AD%20App%3C%2FSTRONG%3E%20%E2%80%93%20using%20your%20app%20(recommended)%2C%20which%20will%20use%20the%20APPLICATION%20permissions%2C%20meaning%20the%20connection%20will%20use%20the%20permissions%20the%20app%20does.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EPnP%20Management%20Shell%3C%2FSTRONG%3E%20%E2%80%93%20the%20multi-tenant%20app%20PnP%20provides%20%E2%80%93%20this%20uses%20DELEGATE%20permissions%20meaning%20the%20connection%20will%20need%20to%20log%20in%20as%20the%20user%20and%20will%20only%20have%20access%20to%20services%20THE%20user%20has%20access%20to.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIf%20you%20need%20clarification%20on%20the%20difference%20between%20the%20types%20of%20permissions%2C%20I%20highly%20recommend%20checking%20out%20an%20awesome%20community%20call%20demo%20from%20Philippe%20Signoret%2C%20Program%20Manager%20of%20the%20Microsoft%20Identity%20team%3A%20%3CA%20href%3D%22https%3A%2F%2Fyoutu.be%2F_BfI4L7j1Po%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fyoutu.be%2F_BfI4L7j1Po%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20why%20are%20use%20an%20Azure%20AD%20app%3F%20Using%20the%20Azure%20Shell%20restricts%20some%20authentication%20options%20when%20connecting%20to%20services%20with%20PnP%20PowerShell%2C%20such%20as%20interactive%20login%2C%20because%20it%20cannot%20display%20a%20pop-up%20window.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22PnP%20PowerShell%20-%20cannot%20use%20Azure%20Shell.png%22%20style%3D%22width%3A%20887px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280443i78DB2E453BCA80E9%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22PnP%20PowerShell%20-%20cannot%20use%20Azure%20Shell.png%22%20alt%3D%22Azure%20Shell%20-%20doesn't%20support%20interactive%20login%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20-%20doesn't%20support%20interactive%20login%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20%22-PnPManagementShell%22%20parameter%20is%20an%20option%3B%20this%20uses%20the%20device%20login%20method%20BUT%20will%20require%20you%20to%20navigate%20to%20another%20site%2Fpage%20to%20authenticate%2C%20including%20going%20back%20to%20grab%20the%20code%20%E2%80%93%20then%20enter%20your%20login%20credentials%20further%20steps%20if%20MFA%20is%20enabled.%20IMHO%20feels%20a%20bit%20cumbersome%20to%20do%20this%20each%20time%20I%20want%20to%20do%20a%20task%20in%20the%20Shell%2C%20especially%20on%20an%20iPad%2Fphone.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--223739944%22%20id%3D%22toc-hId--227404080%22%3E%3CBR%20%2F%3ECreate%20an%20Azure%20AD%20App%3C%2FH3%3E%0A%3CP%3ETo%20set%20up%20the%20app%20quickly%20with%20PnP%20PowerShell%2C%20you%20need%20to%20use%20a%20%3CSTRONG%3E%3CEM%3EWindows%20machine%3C%2FEM%3E%3C%2FSTRONG%3E%20to%20run%20the%20cmdlet%20%3CEM%3E%22Register-PnPAzureApp%22%3C%2FEM%3E%20which%20generates%20the%20certificate%2C%20creates%20the%20Azure%20AD%20app%2C%20sets%20API%20permissions%2C%20will%20pop%20up%20to%20consent%20to%20the%20app%20permissions.%3C%2FP%3E%0A%3CP%3ENote%3A%20you%20may%20have%20to%20install%20the%20PnP%20PowerShell%20locally%20if%20you%20do%20not%20already%20have%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20set%20up%20the%20app%2C%20run%20the%20following%20command%20using%20the%20PnP%20PowerShell%20cmdlet%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24result%20%3D%20Register-PnPAzureADApp%20-ApplicationName%20%22PnP%20PowerShell%20Azure%20Shell%20Access%22%20-Tenant%20yourtenant.co.uk%20-OutPath%20.%5C%20-DeviceLogin%20-ValidYears%202%20-CertificatePassword%20(ConvertTo-SecureString%20-String%20%22yourpassword%22%20-AsPlainText%20-Force)%0A%0A%24result%20%23output%20the%20result%20%E2%80%93%20Specifically%20grab%20the%20AzureAppId%2FClientId%20%E2%80%93%20you%20will%20need%20this%20later%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Register%20App%20-%20Windows.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280444iFAFA3BF7106E24A4%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Register%20App%20-%20Windows.png%22%20alt%3D%22PowerShell%20-%20Registering%20Azure%20App%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPowerShell%20-%20Registering%20Azure%20App%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EDuring%20this%20operation%2C%20two%20windows%20will%20pop-up%20to%20authenticate%20with%20the%20device%20login%20method%20and%20consent%20to%20the%20app%20permissions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20check%20the%20app%20in%20Azure%20AD%2C%20navigate%20to%3A%20%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%23blade%2FMicrosoft_AAD_IAM%2FActiveDirectoryMenuBlade%2FRegisteredApps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fportal.azure.com%2F%23blade%2FMicrosoft_AAD_IAM%2FActiveDirectoryMenuBlade%2FRegisteredApps%3C%2FA%3E%20and%20find%20the%20app%20called%20%22PnP%20PowerShell%20Azure%20Shell%20Access%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%3A%20This%20app%20uses%20the%20minimum%20API%20permissions%20(APPLICATION)%20as%20the%20default%3B%20if%20you%20want%20to%20perform%20operations%20with%20groups%20or%20flow%20as%20an%20example%2C%20you%20will%20need%20to%20add%20these%20permissions%20to%20the%20app.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20App%20Permissions%20-%20Application.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280445i52EBAC099476F437%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20App%20Permissions%20-%20Application.png%22%20alt%3D%22Azure%20App%20-%20API%20permissions%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20App%20-%20API%20permissions%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EFor%20more%20details%20on%20generating%20the%20app%2C%20check%20out%20the%20%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2Farticles%2Fauthentication.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eauthentication%20section%20of%20the%20documentation%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--234243048%22%20id%3D%22toc-hId--237907184%22%3EAdding%20Certificate%20to%20KeyVault%3C%2FH2%3E%0A%3CP%3ENow%20that%20we%20have%20the%20app%20setup%2C%20we%20need%20to%20upload%20the%20certificate%20to%20the%20KeyVault.%3C%2FP%3E%0A%3CP%3ENavigate%20to%20the%20KeyVault%20in%20the%20portal%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20KeyVault%20-%20Certificates.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280446i5CE2E9BF2C10AD95%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20KeyVault%20-%20Certificates.png%22%20alt%3D%22Azure%20KeyVault%20-%20Navigating%20to%20Certificates%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20KeyVault%20-%20Navigating%20to%20Certificates%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESelect%20%22Import%22%3C%2FLI%3E%0A%3CLI%3EEnter%20a%20name%20%22azureshell-pnpps-connection%22%3C%2FLI%3E%0A%3CLI%3ESelect%20the%20generated%20certificate%20%22PnP%20PowerShell%20Azure%20Shell%20Access.pfx%22%3C%2FLI%3E%0A%3CLI%3EEnter%20the%20certificate%20password%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EClick%20Create%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22KeyVault-Importing%20Certificate.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280447i46604D4A6DF19734%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22KeyVault-Importing%20Certificate.png%22%20alt%3D%22Azure%20Key%20Vault%20-%20Upload%20Certificate%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Key%20Vault%20-%20Upload%20Certificate%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThis%20is%20ready%20to%20use%20in%20a%20later%20section.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2041697511%22%20id%3D%22toc-hId--2045361647%22%3EReducing%20the%20time%20to%20get%20going%20on%20new%20sessions%3C%2FH2%3E%0A%3CP%3EWhen%20I%20start%20the%20Azure%20Shell%2C%20I%20want%20to%20minimize%20the%20number%20of%20lines%2C%20to%20connect%20to%20the%20services%20securely%20and%20get%20going%20quickly.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1351136037%22%20id%3D%22toc-hId--1354800173%22%3EParameter%20Splatting%3C%2FH3%3E%0A%3CP%3EParameter%20splatting%20is%20a%20method%20to%20pass%20a%20collection%20of%20parameters%20for%20a%20PowerShell%20command%20into%20a%20variable%20and%20apply%20this%20to%20the%20cmdlet%20you%20intend%20to%20run.%20Example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EGet-NiceMessage%20-Param1%20%22good%22%20-Param2%20%22morning%22%20-Param3%20%22community%22%0A%0A%23%20Splatting%20alternative%0A%24MyParams%20%3D%20%40%7B%0A%20Param1%20%3D%20%22good%22%2C%0A%20Param2%20%3D%20%22morning%22%2C%0A%20Param3%20%3D%20%22community%22%20%0A%7D%0AGet-NiceMessage%20%40MyParams%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20save%20time%20if%20you%20repeatedly%20apply%20the%20same%20parameters%20on%20the%20cmdlets%2C%20reducing%20the%20time%20to%20write%20the%20command%20and%20your%20scripts%20cleaner%20to%20read.%26nbsp%3B%20To%20read%20more%20about%20this%20PowerShell%20feature%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_splatting%3Fview%3Dpowershell-7.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Echeck%20out%20the%20documentation%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-441213317%22%20id%3D%22toc-hId-437549181%22%3EPowerShell%20Profiles%3C%2FH3%3E%0A%3CP%3EInterestingly%2C%20I%20did%20not%20know%20about%20this%20feature%20until%20the%20MOTD%20message%20appeared%20on%20the%20Azure%20Shell.%20I%20wanted%20to%20understand%20this%20further%20so%20dug%20deeper%20and%20I%20have%20managed%20to%20find%20a%20way%20to%20create%20a%20startup%20script%20that%20will%20make%20it%20easier%20to%20connect%20to%20Office%20365.%3C%2FP%3E%0A%3CP%3EWith%20profiles%2C%20we%20can%20setup%20a%20script%20to%20run%20when%20the%20Azure%20Shell%20is%20started%2C%20so%20this%20is%20an%20awesome%20opportunity%20to%20add%20all%20of%20the%20connection%20information%20when%20the%20Shell%20starts%20including%20retrieval%2C%20of%20the%20certificates%20from%20the%20KeyVault.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20create%20a%20profile%20that%20is%20used%20across%20sessions%20but%20for%20your%20user%20account%20use%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Create%20Profile%0Amkdir%20(Split-Path%20%24profile.CurrentUserAllHosts)%0ANew-Item%20-ItemType%20File%20-Path%20%24PROFILE.CurrentUserAllHosts%20-Force%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20Shell%20-%20Setup%20Profile.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280449i288B73A558036A56%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20Shell%20-%20Setup%20Profile.png%22%20alt%3D%22Azure%20Shell%20-%20Setup%20Profiles%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20-%20Setup%20Profiles%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EOnce%20created%2C%20you%20can%20open%20an%20editor%20in%20the%20path%20above%20to%20the%20new%20profile%20script%20location.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20using%20the%20following%20example%2C%20using%20a%20combination%20of%20parameter%20splatting%20and%20profiles%2C%20you%20can%20setup%20everything%20you%20need%20to%20connect%20to%20the%20service%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Connect%20to%20KeyVault%20Data%0Atry%7B%0A%0A%20%20%20%20%24vaultName%20%3D%20%22pkbtenant-keyvault%22%20%23%20Replace%20with%20your%20KeyVault%20name%0A%20%20%20%20%24appName%20%3D%20%22PnP%20PowerShell%20Azure%20Shell%20Access%22%20%0A%20%20%20%20%24appId%20%3D%20(Get-AzADApplication%20-DisplayName%20%24appName).ApplicationId%0A%20%20%20%20%24tenantId%20%3D%20(Get-AzContext).Tenant.Id%0A%20%20%20%20%24certName%20%3D%20%22azureshell-pnpps-connection%22%0A%20%20%20%20%24baseSite%20%3D%20%22https%3A%2F%2Ftenant.sharepoint.com%22%20%23%20Replace%20with%20your%20tenant%0A%20%20%20%20%24base64Cert%20%3D%20Get-AzKeyVaultSecret%20-VaultName%20%24vaultName%20-Name%20%24certName%20-AsPlainText%0A%0A%20%20%20%20%23%20Use%20of%20parameter%20splatting%0A%20%20%20%20%24ConnectInfo%20%3D%20%40%7B%0A%20%20%20%20%20%20%20%20ClientId%20%3D%20%24appId%0A%20%20%20%20%20%20%20%20CertificateBase64Encoded%20%3D%20%24base64Cert%0A%20%20%20%20%20%20%20%20Tenant%20%3D%20%24tenantId%0A%20%20%20%20%7D%0A%0A%20%20%20%20Write-Host%20%22Ready%20to%20connect%20to%20M365%2FSharePoint%22%20-ForegroundColor%20Green%0A%7Dcatch%7B%0A%20%20%20%20Write-Host%20%22Failed%20to%20get%20the%20KeyVault%20data%22%20-ForegroundColor%20Yellow%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEditor%20%E2%80%93%20Ctrl-S%20(Save)%20then%20Ctrl-Q%20(Quit)%3C%2FP%3E%0A%3CP%3ERestart%20Shell%3C%2FP%3E%0A%3CP%3EAfter%20setting%20up%20the%20profile%2C%20reboot%20your%20Shell%20and%20you%20should%20see%20a%20prompt%20to%20indicate%20the%20prerequisites%20are%20ready%20to%20use%20for%20a%20connection%20(green%20text).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20learn%20more%20about%20profiles%20check%20out%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_profiles%3Fview%3Dpowershell-7%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Edocumentation%20for%20profiles%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1366241146%22%20id%3D%22toc-hId--1369905282%22%3EConnecting%20to%20the%20service%3C%2FH3%3E%0A%3CP%3EOnce%20you%20have%20all%20these%20elements%20setup%2C%20you%20can%20connect%20with%20PnP%20PowerShell%20with%20one%20short%20line%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EConnect-PnPOnline%20%40ConnectInfo%20https%3A%2F%2Ftenant.sharepoint.com%0A%0A%23%20-OR-%0A%0AConnect-PnPOnline%20%40ConnectInfo%20%24baseSite%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Azure%20Shell%20-%20Connect.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280450i17FC10FF54B9CF61%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Azure%20Shell%20-%20Connect.png%22%20alt%3D%22Azure%20Shell%20-%20connecting%20with%20PnP%20PowerShell%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Shell%20-%20connecting%20with%20PnP%20PowerShell%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20settings%20are%20persistent%20across%20sessions%2Fdevices%20(like%20an%20Azure%20app%20on%20iPad)%2C%20so%20once%20setup%2C%20you%20can%20open%20Azure%20Shell%20and%20reconnect%20to%20the%20service%20and%20make%20the%20changes%20in%20the%20Shell%20quickly.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBtw%2C%20at%20the%20beginning%20I%20mentioned%20about%20capturing%20the%20output%20to%20the%20text%20file%2C%20this%20is%20particularly%20useful%20if%20you%20are%20grabbing%20a%20lot%20of%20info%20in%20your%20session%20or%20need%20to%20show%20the%20changes%20you%20made%2C%20here%20is%20how%20to%20do%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EStart-Transcript%20%22log.txt%22%0AGet-PnPWeb%0AStop-Transcript%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20save%20the%20file%20to%20the%20current%20location%20in%20the%20Azure%20Shell.%20Use%20Export-File%20to%20download%20it%2C%20or%20the%20UI%20button.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EEnjoy!%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2315482%22%20slang%3D%22en-US%22%3E%3CP%3ELearn%20how%20to%20setup%20Azure%20Shell%20with%20PnP%20PowerShell%20including%20setup%20of%20a%20Azure%20AD%20App%20with%20certificates%20stored%20in%20a%20KeyVault.%20This%20allows%20you%20to%20subsequently%20login%20quick%20in%20future%20Azure%20Shell%20sessions%20with%20PnP%20PowerShell%20to%20perform%20operations.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2315482%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EGetting%20started%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎May 13 2021 04:11 AM
Updated by: