0

I am setting up an Azure VM using a .Net Core exe (turning on MSDTC, making sure all the features are installed, etc). The VM will have attached drives that have not been initialized yet. In Powershell I can simply use Initialize-Disk -PartitionStyle MBR and then New-Partition -UseMaximumSize -DriveLetter 'D' to create the partition.

How do I initialize the disk and create a partition in C# Core?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Tom Padilla
  • 924
  • 10
  • 30

1 Answers1

0

You need to use the below namespaces.

Microsoft.Azure.Management.ResourceManager.Fluent Microsoft.Azure.Management.Compute.Fluent.Models

        var Auth_credentials = SdkContext.AzureCredentialsFactory.FromFile(@"C:\Tools\azureauth.properties");

        var az = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(Auth_credentials)
            .WithDefaultSubscription();

        var virtualMachine = az.VirtualMachines.GetByResourceGroup("resourceGroupName", "vmName");
        Console.WriteLine(virtualMachine.Name);

        DataDisk disc = new DataDisk(virtualMachine.StorageProfile.DataDisks.Count + 1, DiskCreateOptionTypes.Empty, null, 15, null);
        disc.Validate();
        virtualMachine.StorageProfile.DataDisks.Add(disc);
        virtualMachine.StorageProfile.Validate();
        virtualMachine.Update().Apply();

enter image description here

For further information, refer to the SO link1 and SO link2.

Sourav
  • 814
  • 1
  • 9
  • That will add a disk to an Azure VM. I want to initialize the disk. Once I add the disk it is now an uninitialized disk on the VM. I'm running code to setup the VM and I need to initialize the disk after adding it to the machine. – Tom Padilla Apr 04 '23 at 15:32