Snabb start för PowerShell i Azure Cloud ShellQuickstart for PowerShell in Azure Cloud Shell
Det här dokumentet beskriver hur du använder PowerShell i Cloud Shell i Azure Portal.This document details how to use the PowerShell in Cloud Shell in the Azure portal.
Anteckning
En bash i Azure Cloud Shell snabb start är också tillgänglig.A Bash in Azure Cloud Shell Quickstart is also available.
Starta Cloud ShellStart Cloud Shell
Klicka på Cloud Shell -knappen i det övre navigerings fältet i Azure PortalClick on Cloud Shell button from the top navigation bar of the Azure portal
Välj PowerShell-miljön i list rutan så kommer du att vara i Azure Drive
(Azure:)
Select the PowerShell environment from the drop-down and you will be in Azure drive(Azure:)
Kör PowerShell-kommandonRun PowerShell commands
Kör vanliga PowerShell-kommandon i Cloud Shell, till exempel:Run regular PowerShell commands in the Cloud Shell, such as:
PS Azure:\> Get-Date
# Expected Output
Friday, July 27, 2018 7:08:48 AM
PS Azure:\> Get-AzVM -Status
# Expected Output
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
Navigera i Azure-resurserNavigate Azure resources
Visa en lista över alla dina prenumerationer från
Azure
enhetenList all your subscriptions fromAzure
drivePS Azure:\> dir
cd
till önskad prenumerationcd
to your preferred subscriptionPS Azure:\> cd MySubscriptionName PS Azure:\MySubscriptionName>
Visa alla dina Azure-resurser under den aktuella prenumerationenView all your Azure resources under the current subscription
Skriv
dir
för att visa en lista över flera vyer för dina Azure-resurser.Typedir
to list multiple views of your Azure resources.PS Azure:\MySubscriptionName> dir Directory: azure:\MySubscriptionName Mode Name ---- ---- + AllResources + ResourceGroups + StorageAccounts + VirtualMachines + WebApps
AllResources vyAllResources view
Skriv dir
under AllResources
katalog för att se dina Azure-resurser.Type dir
under AllResources
directory to view your Azure resources.
PS Azure:\MySubscriptionName> dir AllResources
Utforska resurs grupperExplore resource groups
Du kan gå till ResourceGroups
katalogen och inuti en speciell resurs grupp för att hitta virtuella datorer.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
Anteckning
Du kan se att det går dir
att visa objekten mycket snabbare när du skriver Cloud Shell.You may notice that the second time when you type dir
, the Cloud Shell is able to display the items much faster.
Detta beror på att de underordnade objekten cachelagras i minnet för en bättre användar upplevelse.This is because the child items are cached in memory for a better user experience.
Du kan dock alltid använda dir -Force
för att hämta nya data.However, you can always use dir -Force
to get fresh data.
Navigera till lagrings resurserNavigate storage resources
Genom att ange i StorageAccounts
katalogen kan du enkelt navigera bland dina lagrings resurserBy entering into the StorageAccounts
directory, you can easily navigate all 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>
Med anslutnings strängen kan du använda följande kommando för att montera Azure Files-resursen.With the connection string, you can use the following command to mount the Azure Files share.
net use <DesiredDriveLetter>: \\<MyStorageAccountName>.file.core.windows.net\<MyFileShareName> <AccountKey> /user:Azure\<MyStorageAccountName>
Mer information finns i montera en Azure Files resurs och få åtkomst till resursen i Windows.For details, see Mount an Azure Files share and access the share in Windows.
Du kan också navigera i katalogerna under Azure Files resursen på följande sätt:You can also navigate the directories under the Azure Files share as follows:
PS Azure:\MySubscriptionName\StorageAccounts\MyStorageAccountName\Files> cd .\MyFileShare1\
PS Azure:\MySubscriptionName\StorageAccounts\MyStorageAccountName\Files\MyFileShare1> dir
Mode Name
---- ----
+ TestFolder
. hello.ps1
Interagera med virtuella datorerInteract with virtual machines
Du kan hitta alla virtuella datorer under den aktuella prenumerationen via VirtualMachines
katalog.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
Anropa PowerShell-skript över fjärranslutna virtuella datorerInvoke PowerShell script across remote VMs
Varning
Se Felsöka fjärrhantering av virtuella Azure-datorer.Please refer to Troubleshooting remote management of Azure VMs.
Förutsatt att du har en virtuell dator, MyVM1, ska vi använda Invoke-AzVMCommand
för att anropa ett PowerShell-skript block på fjärrdatorn.Assuming you have a VM, MyVM1, let's use Invoke-AzVMCommand
to invoke a PowerShell script block on the remote machine.
Enable-AzVMPSRemoting -Name MyVM1 -ResourceGroupname MyResourceGroup
Invoke-AzVMCommand -Name MyVM1 -ResourceGroupName MyResourceGroup -Scriptblock {Get-ComputerInfo} -Credential (Get-Credential)
Du kan också navigera till VirtualMachines-katalogen först och köra Invoke-AzVMCommand
enligt följande.You can also navigate to the VirtualMachines directory first and run Invoke-AzVMCommand
as follows.
PS Azure:\> cd MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines
PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Invoke-AzVMCommand -Scriptblock {Get-ComputerInfo} -Credential (Get-Credential)
# You will 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 :
...
Logga in interaktivt på en fjärran sluten datorInteractively log on to a remote VM
Du kan använda Enter-AzVM
för att interaktivt logga in på en virtuell dator som körs i Azure.You can use Enter-AzVM
to interactively log into a VM running in Azure.
PS Azure:\> Enter-AzVM -Name MyVM1 -ResourceGroupName MyResourceGroup -Credential (Get-Credential)
Du kan också navigera till VirtualMachines
katalogen först och köra Enter-AzVM
enligt följandeYou can also navigate to the VirtualMachines
directory first and run Enter-AzVM
as follows
PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Enter-AzVM -Credential (Get-Credential)
Identifiera webbapparDiscover WebApps
Genom att ange i WebApps
katalogen kan du enkelt navigera bland dina Web Apps-resurserBy entering into the WebApps
directory, 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-AzWebApp -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
SSHSSH
Om du vill autentisera till servrar eller virtuella datorer med SSH genererar du det offentliga privata nyckel paret i Cloud Shell och publicerar den offentliga nyckeln på authorized_keys
fjärrdatorn, till exempel /home/user/.ssh/authorized_keys
.To authenticate to servers or VMs using SSH, generate the public-private key pair in Cloud Shell and publish the public key to authorized_keys
on the remote machine, such as /home/user/.ssh/authorized_keys
.
Anteckning
Du kan skapa privata SSH-offentliga nycklar med ssh-keygen
och publicera dem $env:USERPROFILE\.ssh
i Cloud Shell.You can create SSH private-public keys using ssh-keygen
and publish them to $env:USERPROFILE\.ssh
in Cloud Shell.
Använda SSHUsing SSH
Följ anvisningarna här för att skapa en ny VM-konfiguration med hjälp av Azure PowerShell-cmdletar.Follow instructions here to create a new VM configuration using Azure PowerShell cmdlets.
Innan du ansluter till New-AzVM
för att starta distributionen lägger du till en offentlig SSH-nyckel i VM-konfigurationen.Before calling into New-AzVM
to kick off the deployment, add SSH public key to the VM configuration.
Den nyskapade virtuella datorn innehåller den offentliga nyckeln på ~\.ssh\authorized_keys
platsen och aktiverar därmed en kostnads fri SSH-session med autentiseringsuppgifter till den virtuella datorn.The newly created VM will contain the public key in the ~\.ssh\authorized_keys
location, thereby enabling credential-free SSH session to the VM.
# Create VM config object - $vmConfig using instructions on linked page above
# Generate SSH keys in Cloud Shell
ssh-keygen -t rsa -b 2048 -f $HOME\.ssh\id_rsa
# Ensure VM config is updated with SSH keys
$sshPublicKey = Get-Content "$HOME\.ssh\id_rsa.pub"
Add-AzVMSshPublicKey -VM $vmConfig -KeyData $sshPublicKey -Path "/home/azureuser/.ssh/authorized_keys"
# Create a virtual machine
New-AzVM -ResourceGroupName <yourResourceGroup> -Location <vmLocation> -VM $vmConfig
# SSH to the VM
ssh azureuser@MyVM.Domain.Com
Visa lista över tillgängliga kommandonList available commands
Under Azure
enhet skriver Get-AzCommand
du för att hämta Sammanhangs beroende Azure-kommandon.Under Azure
drive, type Get-AzCommand
to get context-specific Azure commands.
Alternativt kan du alltid använda Get-Command *az* -Module Az.*
för att ta reda på tillgängliga Azure-kommandon.Alternatively, you can always use Get-Command *az* -Module Az.*
to find out the available Azure commands.
Installera anpassade modulerInstall custom modules
Du kan köra Install-Module
för att installera moduler från PowerShell-galleriet.You can run Install-Module
to install modules from the PowerShell Gallery.
Get-HelpGet-Help
Skriv Get-Help
för att hämta information om PowerShell i Azure Cloud Shell.Type Get-Help
to get information about PowerShell in Azure Cloud Shell.
Get-Help
För ett speciellt kommando kan du fortfarande Get-Help
följa med en cmdlet.For a specific command, you can still do Get-Help
followed by a cmdlet.
Get-Help Get-AzVM
Använd Azure Files för att lagra dina dataUse Azure Files to store your data
Du kan skapa ett skript, t. ex. helloworld.ps1
och spara det i clouddrive
för att använda det i en Shell-session.You can create a script, say helloworld.ps1
, and save it to your clouddrive
to use it across shell sessions.
cd $HOME\clouddrive
# Create a new file in clouddrive directory
New-Item helloworld.ps1
# Open the new file for editing
code .\helloworld.ps1
# Add the content, such as 'Hello World!'
.\helloworld.ps1
Hello World!
Nästa gång du använder PowerShell i Cloud Shell, helloworld.ps1
finns filen under $HOME\clouddrive
katalogen som monterar din Azure Files-resurs.Next time when you use PowerShell in Cloud Shell, the helloworld.ps1
file will exist under the $HOME\clouddrive
directory that mounts your Azure Files share.
Använd anpassad profilUse custom profile
Du kan anpassa din PowerShell-miljö genom att skapa PowerShell-profil (er) – profile.ps1
(eller Microsoft.PowerShell_profile.ps1
).You can customize your PowerShell environment, by creating PowerShell profile(s) - profile.ps1
(or Microsoft.PowerShell_profile.ps1
).
Spara den under $profile.CurrentUserAllHosts
(eller $profile.CurrentUserAllHosts
), så att den kan läsas in i varje PowerShell i Cloud Shell-sessionen.Save it under $profile.CurrentUserAllHosts
(or $profile.CurrentUserAllHosts
), so that it can be loaded in every PowerShell in Cloud Shell session.
Information om hur du skapar en profil finns i om profiler.For how to create a profile, refer to About Profiles.
Använd gitUse Git
Om du vill klona en git-lagrings platsen i Cloud Shell måste du skapa en personlig åtkomsttoken och använda den som användar namn.To clone a Git repo in the Cloud Shell, you need to create a personal access token and use it as the username. När du har din token ska du klona databasen på följande sätt:Once you have your token, clone the repository as follows:
git clone https://<your-access-token>@github.com/username/repo.git
Lämna gränssnittetExit the shell
Skriv exit
för att avsluta sessionen.Type exit
to terminate the session.