此腳本會將現有的受控磁碟連結為OS磁碟,以建立虛擬機。 在上述案例中使用此文稿:
- 從被複製自不同訂用帳戶的受控磁碟的現有受控OS磁碟建立VM
- 從特製化 VHD 檔案建立的現有受控磁碟創建 VM
- 從從快照集建立的現有受控 OS 磁碟建立 VM
如果您沒有 Azure 訂用帳戶,請在開始之前,先建立 Azure 免費帳戶。
範例腳本
#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
整理部署
執行下列命令來移除資源群組、VM 和所有相關資源。
Remove-AzResourceGroup -Name myResourceGroup
腳本說明
此腳本會使用下列命令來取得受控磁碟屬性、將受控磁碟連結至新的 VM 並建立 VM。 表格中的每個項目都會連結到與該命令相關的文件。
指令 | 備註 |
---|---|
Get-AzDisk | 根據磁碟的名稱和資源群組,取得磁碟物件。 傳回磁碟對象的標識碼屬性是用來將磁碟連結至新的 VM |
New-AzVMConfig | 建立 VM 組態。 此設定包含 VM 名稱、作系統和系統管理認證等資訊。 設定會在 VM 建立期間使用。 |
Set-AzVMOSDisk | 使用磁碟的Id屬性將受控磁碟作為作業系統磁碟附加至新的虛擬機器 |
New-AzPublicIpAddress | 建立公用IP位址。 |
New-AzNetworkInterface | 建立網路介面。 |
New-AzVM | 建立虛擬機。 |
Remove-AzResourceGroup | 移除資源群組及其內含的所有資源。 |
針對 Marketplace 映射,請使用 Set-AzVMPlan 來設定方案資訊。
Set-AzVMPlan -VM $VirtualMachine -Publisher $Publisher -Product $Product -Name $Bame
後續步驟
如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件。
如需其他虛擬機 PowerShell 腳本範例,請參閱 Azure Windows VM 檔案。