Quickstart voor PowerShell in Azure Cloud Shell

Dit document bevat informatie over het gebruik van PowerShell in Cloud Shell in Azure Portal.

Notitie

Een Bash in Azure Cloud Shell Quickstart is ook beschikbaar.

Cloud Shell starten

  1. Klik op Cloud Shell knop in de bovenste navigatiebalk van de Azure Portal

    Schermopname die laat zien hoe u Azure Cloud Shell de Azure Portal.

  2. Selecteer de PowerShell-omgeving in de vervolgkeuzekeuzelijn. U komt dan in Azure Drive (Azure:)

    Schermopname die laat zien hoe u de PowerShell-omgeving voor de Azure Cloud Shell.

PowerShell-opdrachten uitvoeren

Voer reguliere PowerShell-opdrachten uit in Cloud Shell, zoals:

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

Interactie met virtuele machines

U vindt al uw virtuele machines onder het huidige abonnement 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

PowerShell-script aanroepen op externe VM's

Waarschuwing

Raadpleeg Troubleshooting remote management of Azure VMs (Problemen met extern beheer van Azure-VM's oplossen).

Ervan uitgaande dat u een VM hebt, MyVM1, gaan we gebruiken om een Invoke-AzVMCommand PowerShell-scriptblok aan te roepen op de externe computer.

Enable-AzVMPSRemoting -Name MyVM1 -ResourceGroupname MyResourceGroup
Invoke-AzVMCommand -Name MyVM1 -ResourceGroupName MyResourceGroup -Scriptblock {Get-ComputerInfo} -Credential (Get-Credential)

U kunt ook eerst naar de map VirtualMachines navigeren en als Invoke-AzVMCommand volgt uitvoeren.

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                           :
 ...

Interactief aanmelden bij een externe VM

U kunt gebruiken Enter-AzVM om u interactief aan te melden bij een VM die wordt uitgevoerd in Azure.

PS Azure:\> Enter-AzVM -Name MyVM1 -ResourceGroupName MyResourceGroup -Credential (Get-Credential)

U kunt ook eerst naar de VirtualMachines map navigeren en als volgt Enter-AzVM uitvoeren:

PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Enter-AzVM -Credential (Get-Credential)

WebApps ontdekken

Door in de map in WebApps te gaan, kunt u eenvoudig door de resources van uw web-apps navigeren

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

SSH

Als u zich wilt verifiƫren bij servers of VM's met behulp van SSH, genereert u het openbare-persoonlijke sleutelpaar in Cloud Shell en publiceert u de openbare sleutel naar op de authorized_keys externe computer, zoals /home/user/.ssh/authorized_keys .

Notitie

U kunt openbare SSH-sleutels maken met ssh-keygen behulp van en deze publiceren in $env:USERPROFILE\.ssh Cloud Shell.

SSH gebruiken

Volg deze instructies om een nieuwe VM-configuratie te maken met behulp Azure PowerShell cmdlets. Voordat u New-AzVM aanroept naar om de implementatie te laten van start gaan, voegt u de openbare SSH-sleutel toe aan de VM-configuratie. De zojuist gemaakte VM bevat de openbare sleutel op de locatie, waardoor de SSH-sessie zonder referenties wordt ~\.ssh\authorized_keys inschakelen voor de 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

Beschikbare opdrachten weergeven

Typ Azure onder station om Get-AzCommand contextspecifieke Azure-opdrachten op te halen.

U kunt ook altijd gebruiken om de Get-Command *az* -Module Az.* beschikbare Azure-opdrachten te vinden.

Aangepaste modules installeren

U kunt uitvoeren Install-Module om modules te installeren vanuit de PowerShell Gallery.

Get-Help

Typ Get-Help voor informatie over PowerShell in Azure Cloud Shell.

Get-Help

Voor een specifieke opdracht kunt u nog steeds Get-Help doen gevolgd door een cmdlet.

Get-Help Get-AzVM

Gebruik Azure Files om uw gegevens op te slaan

U kunt een script maken, bijvoorbeeld , en opslaan in uw om het te helloworld.ps1 clouddrive gebruiken in shell-sessies.

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!

De volgende keer dat u PowerShell in Cloud Shell gebruikt, bestaat het bestand onder de map die helloworld.ps1 $HOME\clouddrive uw Azure Files share.

Aangepast profiel gebruiken

U kunt uw PowerShell-omgeving aanpassen door een of meer PowerShell-profielen te profile.ps1 maken: (of Microsoft.PowerShell_profile.ps1 ). Sla deze op onder (of ), zodat deze in elke PowerShell in de Cloud Shell $profile.CurrentUserAllHosts $profile.CurrentUserAllHosts geladen.

Raadpleeg Over profielen voor informatie over het maken van een profiel.

Git gebruiken

Als u een Git-repo in de Cloud Shell, moet u een persoonlijk toegangsteken maken en deze gebruiken als gebruikersnaam. Zodra u uw token hebt, kloont u de opslagplaats als volgt:

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

De shell afsluiten

Typ exit om de sessie te beƫindigen.