unable to create disk

Glenn Maxwell 10,781 Reputation points
2023-04-24T12:05:29.69+00:00

Hi All i am trying to create a disk using the below article. i need to add tags as we have a tagging policy. i have modified the script to add tags but it is not working please guide me. https://learn.microsoft.com/en-us/azure/virtual-machines/windows-in-place-upgrade

$resourceGroup = "WindowsServerUpgrades"
$location = "WestUS2"
$zone = "" 
$diskName = "WindowsServer2022UpgradeDisk"
$sku = "server2022Upgrade"
$publisher = "MicrosoftWindowsServer"
$offer = "WindowsServerUpgrade"
$managedDiskSKU = "Standard_LRS"
$Tags = 
@{"Application"="Test";`
"Owner"="User1";`
"Environment"="Development";}
$versions = Get-AzVMImage -PublisherName $publisher -Location $location -Offer $offer -Skus $sku | sort-object -Descending {[version] $_.Version	}
$latestString = $versions[0].Version
$image = Get-AzVMImage -Location $location `
                       -PublisherName $publisher `
                       -Offer $offer `
                       -Skus $sku `
                       -Version $latestString
if (-not (Get-AzResourceGroup -Name $resourceGroup -ErrorAction SilentlyContinue)) {
    New-AzResourceGroup -Name $resourceGroup -Location $location    
}
if ($zone){
    $diskConfig = New-AzDiskConfig -SkuName $managedDiskSKU `
                                   -CreateOption FromImage `
                                   -Zone $zone `
                                   -Location $location
} else {
    $diskConfig = New-AzDiskConfig -SkuName $managedDiskSKU `
                                   -CreateOption FromImage `
                                   -Location $location
} 
Set-AzDiskImageReference -Disk $diskConfig -Id $image.Id -Lun 0
New-AzDisk -ResourceGroupName $resourceGroup `
           -DiskName $diskName `
           -Disk $diskConfig
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,585 questions
{count} votes

Accepted answer
  1. Prrudram-MSFT 23,211 Reputation points
    2023-04-26T14:12:42.9633333+00:00

    Hi Glenn Maxwell, It seems like there is an issue with the data type of the $image.Id parameter. The Set-AzDiskImageReference cmdlet requires a string data type for the -Id parameter, but it seems like $image.Id is an object array.

    You can try converting the $image.Id object array to a string using the -join operator. Here's an example:

    Set-AzDiskImageReference -Disk $diskConfig -Id ($image.Id -join "") -Lun 0 -Verbose
    

    This should convert the $image.Id object array to a string and pass it to the -Id parameter of the Set-AzDiskImageReference cmdlet.It seems like there is an issue with the data type of the $image.Id parameter. The Set-AzDiskImageReference cmdlet requires a string data type for the -Id parameter, but it seems like $image.Id is an object array. You can try converting the $image.Id object array to a string using the -join operator. Here's an example:

    Set-AzDiskImageReference -Disk $diskConfig -Id ($image.Id -join "") -Lun 0 -Verbose
    

    This should convert the $image.Id object array to a string and pass it to the -Id parameter of the Set-AzDiskImageReference cmdlet. Tag me in your comments for further discussion.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. shiva patpi 13,171 Reputation points Microsoft Employee
    2023-04-24T20:52:12.98+00:00

    @Glenn Maxwell You were missing below 3 lines of the code and also you declared the variable $Tags and initialized but you are not using that $Tags variable.

    #use -Tag flag in New-AzDiskConfig command

    $diskConfig = New-AzDiskConfig -SkuName $managedDiskSKU -CreateOption FromImage -Zone $zone -Location $location -Tag $Tags #Get the existing VM by name

    $vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $VMName

    #Add the created disk to the VM

    $vm = Add-AzVMDataDisk -VM $vm -Name $diskName -CreateOption Attach -ManagedDiskId $disk.Id -Lun 1

    #Update the disk

    Update-AzVM -ResourceGroupName $resourceGroup -VM $vm

    ///////// Regards, Shiva