Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Using PowerShell with Azure Cloud Shell
Microsoft Azure profile imageMichael Bender
Michael Bender forMicrosoft Azure

Posted on

     

Using PowerShell with Azure Cloud Shell

We've selected our favorite tips and tricks created byMichael Crump as well as planned fresh technical content on Azure all April! Miss a day (or more)?Catch up with the series.

Don't have Azure?Grab a free subscription.

Azure Cloud Shell includes two command line languages for you to use: PowerShell and Bash. Traditionally, Bash has been the preferred method within Cloud Shell due to the rich set of tools in Azure CLI. With the update of the Azure PowerShell tools, there is feature parity between the two options so let's take a look at PowerShell in Azure Cloud Shell.

Accessing PowerShell in Azure Cloud Shell

To access PowerShell in Azure Cloud Shell, you have a few options.

  1. You can typepwsh in the shell, and you'll be authenticated into an instance of Cloud Shell with PowerShell without leaving the current Cloud Shell session.
    Switching from Bash to PowerShell in Cloud Shell

  2. Another option is to re-launch the Cloud Shell window by choosingPowerShell from the dropdown menu
    Choosing PowerShell from Dropdown

and choosing 'Confirm' to restart. This will reconnect your session to the current container using PowerShell.
Restart Session with PowerShell

Once you are authenticated, you'll be able to explore the resources available in Azure using PowerShell.

To verify the version of PowerShell you are running, type$PSVersionTable.

PSAzure:\>$PSVersionTableNameValue---------PSVersion6.2.0PSEditionCoreGitCommitId6.2.0OSLinux4.15.0-1041-azure#45-Ubuntu SMP Fri Mar 15 14:41:00 UTC 2019PlatformUnixPSCompatibleVersions{1.0,2.0,3.0,4.0}PSRemotingProtocolVersion2.3SerializationVersion1.1.0.1WSManStackVersion3.0
Enter fullscreen modeExit fullscreen mode

Notice that the OS for the Cloud Shell instance is Linux. That is because cloud shell uses PowerShell Core inside of a Linux container. Many of the same tools you would use in PowerShell Core on your machine are available as you can see with all of the installed modules.

PSAzure:\>Get-ModuleModuleTypeVersionNameExportedCommands-------------------------------------Script1.5.1Az.Accounts{Add-AzEnvironment,Clear-AzContext,Clear-AzDefault,Connect-AzAccount}Script1.8.0Az.Compute{Add-AzContainerServiceAgentPoolProfile,Add-AzImageDataDisk,Add-AzVhd,Add-AzVMAdditiScript1.7.0Az.Network{Add-AzApplicationGatewayAuthenticationCertificate,Add-AzApplicationGatewayBackendAddrScript1.3.1Az.Resources{Add-AzADGroupMember,Export-AzResourceGroup,Get-AzADAppCredential,Get-AzADApplicatioScript1.2.0Az.Storage{Add-AzRmStorageContainerLegalHold,Add-AzStorageAccountManagementPolicyAction,Add-AzSScript0.0.0.9AzureAD.Standard.Preview{Add-AzureADAdministrativeUnitMember,Add-AzureADApplicationOwner,Add-AzureADApplicatiScript0.9.3AzurePSDriveManifest6.1.0.0Microsoft.PowerShell.Management{Add-Content,Clear-Content,Clear-Item,Clear-ItemProperty}Manifest6.1.0.0Microsoft.PowerShell.Utility{Add-Member,Add-Type,Clear-Variable,Compare-Object}Script0.9.3PSCloudShellUtility{Disable-AzVMPSRemoting,Dismount-CloudDrive,Enable-AzVMPSRemoting,Enter-AzVM}Script2.0.0PSReadLine{Get-PSReadLineKeyHandler,Get-PSReadLineOption,Remove-PSReadLineKeyHandler,Set-PSReaBinary0.8.1SHiPS
Enter fullscreen modeExit fullscreen mode

Also, you have built-in tools like the Azure PowerShell module for managing your resources in Azure.

To view all the Azure PowerShell commands, typeGet-Command -Module AZ* | more

PSAzure:\>Get-Command-ModuleAZ*|moreCommandTypeNameVersionSource----------------------------AliasAdd-AdlAnalyticsDataSource1.0.0Az.DataLakeAnalyticsAliasAdd-AdlAnalyticsFirewallRule1.0.0Az.DataLakeAnalyticsAliasAdd-AdlStoreFirewallRule1.2.0Az.DataLakeStore
Enter fullscreen modeExit fullscreen mode

What's great about this is that you don't have to worry about updating these tools; It's all done for you.

Exploring Azure Drive

When you log into Cloud Shell, you'll have access to Azure Drive. Azure Drive gives you file system-like access to all your tenant resources. It does this by building a PowerShell Drive, based on SHiPS (Simple Hierarchy in PowerShell). So you can do a directory listing and see all of your subscriptions like this:

Azure:/PSAzure:\>dirDirectory:Azure:ModeSubscriptionNameSubscriptionIdTenantIdState-----------------------------------------------+ca-mibender-demo-testxxxx-xxxx-xxxx-xxxx-xxxxxxxx-xxxx-xxxx-xxxx-xxxxEnabled
Enter fullscreen modeExit fullscreen mode

Then drill down into the directories to view resources deployed in Azure like Virtual Machines by using cd

Azure:/PSAzure:\>cd./ca-mibender-demo-test/VirtualMachines/Azure:/ca-mibender-demo-test/VirtualMachinesPSAzure:\>dirDirectory:Azure:/ca-mibender-demo-test/VirtualMachinesNameResourceGroupNameLocationVmSizeOsTypeNICProvisioningStatePowerState-----------------------------------------------------------------------myVMCLOUDSHELL-DEMO-MICHAELwesteuropeStandard_A1LinuxmyVM-nicSucceededrunningvm-linux-01AZURE-CLOUDSHELL-DEMOwestus2Standard_DS1_v2Linuxvm-linux-01336Succeededdeallocatedvm-linux-02AZURE-CLOUDSHELL-DEMOwestus2Standard_DS1_v2Linuxvm-linux-02181Succeededrunningvm-win-01AZURE-CLOUDSHELL-DEMOwestus2Standard_DS1_v2Windowsvm-win-01543Succeededrunningvm-win-02AZURE-CLOUDSHELL-DEMOwestus2Standard_DS1_v2Windowsvm-win-02953Succeededrunning
Enter fullscreen modeExit fullscreen mode

Now if you want to see the properties of a VM, let's say vm-linux-01, you can use theGet-AzVm command for that

PSAzure:\>get-AzVm-Namevm-linux-01ResourceGroupNameNameLocationVmSizeOsTypeNICProvisioningStateZone-----------------------------------------------------------------AZURE-CLOUDSHELL-DEMOvm-linux-01westus2Standard_DS1_v2Linuxvm-linux-01336Succeeded
Enter fullscreen modeExit fullscreen mode

Accessing Azure Cloud Drive

Another feature of Cloud Shell is CloudDrive. CloudDrive is a 5GB Azure File Share that providespersistent storage to your Cloud Shell instance. This allows you to store scripts, install tooling, and other file-management tasks, and have all of this available every time you access Cloud Shell.

To get to your Cloud Drive, type incd $Home to access the home drive where Cloud Drive resides. Then typecd ./CloudDrive to enter your persistent CloudDrive storage.

PSAzure:\>cd$homePS/home/michael>dirDirectory:/home/michaelModeLastWriteTimeLengthName---------------------------d----l4/25/191:24PMclouddrivePS/home/michael>cd./clouddrive/PS/home/michael/clouddrive>dirDirectory:/home/michael/clouddriveModeLastWriteTimeLengthName---------------------------d-----4/15/192:13PMDemoDirectoryd-----4/11/199:50PMscriptsPS/home/michael/clouddrive>
Enter fullscreen modeExit fullscreen mode

And if you are still not sold on using PowerShell, you can still run your Bash, and Azure CLI commands in the PowerShell console. It supports both!

PSAzure:\>azgroupcreate--namemyResourceGroup--locationeastus{"id":"/subscriptions/XXXX-XXXX-XXXX-XXXX/resourceGroups/myResourceGroup","location":"eastus","managedBy":null,"name":"myResourceGroup","properties":{"provisioningState":"Succeeded"},"tags":null,"type":null}
Enter fullscreen modeExit fullscreen mode

That will get you started using PowerShell in Azure Cloud Shell!

Want more PowerShell in Azure Cloud Shell? Check out ourquickstarts and tutorials!


We'll be posting articles every day in April, so stay tuned or jump ahead and check out more tips and tricksnow.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
omiossec profile image
Olivier Miossec
Former Microsoft Azure MVP.Find me on https://github.com/omiossec, https://www.youtube.com/@AzurePlatformOps or https://www.linkedin.com/in/omiossec/Living in Amsterdam, NL
  • Location
    Amsterdam
  • Work
    Cloud Architect, Former Microsoft Azure MVP
  • Joined

Great introduction, thank

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Invent with purpose

Any language. Any platform.

More fromMicrosoft Azure

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp