skip to Main Content

I’m writing a utility to move vms between 2 azure tenants.
I’m developping it in C# as part of a larger project.
Microsoft provides instructions in powershell:
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/disks-upload-vhd-to-managed-disk-powershell

It seams pretty straightforward but I can’t find how to get the SAS urls of the source and destination in c#.

$sourceDiskSas = Grant-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName -DurationInSecond 86400 -Access 'Read'

$targetDiskSas = Grant-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName -DurationInSecond 86400 -Access 'Write'

What rest apis or nuget packages and classes should I use to achieve the same in c#?
I searched for quite a long time and didn’t find a documented solution on the web?

2

Answers


  1. The below is the NuGet used for Azure SDK for .NET.

    Microsoft.Azure.Management.Compute
    

    enter image description here

    Using C# code

    using (PowerShell ps = PowerShell.Create())
        {
            ps.AddCommand("Connect-AzAccount");
            ps.AddParameter("SubscriptionId", subId);
            ps.Invoke();
            ps.Commands.Clear();
            ps.AddCommand("Grant-AzDiskAccess");
            ps.AddParameter("ResourceGroupName", rgName);
            ps.AddParameter("DiskName", disk);
            ps.AddParameter("VMName", vm);
            ps.AddParameter("Access", "ReadWrite");
            ps.AddParameter("DurationInSeconds", 3600);
            
            ps.Invoke();
        }
    

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search