Skrip sampel untuk mengunggah VHD ke Azure dan membuat VM baru
Skrip ini mengambil file .vhd lokal dari VM umum dan mengunggahnya ke Azure, membuat gambar Disk Terkelola dan menggunakan untuk membuat VM baru.
Sampel ini memerlukan Azure PowerShell Az 1.0 atau yang lebih baru. Jalankan Get-Module -ListAvailable Az
untuk melihat versi mana yang dipasang.
Jika Anda perlu memasang, lihat Pasang modul Azure PowerShell.
Jalankan Connect-AzAccount untuk masuk ke Azure.
Jika Anda tidak memiliki langganan Azure, buatlah akun gratis sebelum Anda memulai.
Contoh skrip
# Provide values for the variables
$resourceGroup = 'myResourceGroup'
$location = 'EastUS'
$storageaccount = 'mystorageaccount'
$storageType = 'Standard_LRS'
$containername = 'mycontainer'
$localPath = 'C:\Users\Public\Documents\Hyper-V\VHDs\generalized.vhd'
$vmName = 'myVM'
$imageName = 'myImage'
$vhdName = 'myUploadedVhd.vhd'
$diskSizeGB = '128'
$subnetName = 'mySubnet'
$vnetName = 'myVnet'
$ipName = 'myPip'
$nicName = 'myNic'
$nsgName = 'myNsg'
$ruleName = 'myRdpRule'
$computerName = 'myComputerName'
$vmSize = 'Standard_DS1_v2'
# Get the username and password to be used for the administrators account on the VM.
# This is used when connecting to the VM using RDP.
$cred = Get-Credential
# Upload the VHD
New-AzResourceGroup -Name $resourceGroup -Location $location
New-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount -Location $location `
-SkuName $storageType -Kind "Storage"
$urlOfUploadedImageVhd = ('https://' + $storageaccount + '.blob.core.windows.net/' + $containername + '/' + $vhdName)
Add-AzVhd -ResourceGroupName $resourceGroup -Destination $urlOfUploadedImageVhd `
-LocalFilePath $localPath
# Note: Uploading the VHD may take awhile!
# Create a managed image from the uploaded VHD
$imageConfig = New-AzImageConfig -Location $location
$imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized `
-BlobUri $urlOfUploadedImageVhd
$image = New-AzImage -ImageName $imageName -ResourceGroupName $resourceGroup -Image $imageConfig
# Create the networking resources
$singleSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location `
-AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet
$pip = New-AzPublicIpAddress -Name $ipName -ResourceGroupName $resourceGroup -Location $location `
-AllocationMethod Dynamic
$rdpRule = New-AzNetworkSecurityRuleConfig -Name $ruleName -Description 'Allow RDP' -Access Allow `
-Protocol Tcp -Direction Inbound -Priority 110 -SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 3389
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name $nsgName -SecurityRules $rdpRule
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
$vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroup -Name $vnetName
# Start building the VM configuration
$vm = New-AzVMConfig -VMName $vmName -VMSize $vmSize
# Set the VM image as source image for the new VM
$vm = Set-AzVMSourceImage -VM $vm -Id $image.Id
# Finish the VM configuration and add the NIC.
$vm = Set-AzVMOSDisk -VM $vm -DiskSizeInGB $diskSizeGB -CreateOption FromImage -Caching ReadWrite
$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $computerName -Credential $cred `
-ProvisionVMAgent -EnableAutoUpdate
$vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
# Create the VM
New-AzVM -VM $vm -ResourceGroupName $resourceGroup -Location $location
# Verify that the VM was created
$vmList = Get-AzVM -ResourceGroupName $resourceGroup
$vmList.Name
Membersihkan penyebaran
Jalankan perintah berikut untuk menghapus grup sumber daya, VM, dan semua sumber daya terkait.
Remove-AzResourceGroup -Name $resourceGroup
Penjelasan skrip
Skrip ini menggunakan perintah berikut untuk membuat penyebaran. Setiap item dalam tabel menautkan dokumentasi spesifik perintah.
Perintah | Catatan |
---|---|
Baru-AzResourceGroup | Membuat grup sumber daya tempat semua sumber daya disimpan. |
New-AzStorageAccount | Membuat akun penyimpanan. |
Add-AzVhd | Mengunggah hard disk virtual dari mesin virtual lokal ke blob di akun penyimpanan cloud di Azure. |
New-AzImageConfig | Membuat objek gambar yang dapat dikonfigurasi. |
Set-AzImageOsDisk | Mengatur properti disk sistem operasi pada objek gambar. |
New-AzImage | Membuat gambar baru. |
New-AzVirtualNetworkSubnetConfig | Membuat konfigurasi subnet. Konfigurasi ini digunakan dengan proses pembuatan jaringan virtual. |
New-AzVirtualNetwork | Membuat jaringan virtual. |
Untuk informasi selengkapnya tentang Azure PowerShell, lihat Dokumentasi Microsoft Azure PowerShell | Membuat alamat IP publik. |
New-AzNetworkInterface | Membuat antarmuka jaringan. |
New-AzNetworkSecurityRuleConfig | Membuat konfigurasi aturan grup keamanan jaringan. Konfigurasi ini digunakan untuk membuat aturan NSG saat NSG dibuat. |
New-AzNetworkSecurityGroup | Membuat grup keamanan jaringan. |
Pulihkan-AzSqlDatabase | Mendapatkan jaringan virtual dalam grup sumber daya. |
New-AzVMConfig | Membuat konfigurasi komputer virtual. Konfigurasi ini mencakup informasi seperti nama komputer virtual, sistem operasi, dan kredensial administratif. Konfigurasi digunakan selama pembuatan komputer virtual. |
Set-AzVMSourceImage | Menentukan gambar untuk mesin virtual. |
Set-AzVMOSDisk | Mengatur properti disk sistem operasi pada mesin virtual. |
Set-AzVMOperatingSystem | Mengatur properti disk sistem operasi pada mesin virtual. |
Add-AzVMNetworkInterface | Menambahkan antarmuka jaringan ke mesin virtual. |
New-AzVM | Membuat komputer virtual. |
Remove-AzResourceGroup | Menghapus grup sumber daya dan semua sumber daya yang ada di dalamnya. |
Langkah berikutnya
Untuk informasi selengkapnya tentang modul Azure PowerShell, lihat dokumentasi Azure PowerShell.
Sampel skrip PowerShell mesin virtual tambahan dapat ditemukan di dokumentasi VM Windows Azure.