2

The title says it all. I've tried lots of things but I don't think any of them are worth mentioning. I have finally figured out to avoid Microsoft.WindowsAzure and have installed the Microsoft.Azure.Management.Compute and Microsoft.Azure.Common libraries.

I've finally got an authentication token like this:

var authenticationContext = new AuthenticationContext("https://login.windows.net/deadbeef-beef-beef-beef-ec74557498e8");
var credential = new ClientCredential("beefbeef-beef-beef-beef-b1d3cf5d037d", "passwordpasswordpasswordpasswordpasswordpas=");
var result = authenticationContext.AcquireTokenAsync("https://www.url.com/servicename", credential);

But now I'm struggling to use the documentation to learn to power on my VMs. I'm not even sure exactly where to start. All I know is I'd like to avoid the REST API and keep my code in C#. I'm looking for something like:

using (var client = new ComputeManagementClient(creds)) {
    foreach (var vm in client.VMs)
    {
        Console.WriteLine("Starting VM: {0}", vm.Name);

        vm.PowerOn();
    }
}
David Makogon
  • 69,407
  • 21
  • 141
  • 189
sirdank
  • 3,351
  • 3
  • 25
  • 58

1 Answers1

2

Assuming that you're dealing with ARM-based VMs, here's the code to start one. "context" is ComputeManagementClient under the Microsoft.Azure.Management.Compute namespace

var result = VirtualMachinesOperationsExtensions.Start(context.VirtualMachines, azureResourceGroup, azureResourceName);

If you are dealing with with Classic VMs, here's the code to start one. "context" is ComputeManagementClient under the Microsoft.WindowsAzure.Management.Compute namespace

var result = context.VirtualMachines.BeginStarting(serviceName, deploymentName,
                        instanceName);

You can also avoid all of the headaches of writing your own code and monitoring it making sure it all works properly and use CloudMonix to schedule startups and shutdowns of your Azure VMs. (I'm affiliated with the service)

Igorek
  • 15,716
  • 3
  • 54
  • 92
  • Thank you so much! The `Microsoft.Azure` vs `Microsoft.WindowsAzure` thing has caused me no end of headaches. I appreciate the recommendation but I need to do this on-demand and in a manner integrated with some of our other internal systems. – sirdank May 25 '16 at 13:32
  • Also, d'oh! The whole reason I wanted to comment was to say I can't make a one character edit but I think `VirtualMachineOperationsExtensions` should be `VirtualMachinesOperationsExtensions`. It might save someone a minute or two in the future. – sirdank May 25 '16 at 13:47