我正在通过PowerShell使用自定义映像创建Azure VM。自定义映像具有操作系统磁盘和数据磁盘配置。但在执行代码时,VM成功部署,但操作系统磁盘和数据磁盘配置不同。
如何编写PowerShell代码来创建操作系统磁盘和数据磁盘沿着自定义映像?
下面是代码:
#Define the required Parameters
$resourceGroup = "RG-Test2"
$location = "West US3"
$subnetConfig = "mySubnet"
$vnet = "MYvNET"
$vmName = "MyTestVM"
$galleryname = "mygallery"
$ImageDefinitionName = "windowsimage1"
# Install the AZ modules and Login to Azure
Install-Module -Name Az -AllowClobber -Scope CurrentUser
Connect-AzAccount
#Get the Image details present in resource group
$imageDefinition = Get-AzGalleryImageDefinition `
-GalleryName $galleryname `
-ResourceGroupName $resourceGroup `
-Name $ImageDefinitionName
# Create user credential object which would be used for Virtual machine
do {
$cred = Get-Credential -Message "Enter a username and password for VM."
$password = $cred.GetNetworkCredential().Password
if ($password.Length -lt 8) {
Write-Host "Password must be at least 8 characters long. Please try again."
}
else {
# Password meets the minimum length requirement.
# You can proceed with the credential ($cred) as needed.
break
}
} while ($true)
# Create an inbound network security group rule for port 3389
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleSSH `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 `
-Access Allow
# Create a network security group
$nsg = New-AzNetworkSecurityGroup `
-ResourceGroupName $resourceGroup `
-Location $location `
-Name myNetworkSecurityGroup `
-SecurityRules $nsgRuleSSH
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzNetworkInterface `
-Name myNic `
-ResourceGroupName $resourceGroup `
-Location $location `
-SubnetId $vnet.Subnets[0].Id `
-NetworkSecurityGroupId $nsg.Id
# Create a virtual machine configuration
$vmConfig = New-AzVMConfig `
-VMName $vmName `
-VMSize Standard_D2s_v3 | `
Set-AzVMSecurityProfile -SecurityType "TrustedLaunch" | `
Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
Set-AzVMSourceImage -Id $imageDefinition.Id | `
Add-AzVMNetworkInterface -Id $nic.Id
# Create a virtual machine
$vm = New-AzVM `
-ResourceGroupName $resourceGroup `
-Location $location `
-VM $vmConfig
# Check if the VM creation was successful
if ($vm) {
Write-Host "VM '$($vmName)' has been created successfully."
} else {
Write-Host "Failed to create the VM."
}
字符串
1条答案
按热度按时间92vpleto1#
如何编写PowerShell代码来创建操作系统磁盘和数据磁盘沿着自定义映像?
下面是更新的PowerShell脚本,用于从图库图像创建
OS
磁盘和Data
磁盘。字符串
执行PowerShell代码后,将从图库图像中创建大小相同的
OS
和data disks
。的数据
下面是我的图库图像的
OS
和Data
磁盘。的