Skip to content

Latest commit

 

History

History
341 lines (225 loc) · 12.7 KB

quickstart-powershell.md

File metadata and controls

341 lines (225 loc) · 12.7 KB
title description services documentationcenter author manager tags ms.assetid ms.service ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
PowerShell in Azure Cloud Shell (Preview) Quickstart | Microsoft Docs
Quickstart for PowerShell in Cloud Shell
Azure
maertendmsft
timlt
azure-resource-manager
azure
infrastructure-services
vm-linux
na
article
09/25/2017
damaerte

Quickstart for PowerShell in Azure Cloud Shell (Preview)

This document details how to use the PowerShell in Cloud Shell in the Azure portal.

Note

A Bash in Azure Cloud Shell Quickstart is also available.

Start Cloud Shell

  1. Click on Cloud Shell button from the top navigation bar of the Azure portal

  1. Select the PowerShell environment from the drop-down and you will be in Azure drive (Azure:)

Run PowerShell commands

Run regular PowerShell commands in the Cloud Shell, such as:

PS Azure:\> Get-Date
Monday, September 25, 2017 08:55:09 AM

PS Azure:\> Get-AzureRmVM -Status

ResourceGroupName       Name       Location                VmSize   OsType     ProvisioningState  PowerState
-----------------       ----       --------                ------   ------     -----------------  ----------
MyResourceGroup2        Demo        westus         Standard_DS1_v2  Windows    Succeeded           running
MyResourceGroup         MyVM1       eastus            Standard_DS1  Windows    Succeeded           running
MyResourceGroup         MyVM2       eastus   Standard_DS2_v2_Promo  Windows    Succeeded           deallocated

Navigate Azure resources

  1. List your subscriptions

    PS Azure:\> dir
  2. cd to your preferred subscription

    PS Azure:\> cd MySubscriptionName
    PS Azure:\MySubscriptionName>
  3. View all your Azure resources under the current subscription

    Type dir to list multiple views of your Azure resources.

    PS Azure:\MySubscriptionName> dir
    
        Directory: azure:\MySubscriptionName
    
    Mode Name
    ---- ----
    +    AllResources
    +    ResourceGroups
    +    StorageAccounts
    +    VirtualMachines
    +    WebApps

AllResources view

Type dir under AllResources directory to view your Azure resources.

PS Azure:\MySubscriptionName> dir AllResources

Explore resource groups

You can go to the ResourceGroups directory and inside a specific resource group you can find virtual machines.

PS Azure:\MySubscriptionName> cd ResourceGroups\MyResourceGroup1\Microsoft.Compute\virtualMachines

PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup1\Microsoft.Compute\virtualMachines> dir


    Directory: Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup1\Microsoft.Compute\virtualMachines


VMName    Location   ProvisioningState VMSize          OS            SKU             OSVersion AdminUserName  NetworkInterfaceName
------    --------   ----------------- ------          --            ---             --------- -------------  --------------------
TestVm1   westus     Succeeded         Standard_DS2_v2 WindowsServer 2016-Datacenter Latest    AdminUser      demo371
TestVm2   westus     Succeeded         Standard_DS1_v2 WindowsServer 2016-Datacenter Latest    AdminUser      demo271

Note

You may notice that the second time when you type dir, the cloud shell is able to display the items much faster. This is because the child items are cached in memory for a better user experience. However, you can always use dir -Force to get fresh data.

Navigate storage resources

By entering into the StorageAccounts folder you can easily navigate your storage resources

PS Azure:\MySubscriptionName\StorageAccounts\MyStorageAccountName\Files> dir

    Directory: Azure:\MySubscriptionNameStorageAccounts\MyStorageAccountName\Files


Name          ConnectionString
----          ----------------
MyFileShare1  \\MyStorageAccountName.file.core.windows.net\MyFileShare1;AccountName=MyStorageAccountName AccountKey=<key>
MyFileShare2  \\MyStorageAccountName.file.core.windows.net\MyFileShare2;AccountName=MyStorageAccountName AccountKey=<key>
MyFileShare3  \\MyStorageAccountName.file.core.windows.net\MyFileShare3;AccountName=MyStorageAccountName AccountKey=<key>

With the connection string, you can use the following command to mount the Azure File share.

net use <DesiredDriveLetter>: \\<MyStorageAccountName>.file.core.windows.net\<MyFileShareName> <AccountKey> /user:Azure\<MyStorageAccountName>

For details, see Mount an Azure File share and access the share in Windows.

You can also navigate the directories under the Azure File share as follows:

PS Azure:\MySubscriptionName\StorageAccounts\MyStorageAccountName\Files> cd .\MyFileShare1\
PS Azure:\MySubscriptionName\StorageAccounts\MyStorageAccountName\Files\MyFileShare1> dir

Mode  Name
----  ----
+     TestFolder
.     hello.ps1

    

Interact with virtual machines

You can find all your virtual machines under the current subscription via VirtualMachines directory.

PS Azure:\MySubscriptionName\VirtualMachines> dir

    Directory: Azure:\MySubscriptionName\VirtualMachines


Name       ResourceGroupName  Location  VmSize          OsType              NIC ProvisioningState  PowerState
----       -----------------  --------  ------          ------              --- -----------------  ----------
TestVm1    MyResourceGroup1   westus    Standard_DS2_v2 Windows       my2008r213         Succeeded     stopped
TestVm2    MyResourceGroup1   westus    Standard_DS1_v2 Windows          jpstest         Succeeded deallocated
TestVm10   MyResourceGroup2   eastus    Standard_DS1_v2 Windows           mytest         Succeeded     running

Invoke PowerShell script across remote VMs

Assuming you have a VM, MyVM1, let's use Invoke-AzureRmVMCommand to invoke a PowerShell scriptblock on the remote machine.

Invoke-AzureRmVMCommand -Name MyVM1 -ResourceGroupName MyResourceGroup -Scriptblock {Get-ComputerInfo} -EnableRemoting

You can also navigate to the virtualMachines directory first and run Invoke-AzureRmVMCommand as follows.

PS Azure:\> cd MySubscriptionName\MyResourceGroup\Microsoft.Compute\virtualMachines
PS Azure:\MySubscriptionName\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Invoke-AzureRmVMCommand -Scriptblock{Get-ComputerInfo}

You see output similar to the following:

PSComputerName                                          : 65.52.28.207
RunspaceId                                              : 2c2b60da-f9b9-4f42-a282-93316cb06fe1
WindowsBuildLabEx                                       : 14393.1066.amd64fre.rs1_release_sec.170327-1835
WindowsCurrentVersion                                   : 6.3
WindowsEditionId                                        : ServerDatacenter
WindowsInstallationType                                 : Server
WindowsInstallDateFromRegistry                          : 5/18/2017 11:26:08 PM
WindowsProductId                                        : 00376-40000-00000-AA947
WindowsProductName                                      : Windows Server 2016 Datacenter
WindowsRegisteredOrganization                           :
 ...

Interactively log on to a remote VM

You can use Enter-AzureRmVM to interactively log into a VM running in Azure.

Enter-AzureRmVM -Name MyVM1 -ResourceGroupName MyResourceGroup -EnableRemoting

You can also navigate to the virtualMachines directory first and run Enter-AzureRmVM as follows

PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Enter-AzureRmVM

Discover WebApps

By entering into the WebApps folder you can easily navigate your web apps resources

PS Azure:\MySubscriptionName> dir .\WebApps\

    Directory: Azure:\MySubscriptionName\WebApps


Name            State    ResourceGroup      EnabledHostNames                  Location
----            -----    -------------      ----------------                  --------
mywebapp1       Stopped  MyResourceGroup1   {mywebapp1.azurewebsites.net...   West US
mywebapp2       Running  MyResourceGroup2   {mywebapp2.azurewebsites.net...   West Europe
mywebapp3       Running  MyResourceGroup3   {mywebapp3.azurewebsites.net...   South Central US



# You can use Azure cmdlets to Start/Stop your web apps
PS Azure:\MySubscriptionName\WebApps> Start-AzureRmWebApp -Name mywebapp1 -ResourceGroupName MyResourceGroup1

Name           State    ResourceGroup        EnabledHostNames                   Location
----           -----    -------------        ----------------                   --------
mywebapp1      Running  MyResourceGroup1     {mywebapp1.azurewebsites.net ...   West US

# Refresh the current state with -Force
PS Azure:\MySubscriptionName\WebApps> dir -Force

    Directory: Azure:\MySubscriptionName\WebApps


Name            State    ResourceGroup      EnabledHostNames                  Location
----            -----    -------------      ----------------                  --------
mywebapp1       Running  MyResourceGroup1   {mywebapp1.azurewebsites.net...   West US
mywebapp2       Running  MyResourceGroup2   {mywebapp2.azurewebsites.net...   West Europe
mywebapp3       Running  MyResourceGroup3   {mywebapp3.azurewebsites.net...   South Central US

List available commands

Under Azure drive, type Get-AzureRmCommand to get context-specific Azure commands.

Alternatively, you can always use Get-Command *azurerm* -Module AzureRM.* to find out the available Azure commands.

Install custom modules

You can run Install-Module to install modules from the PowerShell Gallery.

Get-Help

Type Get-Help to get information about PowerShell in Azure Cloud Shell.

PS Azure:\> Get-Help

For a specific command, you can still do Get-Help followed by a cmdlet.

PS Azure:\> Get-Help Get-AzureRmVM

Use Azure File Storage to store your data

You can create a script, say helloworld.ps1, and save it to your CloudDrive to use it across shell sessions.

cd C:\users\ContainerAdministrator\CloudDrive
PS C:\users\ContainerAdministrator\CloudDrive> vim .\helloworld.ps1
# Add the content, such as 'Hello World!'
PS C:\users\ContainerAdministrator\CloudDrive> .\helloworld.ps1
Hello World!

Next time when you use PowerShell in Cloud Shell, the helloworld.ps1 file will exist under the CloudDrive folder that mounts your Azure File share.

Use custom profile

You can customize your PowerShell environment, by creating PowerShell profile(s) - profile.ps1 or Microsoft.PowerShell_profile.ps1. Save it under the CloudDrive so that it can be loaded in every PowerShell session when you launch the Cloud Shell.

For how to create a profile, refer to About Profiles.

Use Git

To clone a git repo in the Cloud Shell, you need to create a personal access token and use it as the username. Once you have your token, clone the repository as follows:

 git clone https://<your-access-token>@github.com/username/repo.git

Since sessions in Cloud Shell do not persist when you sign out or the session times out, the Git config file will not exist upon the next logon. To have your Git config persist, you must save your .gitconfig to your CloudDrive and copy it or create a symlink when the Cloud Shell gets launched. Use the following code snippet in your profile.ps1, to create a symlink to CloudDrive.

# .gitconfig path relative to this script
$script:gitconfigPath = Join-Path $PSScriptRoot .gitconfig

# Create a symlink to .gitconfig in user's $home
if(Test-Path $script:gitconfigPath){

   if(-not (Test-Path (Join-Path $Home .gitconfig ))){
        New-Item -ItemType SymbolicLink -Path $home -Name .gitconfig -Value $script:gitconfigPath
   }
}

Exit the shell

Type exit to terminate the session.