possibly a LUN issue. Try adding a condition like below:
# Find the next available LUN
$lun = 0
while ($vm.DataDisks.Lun -contains $lun) {
$lun++ }
You can mark it 'Accept Answer' and 'Upvote' if this helped you
Regards,
Abiola
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello Everyone.
I am trying to add one OS disk as data disk into my vm using powershell. I am able to update the vm. after this it is not showing in portal.
are we need to add manually. I want to reduce this efforts as well. I really appreciate for quick reply.
8------------------------------------------------------------------------------------------
$rgName = "myResourceGroup"
$vmName = "RecoveryVM"
$location = "eastus"
$dataDiskName = "newOSDisk"
$disk = Get-AzDisk -ResourceGroupName $rgName -DiskName $dataDiskName
$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName
$vm = Add-AzVMDataDisk -CreateOption Attach -Lun 0 -VM $vm -ManagedDiskId $disk.Id
Update-AzVM -VM $vm -ResourceGroupName $rgName
possibly a LUN issue. Try adding a condition like below:
# Find the next available LUN
$lun = 0
while ($vm.DataDisks.Lun -contains $lun) {
$lun++ }
You can mark it 'Accept Answer' and 'Upvote' if this helped you
Regards,
Abiola
Hello Mohsin Khan,
Greetings! Welcome to Microsoft Q&A Platform.
$vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
$vm.StorageProfile.DataDisks
This command will list all data disks attached to the VM. Ensure your new disk is listed.
$vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
$vm.StorageProfile.DataDisks | ForEach-Object {
if ($_.Lun -eq 0) {
$_.CreateOption = "Attach"
$_.ManagedDisk = $disk
}
}
Update-AzVM -ResourceGroupName $rgName -VM $vm
If the disk still doesn’t show up, you might need to manually add it through the Azure portal. Go to your VM’s settings, select “Disks,” and then add the data disk from there. refer - https://learn.microsoft.com/en-us/azure/virtual-machines/windows/tutorial-manage-data-disk, https://learn.microsoft.com/en-us/azure/virtual-machines/windows/os-disk-swap.
Hope this information helps! Please let us know if you have any further queries. I’m happy to assist you further.
Please "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.