1.脚本背景信息:
a.使用平台镜像(Windows Server 2016 zh-cn)部署高性能托管磁盘虚拟机
b.虚拟机默认不开启Boot诊断
c.添加三块已经创建好的数据磁盘
d.添加已创建好的网络接口(该网络接口已经绑定了NSG,固定公网IP,固定内网IP)
b.需要将虚拟机部署在制定的可用性集
$resourcegroup = "hlmrgt1" $location = "Chinanorth2" $vmsize="Standard_DS2_v2" $cred=Get-Credential $osdiskcache='ReadWrite' #/subscriptions/0b4b20c5-29b3-4dd9-bfdc-40b75f1dc4c9/resourceGroups/hlmrgt1/providers/Microsoft.Network/networkInterfaces/hlmnict1 $vmname='hlmvmt1' #$vnetname='hlmvnett1' #$publicip='hlmpublict1' $nicname='hlmnict1' $availabilitysetname='hlmast1' #$storageType='Standard_LRS' $storageType='Premium_LRS' #/subscriptions/0b4b20c5-29b3-4dd9-bfdc-40b75f1dc4c9/resourceGroups/hlmrgt1/providers/Microsoft.Compute/disks/hlmmadisk1 $dataDiskName1='hlmmadisk1' $dataDiskName2='hlmmadisk2' $dataDiskName3='hlmmadisk3' $OSDiskName='hlmmaosdisk' #$OSDiskUri='https://Mydisk.blob.core.windows.net/disks/MyOSDisk.vhd' $dataDisk1=Get-AzureRmDisk -DiskName $dataDiskName1 -ResourceGroupName $resourcegroup $dataDisk2=Get-AzureRmDisk -DiskName $dataDiskName2 -ResourceGroupName $resourcegroup $dataDisk3=Get-AzureRmDisk -DiskName $dataDiskName3 -ResourceGroupName $resourcegroup #$vnet=Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $resourcegroup #$pubip=Get-AzureRmPublicIpAddress -ResourceGroupName $resourcegroup -Name $publicip $nic=Get-AzureRmNetworkInterface -ResourceGroupName $resourcegroup -Name $nicname $availabilityset=Get-AzureRmAvailabilitySet -ResourceGroupName $resourcegroup -Name $availabilitysetname $vm=New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize -AvailabilitySetID $availabilityset.Id $vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2016-Datacenter-zhcn" -Version "latest" $vm=Set-AzureRmVMOSDisk -VM $vm -Name $OSDiskName -StorageAccountType $storageType -DiskSizeInGB 128 -CreateOption FromImage -Caching $osdiskcache #-VhdUri $OSDiskUri #$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmname -Credential $cred -ProvisionVMAgent $false -EnableAutoUpdate $false $vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmname -Credential $cred -ProvisionVMAgent -EnableAutoUpdate $vm=Set-AzureRmVMBootDiagnostics -VM $vm -Disable $vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id $vm=Add-AzureRmVMDataDisk -VM $vm -Name $dataDiskName1 -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 0 $vm=Add-AzureRmVMDataDisk -VM $vm -Name $dataDiskName2 -CreateOption Attach -ManagedDiskId $dataDisk2.Id -Lun 1 $vm=Add-AzureRmVMDataDisk -VM $vm -Name $dataDiskName3 -CreateOption Attach -ManagedDiskId $dataDisk3.Id -Lun 2 New-AzureRmVM -VM $vm -ResourceGroupName $resourcegroup -Location $location
2.经测试,上述脚本在Azure PS6.4/PS6.5/PS6.8上执行都没问题,但在PS6.7上执行时,会提示报错信息“Object reference not set to an instance of an object”,经排查在Azure Powershell 6.7的版本中,使用Powershell命令“Add-AzureRmVMDataDisk”添加数据磁盘时,需要指定“-StorageAccountType Premium_LRS”参数才可以成功执行
3.客户的进一步需求,如何使用Azure Powershell创建一块带有操作系统磁盘的托管磁盘,命令参考如下:
$OsDiskName = "hlmosdiskt1" $OsDiskImageReference = New-Object Microsoft.Azure.Management.Compute.Models.ImageDiskReference("/Subscriptions/6966eb56-434e-4874-a1d2-41892ca6938b/Providers/Microsoft.Compute/Locations/ChinaEast2/Publishers/MicrosoftWindowsServer/ArtifactTypes/VMImage/Offers/WindowsServer/Skus/2016-Datacenter-zhcn/Versions/2016.127.20180613") $OsDiskConfig = New-AzureRmDiskConfig -CreateOption FromImage -OsType Windows -DiskSizeGB 128 -AccountType Premium_LRS -Location "China East 2" -ImageReference $OsDiskImageReference $OsDisk = New-AzureRmDisk -ResourceGroupName liucheng -DiskName $OsDiskName -Disk $OsDiskConfig
4.如果需要将3中生产的disk创建为image,请参考如下命令:
$maOSdisk = Get-AzureRmDisk -ResourceGroupName hlmrgt1 -DiskName hlmosdiskt1 $imageConfig = New-AzureRmImageConfig -Location 'Chinanorth2' Set-AzureRmImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized -ManagedDiskId $maOSdisk.Id New-AzureRmImage -Image $imageConfig -ImageName hlmimaget1 -ResourceGroupName hlmrgt1
5.如果需要使用3中生成disk部署虚拟机,请参考,
I hear and I forget. I see and I remeber. I do and I understand!