PowerShell でスナップショットから仮想マシンを作成するCreate a virtual machine from a snapshot with PowerShell
このスクリプトは、OS ディスクのスナップショットから仮想マシンを作成します。This script creates a virtual machine from a snapshot of an OS disk.
Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。If you don't have an Azure subscription, create a free account before you begin.
サンプル スクリプトSample script
#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
デプロイのクリーンアップClean up deployment
次のコマンドを実行して、リソース グループ、VM、すべての関連リソースを削除します。Run the following command to remove the resource group, VM, and all related resources.
Remove-AzResourceGroup -Name myResourceGroup
スクリプトの説明Script explanation
このスクリプトは、次のコマンドを使用して、スナップショットのプロパティを取得し、スナップショットからマネージド ディスクを作成し、VM を作成します。This script uses the following commands to get snapshot properties, create a managed disk from snapshot and create a VM. 表内の各項目は、コマンドごとのドキュメントにリンクされています。Each item in the table links to command specific documentation.
commandCommand | メモNotes |
---|---|
Get-AzSnapshotGet-AzSnapshot | スナップショット名を使用してスナップショットを取得します。Gets a snapshot using snapshot name. |
New-AzDiskConfigNew-AzDiskConfig | ディスク構成を作成します。Creates a disk configuration. この構成は、ディスク作成プロセスで使用されます。This configuration is used with the disk creation process. |
New-AzDiskNew-AzDisk | マネージド ディスクを作成します。Creates a managed disk. |
New-AzVMConfigNew-AzVMConfig | VM 構成を作成します。Creates a VM configuration. この構成には、VM 名、オペレーティング システム、管理資格情報などの情報が含まれます。This configuration includes information such as VM name, operating system, and administrative credentials. この構成は、VM の作成時に使用されます。The configuration is used during VM creation. |
Set-AzVMOSDiskSet-AzVMOSDisk | マネージド ディスクを OS ディスクとして仮想マシンに接続します。Attaches the managed disk as OS disk to the virtual machine |
New-AzPublicIpAddressNew-AzPublicIpAddress | パブリック IP アドレスを作成します。Creates a public IP address. |
New-AzNetworkInterfaceNew-AzNetworkInterface | ネットワーク インターフェイスを作成します。Creates a network interface. |
New-AzVMNew-AzVM | 仮想マシンを作成します。Creates a virtual machine. |
Remove-AzResourceGroupRemove-AzResourceGroup | リソース グループと、それに含まれているすべてのリソースを削除します。Removes a resource group and all resources contained within. |
次の手順Next steps
Azure PowerShell モジュールの詳細については、Azure PowerShell のドキュメントを参照してください。For more information on the Azure PowerShell module, see Azure PowerShell documentation.
その他の仮想マシン用の PowerShell サンプル スクリプトは、Azure Windows VM のドキュメントにあります。Additional virtual machine PowerShell script samples can be found in the Azure Windows VM documentation.
フィードバック
フィードバックを読み込んでいます...