Virtuális gép létrehozása pillanatképből a PowerShell használatával (Linux)

Ez a szkript létrehoz egy virtuális gépet egy operációsrendszer-lemez pillanatképéből.

Ha nem rendelkezik Azure-előfizetéssel, először hozzon létre egy ingyenes fiókot.

Példaszkript

#Provide the subscription Id
$subscriptionId = 'yourSubscriptionId'

#Provide the name of your resource group
$resourceGroupName ='yourResourceGroupName'

#Provide the name of the snapshot that will be used to create OS disk
$snapshotName = 'yourSnapshotName'

#Provide the name of the OS disk that will be created using the snapshot
$osDiskName = 'yourOSDiskName'

#Provide the name of an existing virtual network where virtual machine will be created
$virtualNetworkName = 'yourVNETName'

#Provide the name of the virtual machine
$virtualMachineName = 'yourVMName'

#Provide the size of the virtual machine
#e.g. Standard_DS3
#Get all the vm sizes in a region using below script:
#e.g. Get-AzVMSize -Location westus
$virtualMachineSize = 'Standard_DS3'

#Set the context to the subscription Id where Managed Disk will be created
Select-AzSubscription -SubscriptionId $SubscriptionId

$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName

$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy

$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName

#Initialize virtual machine configuration
$VirtualMachine = New-AzVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize

#Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows

#Create a public IP for the VM
$publicIp = New-AzPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic

#Get the virtual network where virtual machine will be hosted
$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName

# Create NIC in the first subnet of the virtual network
$nic = New-AzNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id

$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $nic.Id

#Create the virtual machine with Managed Disk
New-AzVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location

Az üzemelő példány eltávolítása

Az alábbi paranccsal eltávolítható az erőforráscsoport, a virtuális gép és az összes kapcsolódó erőforrás.

Remove-AzResourceGroup -Name myResourceGroup

Szkript ismertetése

Ez a szkript a következő parancsokat használja a pillanatkép tulajdonságainak lekért létrehozásához, felügyelt lemez pillanatképből való létrehozásához és virtuális gép létrehozásához. A táblázatban lévő összes elem a hozzá tartozó dokumentációra hivatkozik.

Parancs Jegyzetek
Get-AzSnapshot Pillanatképet készít a pillanatkép nevével.
New-AzDiskConfig Lemezkonfigurációt hoz létre. Ez a konfiguráció a lemez-létrehozási folyamattal együtt használatos.
New-AzDisk Létrehoz egy felügyelt lemezt.
New-AzVMConfig Egy virtuálisgép-konfigurációt hoz létre. Ebben a konfigurációban olyan információk szerepelnek, mint a virtuális gép neve, az operációs rendszer és a rendszergazdai hitelesítő adatok. A rendszer a virtuális gépek létrehozása során használja ezt a konfigurációt.
Set-AzVMOSDisk A felügyelt lemezt operációsrendszer-lemezként csatolja a virtuális géphez
New-AzPublicIpAddress Egy nyilvános IP-címet hoz létre.
New-AzNetworkInterface Hálózati adaptert hoz létre.
New-AzVM Létrehoz egy virtuális gépet.
Remove-AzResourceGroup Eltávolít egy erőforráscsoportot és az összes abban található erőforrást.

Következő lépések

Az Azure PowerShell modullal kapcsolatos további információért lásd az Azure PowerShell dokumentációját.

A virtuális gépekhez kapcsolódó további PowerShell-példaszkripteket az Azure Linux rendszerű virtuális gépekre vonatkozó dokumentációjában találhat.