How to turn on/off Azure virtual machine via Azure management api (rest) How to turn on/off Azure virtual machine via Azure management api (rest) azure azure

How to turn on/off Azure virtual machine via Azure management api (rest)


calls azure management API to start deallocated virtual machine

Virtual Machines REST API lists the operations on virtual machines. To start a virtual machine, you can try this API:

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{vm}/start?api-version={apiVersion}

where I can get values for API method parameters (e.g. subscription id, resource id, etc).

You can find {subscriptionId} and { resourceGroup} on Azure portal.

enter image description here

how to deal with authorization

You can check this article to get started with Azure REST operations and request authentication. And you can refer to the following code to acquire an access token.

string tenantId = "{tenantId}";string clientId = "{clientId}";string clientSecret = "{secret}";string subscriptionid = "{subscriptionid}";string authContextURL = "https://login.windows.net/" + tenantId;var authenticationContext = new AuthenticationContext(authContextURL);var credential = new ClientCredential(clientId, clientSecret);var result = await authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential);if (result == null){    throw new InvalidOperationException("Failed to obtain the JWT token");}string token = result.AccessToken;

Besides, this article explained how to create AD application and service principal that can access resources, please refer to it.