Skip to content

Instantly share code, notes, and snippets.

@irwinwilliams
Created December 2, 2018 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irwinwilliams/cc639c2984b0b11a0cc11aaf28801e80 to your computer and use it in GitHub Desktop.
Save irwinwilliams/cc639c2984b0b11a0cc11aaf28801e80 to your computer and use it in GitHub Desktop.
An example of toggling VM state using Azure's Fluent SDK
public Tuple<string,string> SetState(string state)
{
var vmName = VirtualMachineName;
var client = AzureClientId;
var key = AzureClientKey;
var tenant = AzureTenantId;
var creds = new AzureCredentialsFactory()
.FromServicePrincipal(client, key, tenant, AzureEnvironment.AzureGlobalCloud);
var subscriptionId = AzureSubscriptionId;
var azure = Microsoft.Azure.Management.Fluent.Azure.Authenticate(creds).WithSubscription(subscriptionId);
var allVMs = azure.VirtualMachines.List();
var vmCurrent = (from vm in allVMs where vm.Name == vmName select vm).First();
switch (state)
{
case "on":
vmCurrent.Start();
break;
case "off":
vmCurrent.PowerOff();
break;
default:
break;
}
var message = new Tuple<string, string>(vmCurrent
.GetPrimaryNetworkInterface().PrimaryIPConfiguration.GetPublicIPAddress().IPAddress
,vmCurrent.PowerState.Value);
return message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment