Created
December 2, 2018 22:44
-
-
Save irwinwilliams/cc639c2984b0b11a0cc11aaf28801e80 to your computer and use it in GitHub Desktop.
An example of toggling VM state using Azure's Fluent SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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