Quickstart voor PowerShell in Azure Cloud Shell
In dit document wordt beschreven hoe u PowerShell gebruikt in Cloud Shell in de Azure Portal.
Notitie
Er is ook een Bash in Azure Cloud Shell Quickstart beschikbaar.
Cloud Shell starten
Klik op Cloud Shell knop in de bovenste navigatiebalk van het Azure Portal
Selecteer de PowerShell-omgeving in de vervolgkeuzelijst en u bevindt zich in het Azure-station
(Azure:)
PowerShell-opdrachten uitvoeren
Voer reguliere PowerShell-opdrachten uit in de 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
de 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 het oplossen van problemen met extern beheer van Azure-VM's.
Ervan uitgaande dat u een VIRTUELE machine hebt, MyVM1, gaan Invoke-AzVMCommand
we een PowerShell-scriptblok aanroepen 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 volgt uitvoeren Invoke-AzVMCommand
.
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 zich Enter-AzVM
interactief aanmelden 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 uitvoeren Enter-AzVM
:
PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Enter-AzVM -Credential (Get-Credential)
WebApps ontdekken
Door de map in te WebApps
voeren, kunt u eenvoudig navigeren in uw resources voor web-apps
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 wilt verifiƫren bij servers of VM's met behulp van SSH, genereert u het sleutelpaar met een openbare persoonlijke sleutel in Cloud Shell en publiceert u de openbare sleutel op authorized_keys
de externe computer, zoals /home/user/.ssh/authorized_keys
.
Notitie
U kunt persoonlijke SSH-sleutels ssh-keygen
maken en publiceren naar $env:USERPROFILE\.ssh
Cloud Shell.
SSH gebruiken
Volg hier de instructies voor het maken van een nieuwe VM-configuratie met behulp van Azure PowerShell cmdlets.
Voordat u aanroept New-AzVM
om de implementatie te starten, voegt u de openbare SSH-sleutel toe aan de VM-configuratie.
De zojuist gemaakte VM bevat de openbare sleutel op de ~\.ssh\authorized_keys
locatie, waardoor de SSH-sessie zonder referenties wordt ingeschakeld voor de virtuele machine.
# 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 Get-AzCommand
onder Azure
station om contextspecifieke Azure-opdrachten op te halen.
U kunt ook altijd de Get-Command *az* -Module Az.*
beschikbare Azure-opdrachten vinden.
Aangepaste modules installeren
U kunt Install-Module
uitvoeren om modules te installeren vanuit de PowerShell Gallery.
Get-Help
Typ Get-Help
het type voor informatie over PowerShell in Azure Cloud Shell.
Get-Help
Voor een specifieke opdracht kunt u nog steeds een cmdlet uitvoeren Get-Help
.
Get-Help Get-AzVM
Azure Files gebruiken om uw gegevens op te slaan
U kunt een script maken, bijvoorbeeld helloworld.ps1
, en opslaan in uw clouddrive
script om het te 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 helloworld.ps1
bestand onder de $HOME\clouddrive
map die uw Azure Files-share koppelt.
Aangepast profiel gebruiken
U kunt uw PowerShell-omgeving aanpassen door PowerShell-profiel(s) - profile.ps1
(of Microsoft.PowerShell_profile.ps1
) te maken.
Sla deze op onder $profile.CurrentUserAllHosts
(of$profile.CurrentUserCurrentHost
), zodat deze in elke PowerShell in Cloud Shell sessie kan worden geladen.
Raadpleeg Over profielen voor informatie over het maken van een profiel.
Git gebruiken
Als u een Git-opslagplaats in de Cloud Shell wilt klonen, moet u een persoonlijk toegangstoken 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
het type om de sessie te beƫindigen.