使用PowerShell创建具有自定义映像的Azure VM时出错

mbjcgjjk  于 2023-11-21  发布在  Shell
关注(0)|答案(1)|浏览(223)

我正在通过PowerShell使用自定义映像创建Azure VM。自定义映像具有操作系统磁盘和数据磁盘配置。但在执行代码时,VM成功部署,但操作系统磁盘和数据磁盘配置不同。
如何编写PowerShell代码来创建操作系统磁盘和数据磁盘沿着自定义映像?
下面是代码:

  1. #Define the required Parameters
  2. $resourceGroup = "RG-Test2"
  3. $location = "West US3"
  4. $subnetConfig = "mySubnet"
  5. $vnet = "MYvNET"
  6. $vmName = "MyTestVM"
  7. $galleryname = "mygallery"
  8. $ImageDefinitionName = "windowsimage1"
  9. # Install the AZ modules and Login to Azure
  10. Install-Module -Name Az -AllowClobber -Scope CurrentUser
  11. Connect-AzAccount
  12. #Get the Image details present in resource group
  13. $imageDefinition = Get-AzGalleryImageDefinition `
  14. -GalleryName $galleryname `
  15. -ResourceGroupName $resourceGroup `
  16. -Name $ImageDefinitionName
  17. # Create user credential object which would be used for Virtual machine
  18. do {
  19. $cred = Get-Credential -Message "Enter a username and password for VM."
  20. $password = $cred.GetNetworkCredential().Password
  21. if ($password.Length -lt 8) {
  22. Write-Host "Password must be at least 8 characters long. Please try again."
  23. }
  24. else {
  25. # Password meets the minimum length requirement.
  26. # You can proceed with the credential ($cred) as needed.
  27. break
  28. }
  29. } while ($true)
  30. # Create an inbound network security group rule for port 3389
  31. $nsgRuleSSH = New-AzNetworkSecurityRuleConfig `
  32. -Name myNetworkSecurityGroupRuleSSH `
  33. -Protocol Tcp `
  34. -Direction Inbound `
  35. -Priority 1000 `
  36. -SourceAddressPrefix * `
  37. -SourcePortRange * `
  38. -DestinationAddressPrefix * `
  39. -DestinationPortRange 3389 `
  40. -Access Allow
  41. # Create a network security group
  42. $nsg = New-AzNetworkSecurityGroup `
  43. -ResourceGroupName $resourceGroup `
  44. -Location $location `
  45. -Name myNetworkSecurityGroup `
  46. -SecurityRules $nsgRuleSSH
  47. # Create a virtual network card and associate with public IP address and NSG
  48. $nic = New-AzNetworkInterface `
  49. -Name myNic `
  50. -ResourceGroupName $resourceGroup `
  51. -Location $location `
  52. -SubnetId $vnet.Subnets[0].Id `
  53. -NetworkSecurityGroupId $nsg.Id
  54. # Create a virtual machine configuration
  55. $vmConfig = New-AzVMConfig `
  56. -VMName $vmName `
  57. -VMSize Standard_D2s_v3 | `
  58. Set-AzVMSecurityProfile -SecurityType "TrustedLaunch" | `
  59. Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
  60. Set-AzVMSourceImage -Id $imageDefinition.Id | `
  61. Add-AzVMNetworkInterface -Id $nic.Id
  62. # Create a virtual machine
  63. $vm = New-AzVM `
  64. -ResourceGroupName $resourceGroup `
  65. -Location $location `
  66. -VM $vmConfig
  67. # Check if the VM creation was successful
  68. if ($vm) {
  69. Write-Host "VM '$($vmName)' has been created successfully."
  70. } else {
  71. Write-Host "Failed to create the VM."
  72. }

字符串

92vpleto

92vpleto1#

如何编写PowerShell代码来创建操作系统磁盘和数据磁盘沿着自定义映像?
下面是更新的PowerShell脚本,用于从图库图像创建OS磁盘和Data磁盘。

  1. #Create some variables for the new VM.
  2. $resourceGroup = "xxxxxxx"
  3. $location = "West US 2"
  4. $subnetConfig = "vmsubnet2"
  5. $vnet = "venkatvnet3"
  6. $vmName = "TestVM3"
  7. $galleryname = "samplegallery"
  8. $ImageDefinitionName = "venkat-linux"
  9. $imageDefinition = Get-AzGalleryImageDefinition `
  10. -GalleryName $galleryname `
  11. -ResourceGroupName $resourceGroup `
  12. -Name $ImageDefinitionName
  13. # Create user credential object which would be used for Virtual machine
  14. do {
  15. $cred = Get-Credential -Message "Enter a username and password for VM."
  16. $password = $cred.GetNetworkCredential().Password
  17. if ($password.Length -lt 8) {
  18. Write-Host "Password must be at least 8 characters long. Please try again."
  19. }
  20. else {
  21. break
  22. }
  23. } while ($true)
  24. $subnetConfig = New-AzVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
  25. $vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
  26. -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
  27. $pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
  28. -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
  29. $nsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
  30. -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
  31. -DestinationPortRange 3389 -Access Allow
  32. $nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
  33. -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
  34. $nic = New-AzNetworkInterface -Name $vmName -ResourceGroupName $resourceGroup -Location $location `
  35. -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
  36. $vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_D2s_v3 | `
  37. Set-AzVMSecurityProfile -SecurityType "Standard" | `
  38. Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
  39. Set-AzVMSourceImage -Id $imageDefinition.Id | `
  40. Add-AzVMNetworkInterface -Id $nic.Id
  41. #Create a virtual machine
  42. New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

字符串
执行PowerShell代码后,将从图库图像中创建大小相同的OSdata disks


的数据
下面是我的图库图像的OSData磁盘。


展开查看全部

相关问题