使用現有受控 OS 磁碟搭配 PowerShell 以建立虛擬機器

此指令碼會連結現有受控磁碟作為 OS 磁碟,以建立虛擬機器。 在先前案例中使用此指令碼︰

  • 從不同訂用帳戶中的受控磁碟複製而來的現有受控 OS 磁碟建立 VM
  • 從特殊化 VHD 檔案建立的現有受控磁碟建立 VM
  • 從快照集建立的現有受控 OS 磁碟建立 VM

如果您沒有 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。 下表中的每個項目都會連結至命令特定的文件。

Command 注意
Get-AzDisk 根據磁碟的名稱和資源群組取得磁碟物件。 傳回物件的 Id 屬性用來將磁碟連結至新的 VM
New-AzVMConfig 建立 VM 組態。 此組態包括 VM 名稱、作業系統和系統管理認證等資訊。 建立 VM 時會使用此組態。
Set-AzVMOSDisk 使用磁碟的 Id 屬性將受控磁碟當作 OS 磁碟連結至新的虛擬機器
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 文件

您可以在 Azure Windows VM 文件中找到其他的虛擬機器 PowerShell 指令碼範例。