Utilizing Azure Cloud Shell from PowerShell

7 min read

Using Azure Cloud Shell from PowerShell

Azure Cloud Shell is a browser-based shell for managing Azure assets utilizing your favorite shell, Bash or PowerShell.
Cloud Shell is usually used from Azure Portal. It supplies an quick access to Azure CLI, Azure PowerShell and Azure AD PowerShell.

On this weblog, I’ll introduce a brand new method to entry Cloud Shell from PowerShell (requires AADInternals v0.4.3 or newer).

As per Microsoft documentation:

Azure Cloud Shell is an interactive, authenticated, browser-accessible shell for managing Azure assets. It supplies the pliability of selecting the shell expertise that most accurately fits the best way you’re employed, both Bash or PowerShell.

Cloud Shell could be accessed in 4 other ways: Utilizing a direct hyperlink (shell.azure.com),
from Azure portal, from docs.microsoft.com code snippets, and now from PowerShell with AADInternals.

Sometimes, the Cloud Shell is used from the Azure portal:
Azure Portal

The nicest characteristic of the Cloud Shell is that it comes with pre-installed PowerShell and CLI instruments for managing Azure.

As an example, itemizing the out there PowerShell modules exhibits that we’ve got all we have to handle Azure and Azure AD.

PS /dwelling/admin> Get-Module -ListAvailable
    Listing: /usr/native/share/powershell/Modules

ModuleType Model    PreRelease Title                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     4.7.0                 Az                                  Core,Desk
Script     1.9.4                 Az.Accounts                         Core,Desk {Disable-AzDataCollection, Disable-AzContex…
Script     1.1.1                 Az.Advisor                          Core,Desk {Get-AzAdvisorRecommendation, Allow-AzAdvi…
Script     1.3.0                 Az.Aks                              Core,Desk {Get-AzAksCluster, New-AzAksCluster, Take away…
Script     1.1.4                 Az.AnalysisServices                 Core,Desk {Resume-AzAnalysisServicesServer, Droop-A…
Script     2.1.0                 Az.ApiManagement                    Core,Desk {Add-AzApiManagementApiToGateway, Add-AzApi…
Script     1.1.0                 Az.ApplicationInsights              Core,Desk {Get-AzApplicationInsights, New-AzApplicati…
Script     1.4.0                 Az.Automation                       Core,Desk {Get-AzAutomationHybridWorkerGroup, Take away-…
Script     3.1.0                 Az.Batch                            Core,Desk {Take away-AzBatchAccount, Get-AzBatchAccount,…
Script     1.0.3                 Az.Billing                          Core,Desk {Get-AzBillingInvoice, Get-AzBillingPeriod,…
Script     1.4.3                 Az.Cdn                              Core,Desk {Get-AzCdnProfile, Get-AzCdnProfileSsoUrl, …
Script     1.6.0                 Az.CognitiveServices                Core,Desk {Get-AzCognitiveServicesAccount, Get-AzCogn…
Script     4.4.0                 Az.Compute                          Core,Desk {Take away-AzAvailabilitySet, Get-AzAvailabili…
Script     1.0.3                 Az.ContainerInstance                Core,Desk {New-AzContainerGroup, Get-AzContainerGroup…
Script     1.1.1                 Az.ContainerRegistry                Core,Desk {New-AzContainerRegistry, Get-AzContainerRe…
Script     1.1.0                 Az.DataBoxEdge                      Core,Desk {Get-AzDataBoxEdgeJob, Get-AzDataBoxEdgeDev…
Script     1.10.1                Az.DataFactory                      Core,Desk {Set-AzDataFactoryV2, Replace-AzDataFactoryV…
Script     1.0.2                 Az.DataLakeAnalytics                Core,Desk {Get-AzDataLakeAnalyticsDataSource, New-AzD…
Script     1.2.8                 Az.DataLakeStore                    Core,Desk {Get-AzDataLakeStoreTrustedIdProvider, Remo…
Script     1.0.0                 Az.DataShare                        Core,Desk {New-AzDataShareAccount, Get-AzDataShareAcc…
Script     1.1.0                 Az.DeploymentManager                Core,Desk {Get-AzDeploymentManagerArtifactSource, New…
Script     1.0.0                 Az.DesktopVirtualization            Core,Desk {Disconnect-AzWvdUserSession, Get-AzWvdAppl…
Script     1.0.2                 Az.DevTestLabs                      Core,Desk {Get-AzDtlAllowedVMSizesPolicy, Get-AzDtlAu…
Script     1.1.2                 Az.Dns                              Core,Desk {Get-AzDnsRecordSet, New-AzDnsRecordConfig,…
Script     1.3.0                 Az.EventGrid                        Core,Desk {New-AzEventGridTopic, Get-AzEventGridTopic…
Script     1.6.0                 Az.EventHub                         Core,Desk {New-AzEventHubNamespace, Get-AzEventHubNam…
Script     1.6.1                 Az.FrontDoor                        Core,Desk {New-AzFrontDoor, Get-AzFrontDoor, Set-AzFr…
Script     1.0.2                 Az.Capabilities                        Core,Desk {Get-AzFunctionApp, Get-AzFunctionAppAvaila…
Script     0.10.8                Az.GuestConfiguration               Core,Desk {Get-AzVMGuestPolicyStatus, Get-AzVMGuestPo…
Script     3.6.0                 Az.HDInsight                        Core,Desk {Get-AzHDInsightJob, New-AzHDInsightSqoopJo…
Script     1.1.0                 Az.HealthcareApis                   Core,Desk {New-AzHealthcareApisService, Take away-AzHeal…
Script     2.5.0                 Az.IotHub                           Core,Desk {Add-AzIotHubKey, Get-AzIotHubEventHubConsu…
Script     2.2.0                 Az.KeyVault                         Core,Desk {Add-AzKeyVaultCertificate, Replace-AzKeyVau…
Script     1.0.0                 Az.Kusto                            Core,Desk {Add-AzKustoClusterLanguageExtension, Add-A…
Script     1.3.2                 Az.LogicApp                         Core,Desk {Get-AzIntegrationAccountAgreement, Get-AzI…
Script     1.1.3                 Az.MachineLearning                  Core,Desk {Transfer-AzMlCommitmentAssociation, Get-AzMlCo…
Script     1.1.0                 Az.Upkeep                      Core,Desk {Get-AzApplyUpdate, Get-AzConfigurationAssi…
Script     1.1.0                 Az.ManagedServices                  Core,Desk {Get-AzManagedServicesAssignment, New-AzMan…
Script     1.0.2                 Az.MarketplaceOrdering              Core,Desk {Get-AzMarketplaceTerms, Set-AzMarketplaceT…
Script     1.1.1                 Az.Media                            Core,Desk {Sync-AzMediaServiceStorageKey, Set-AzMedia…
Script     2.1.0                 Az.Monitor                          Core,Desk {Get-AzMetricDefinition, Get-AzMetric, Remo…
Script     3.4.0                 Az.Community                          Core,Desk {Add-AzApplicationGatewayAuthenticationCert…
Script     1.1.1                 Az.NotificationHubs                 Core,Desk {Get-AzNotificationHub, Get-AzNotificationH…
Script     2.3.0                 Az.OperationalInsights              Core,Desk {New-AzOperationalInsightsAzureActivityLogD…
Script     1.3.1                 Az.PolicyInsights                   Core,Desk {Get-AzPolicyEvent, Get-AzPolicyState, Get-…
Script     1.1.2                 Az.PowerBIEmbedded                  Core,Desk {Take away-AzPowerBIWorkspaceCollection, Get-A…
Script     1.0.3                 Az.PrivateDns                       Core,Desk {Get-AzPrivateDnsZone, Take away-AzPrivateDnsZ…
Script     2.12.1                Az.RecoveryServices                 Core,Desk {Get-AzRecoveryServicesBackupProperty, Get-…
Script     1.2.1                 Az.RedisCache                       Core,Desk {Take away-AzRedisCachePatchSchedule, New-AzRe…
Script     1.0.3                 Az.Relay                            Core,Desk {New-AzRelayNamespace, Get-AzRelayNamespace…
Script     2.5.1                 Az.Assets                        Core,Desk {Get-AzProviderOperation, Take away-AzRoleAssi…
Script     1.4.1                 Az.ServiceBus                       Core,Desk {New-AzServiceBusNamespace, Get-AzServiceBu…
Script     2.2.0                 Az.ServiceFabric                    Core,Desk {Add-AzServiceFabricClientCertificate, Add-…
Script     1.2.0                 Az.SignalR                          Core,Desk {New-AzSignalR, Get-AzSignalR, Get-AzSignal…
Script     2.10.0                Az.Sql                              Core,Desk {Get-AzSqlDatabaseTransparentDataEncryption…
Script     1.1.0                 Az.SqlVirtualMachine                Core,Desk {New-AzSqlVM, Get-AzSqlVM, Replace-AzSqlVM, …
Script     2.6.0                 Az.Storage                          Core,Desk {Get-AzStorageAccount, Get-AzStorageAccount…
Script     1.3.0                 Az.StorageSync                      Core,Desk {Invoke-AzStorageSyncCompatibilityCheck, Ne…
Script     1.0.1                 Az.StreamAnalytics                  Core,Desk {Get-AzStreamAnalyticsFunction, Get-AzStrea…
Script     1.0.0                 Az.Help                          Core,Desk {Get-AzSupportService, Get-AzSupportProblem…
Script     1.0.4                 Az.TrafficManager                   Core,Desk {Add-AzTrafficManagerCustomHeaderToEndpoint…
Script     1.11.0                Az.Web sites                         Core,Desk {Get-AzAppServicePlan, Set-AzAppServicePlan…
Script     0.0.0.10              AzureAD.Normal.Preview            Desk      {Set-AzureADTenantDetail, Set-AzureADApplic…
Script     0.9.3                 AzurePSDrive                        Desk
Script     17.0.4716…            EXOPSSessionConnector               Desk      Join-EXOPSSession
Binary     0.1.1                 Microsoft.PowerShell.UnixCompleters Core      {Import-UnixCompleters, Take away-UnixComplete…
Manifest   1.0.840               MicrosoftPowerBIMgmt                Desk
Binary     1.0.840               MicrosoftPowerBIMgmt.Admin          Desk      {Add-PowerBIEncryptionKey, Get-PowerBIEncry…
Binary     1.0.840               MicrosoftPowerBIMgmt.Capacities     Desk      Get-PowerBICapacity
Binary     1.0.840               MicrosoftPowerBIMgmt.Information           Desk      {Add-PowerBIDataset, Set-PowerBITable, New-…
Binary     1.0.840               MicrosoftPowerBIMgmt.Profile        Desk      {Join-PowerBIServiceAccount, Disconnect-…
Binary     1.0.840               MicrosoftPowerBIMgmt.Experiences        Desk      {Get-PowerBIReport, New-PowerBIReport, Expo…
Binary     1.0.840               MicrosoftPowerBIMgmt.Workspaces     Desk      {Get-PowerBIWorkspace, Get-PowerBIWorkspace…
Binary     1.1.4                 MicrosoftTeams                      Core,Desk {Add-TeamUser, Join-MicrosoftTeams, Disc…
Script     0.9.3                 PSCloudShellUtility                 Desk      {Enter-AzVM, Get-AzCommand, Invoke-AzVMComm…
Binary     0.8.1                 SHiPS                               Desk
Script     21.1.18226            SqlServer                           Desk      {Add-RoleMember, Add-SqlAvailabilityDatabas…

    Listing: /decide/microsoft/powershell/7/Modules

ModuleType Model    PreRelease Title                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Manifest   1.2.5                 Microsoft.PowerShell.Archive        Desk      {Compress-Archive, Increase-Archive}
Manifest   7.0.0.0               Microsoft.PowerShell.Host           Core      {Begin-Transcript, Cease-Transcript}
Manifest   7.0.0.0               Microsoft.PowerShell.Administration     Core      {Add-Content material, Clear-Content material, Clear-ItemProp…
Manifest   7.0.0.0               Microsoft.PowerShell.Safety       Core      {Get-Credential, Get-ExecutionPolicy, Set-E…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        Core      {Export-Alias, Get-Alias, Import-Alias, New…
Script     1.4.7                 PackageManagement                   Desk      {Discover-Bundle, Get-Bundle, Get-PackageProv…
Script     2.2.4.1               PowerShellGet                       Desk      {Discover-Command, Discover-DSCResource, Discover-Modul…
Script     2.0.5                 PSDesiredStateConfiguration         Core      {Configuration, New-DscChecksum, Get-DscRes…
Script     2.0.2                 PSReadLine                          Desk      {Get-PSReadLineKeyHandler, Set-PSReadLineKe…
Binary     2.0.3                 ThreadJob                           Desk      Begin-ThreadJob

It’s even attainable to put in AADInternals to Cloud Shell! Nevertheless, as Cloud Shell is operating on Linux, solely .Web Core
is supported. So all the things won’t work.

Technically, the Cloud Shell is nothing however a WebSocket connection to terminal session of a Linux field in Azure.

Technical particulars

The technical course of to connect with Cloud Shell is as follows.

The 1st step

First, a HTTP request (PUT) is shipped to:

https://administration.azure.com/suppliers/Microsoft.Portal/consoles/default?api-version=2020-04-01-preview

The payload of the request is:

{"properties":{"osType":"linux"}}

The response contains an url for the gateway.

{
  "properties": {
    "osType": "Linux",
    "provisioningState": "Succeeded",
    "uri": "https://gateway12.westeurope.console.azure.com:443/n/cc-20a66344/cc-20a66344"
  }
}

Step two

Subsequent, a HTTP request (POST) is shipped to the gateway url with the dimensions data (rows and columns) and the shell sort (bash or pwsh):

https://gateway12.westeurope.console.azure.com/n/cc-20a66344/cc-20a66344/terminals?cols=124&rows=13&model=2019-01-01&shell=pwsh

The response contains the url for the terminal WebSocket.

{
	"id": "0ca733551cd94ce83f8d6a1b023ff8ef",
	"socketUri": "wss://gateway12.westeurope.console.azure.com/n/cc-20a66344/cc-20a66344/terminals/0ca733551cd94ce83f8d6a1b023ff8ef",
	"idleTimeout": "20",
	"tokenUpdated": true,
	"rootDirectory": "/dwelling/admin"
}

Step three

Subsequent, a HTTP request (POST) is shipped to the gateway for authorization:

https://gateway12.westeurope.console.azure.com/n/cc-20a66344/cc-20a66344/authorize

The response contains the authorization token.

{"token":"KtkU1c9zXEh1jidHf6xYskuZL5N4WujFsiw043VDEm0mq7MjBnbOKJR92nmoWazUL4yFEcm4PRRq4r5KJag"}

Step 4

The ultimate step is make a WebSocket connection to the gateway handle:

wss://gateway12.westeurope.console.azure.com/n/cc-20a66344/cc-20a66344/terminals/0ca733551cd94ce83f8d6a1b023ff8ef

The authorization token should be included in a cookie:

Cookie: auth-token=KtkU1c9zXEh1jidHf6xYskuZL5N4WujFsiw043VDEm0mq7MjBnbOKJR92nmoWazUL4yFEcm4PRRq4r5KJag

After the connection is made, all keystrokes from the (native) PowerShell are despatched by the WebSocket to Azure.
Additionally all the things despatched from the Azure by the WebSocket are printed to console.

The output from the Azure are VT100 primarily based management character sequences.
Fortunately, since round 2018, PowerShell console helps VT100!
So, all I needed to do was to pipe issues immediately between PowerShell console and Cloud Shell.

Making the connection

Connecting to Cloud Shell with AADInternals is fairly straight ahead:

# Get entry token 
Get-AADIntAccessTokenForCloudShell -SaveToCache

# Begin the Cloud Shell (PowerShell)
Begin-AADIntCloudShell

Cloud Shell

To begin a Bash shell as a substitute:

# Begin the Cloud Shell (Bash)
Begin-AADIntCloudShell -Shell Bash

Cloud Shell

To exit from the Cloud Shell, sort exit

You too can transfer between PowerShell and Bash in Cloud Shell. To begin a bash shell whereas in PowerShell, sort bash.
To begin PowerShell whereas in Bash, sort pwsh.

You May Also Like

More From Author

+ There are no comments

Add yours