Reputation: 3571
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();
}
}
Upvotes: 2
Views: 858
Reputation: 15850
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)
Upvotes: 2