gpt4 book ai didi

azure - 使用 Azure Powershell 脚本出现错误 "Changing property ' osDisk.name' is not allowed."

转载 作者:行者123 更新时间:2023-12-02 07:10:27 26 4
gpt4 key购买 nike

我正在尝试将带有托管磁盘的现有 Azure VM 移至现有可用性集中。但是,当我应用命令时:

New-AzureRmVM -ResourceGroupName $rg -Location $OriginalVM.Location -VM $NewVM -DisableBginfoExtension

我收到以下错误:

New-AzureRmVM : Changing property 'osDisk.name' is not allowed. ErrorCode: PropertyChangeNotAllowed ErrorMessage: Changing property 'osDisk.name' is not allowed. StatusCode: 409 ReasonPhrase: Conflict OperationID : c179070b-e189-4025-84b0-87ba748f5844 At line:2 char:5 + New-AzureRmVM -ResourceGroupName $rg -Location $OriginalVM.Locati ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [New-AzureRmVM], ComputeCloudException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand

最佳答案

在 Azure 中,一旦磁盘附加到 VM,就无法更改磁盘的名称。操作系统磁盘将获取您在创建 VM 期间提供的 VM 名称。您可以引用this link查找更多详细信息。

我做了一个测试并重现了与你相同的错误。因为我使用 Set-AzureRmVMOSDisk 更改了操作系统磁盘名称。然后我删除了更改操作系统磁盘名称的cmdlet并成功。

您可以引用以下 cmdlet 来创建虚拟机而不更改操作系统磁盘名称:

$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows 

我使用的整个 powershell cmdlet:

#Provide the subscription Id
$subscriptionId = 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'

$resourceGroupName ='yangsatest'

$diskName = 'VM1_OsDisk_1_xxxxxxxxxxxx'
$location = 'eastus'

$virtualNetworkName = 'yangsatest-vnet'
$virtualMachineName = 'VM2'

$virtualMachineSize = 'Standard_A1'
Select-AzureRmSubscription -SubscriptionId $SubscriptionId

$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $diskName

$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize -AvailabilitySetId /subscriptions/xxxxx-xxxxxx-xxx-xxxx8-xxxxxx/resourceGroups/yangsatest/providers/Microsoft.Compute/availabilitySets/Myset

#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-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows

$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $location -AllocationMethod Dynamic

$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName

$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id

$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id

#Create the virtual machine with Managed Disk
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $location

----------更新----------更新脚本以适应官方文档:更改托管 Windows VM 的可用性集( https://learn.microsoft.com/en-us/azure/virtual-machines/windows/change-availability-set ):

#set variables
$rg = "demo-resource-group"
$vmName = "demo-vm"
$newAvailSetName = "demo-as"
$outFile = "C:\temp\outfile.txt"

#Get VM Details
$OriginalVM = get-azurermvm -ResourceGroupName $rg -Name $vmName

#Output VM details to file
"VM Name: " | Out-File -FilePath $outFile
$OriginalVM.Name | Out-File -FilePath $outFile -Append

"Extensions: " | Out-File -FilePath $outFile -Append
$OriginalVM.Extensions | Out-File -FilePath $outFile -Append

"VMSize: " | Out-File -FilePath $outFile -Append
$OriginalVM.HardwareProfile.VmSize | Out-File -FilePath $outFile -Append

"NIC: " | Out-File -FilePath $outFile -Append
$OriginalVM.NetworkProfile.NetworkInterfaces.Id | Out-File -FilePath $outFile -Append

"OSType: " | Out-File -FilePath $outFile -Append
$OriginalVM.StorageProfile.OsDisk.OsType | Out-File -FilePath $outFile -Append

"OSDisk: " | Out-File -FilePath $outFile -Append
$OriginalVM.StorageProfile.OsDisk.ManagedDisk.Id| Out-File -FilePath $outFile -Append

if ($OriginalVM.StorageProfile.DataDisks) {
"Data Disk(s): " | Out-File -FilePath $outFile -Append
$OriginalVM.StorageProfile.DataDisks.Id | Out-File -FilePath $outFile -Append
}

#Remove the original VM
Remove-AzureRmVM -ResourceGroupName $rg -Name $vmName
#Create new availability set if it does not exist
$availSet = Get-AzureRmAvailabilitySet -ResourceGroupName $rg -Name $newAvailSetName -ErrorAction Ignore
if (-Not $availSet) {
$availset = New-AzureRmAvailabilitySet -ResourceGroupName $rg -Name $newAvailSetName -Location $OriginalVM.Location -Managed -PlatformFaultDomainCount 2 -PlatformUpdateDomainCount 2
}

#Create the basic configuration for the replacement VM
$newVM = New-AzureRmVMConfig -VMName $OriginalVM.Name -VMSize $OriginalVM.HardwareProfile.VmSize -AvailabilitySetId $availSet.Id
Set-AzureRmVMOSDisk -VM $NewVM -ManagedDisk $OriginalVM.StorageProfile.OsDisk.ManagedDisk.Id -CreateOption Attach -Windows

#Add Data Disks
foreach ($disk in $OriginalVM.StorageProfile.DataDisks ) {
Add-AzureRmVMDataDisk -VM $newVM -Name $disk.Name -ManagedDiskId $OriginalVM.StorageProfile.DataDisks.Id -Caching $disk.Caching -Lun $disk.Lun -CreateOption Attach -DiskSizeInGB $disk.DiskSizeGB
}

#Add NIC(s)
foreach ($nic in $OriginalVM.NetworkProfile.NetworkInterfaces.Id) {
Add-AzureRmVMNetworkInterface -VM $NewVM -Id $nic
}


#Create the VM
New-AzureRmVM -ResourceGroupName $rg -Location $OriginalVM.Location -VM $NewVM -DisableBginfoExtension

关于azure - 使用 Azure Powershell 脚本出现错误 "Changing property ' osDisk.name' is not allowed.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com