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