Managing VirtualMachines in Azure with C#
Tags: azure, serverless
Published: 2019-09-10 07:50:13

---
title: Managing VirtualMachines in Azure with C#
published: true
tags: Azure,Serverless
canonical_url: https://peterrombouts.nl/2019/01/24/managing-virtualmachines-in-azure-with-c/
---

Managing VirtualMachines is often done using Azure Automation or PowerShell. But what if you want to do this using C#, in an Azure Function? This post shows a very simple example on how to implement this in a .Net Core 2 Azure Function.

##   
Setting up the project

To get started, simply create an Azure Function project in Visual Studio:

![createnewfunctionproject](https://peterrombouts.files.wordpress.com/2019/01/createnewfunctionproject.png?w=840)

## Setting up the function

For this example I’m going to create a Function with HTTP trigger that deallocates all VMs in a resource group.

To get started, create a new function called ‘DeallocateVMs’:

![deallocatevms](https://peterrombouts.files.wordpress.com/2019/01/deallocatevms.png?w=840)

I’ve chosen the ‘HTTP’ trigger because this is the easiest to run and debug on your local machine.

## Setting up the packages

First of all, install the next NuGet packages:

```
Install-Package Microsoft.Azure.Management.Fluent Install-Package Microsoft.Azure.Management.Compute.Fluent
```

This allows us to make use of the Azure libraries we need to manage VMs.

## Boilerplate code

The library needs to connect to Azure and have rights in the resource group you want to manage. For this, we need some boilerplate code to get the **AzureCredentials** :

```
internal static AzureCredentials GetCredentials() { var clientId = "Enter ClientId Here"; var clientSecret = "Enter ClientSecret Here"; var tenantId = "Enter TenantId Here"; return SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud); }
```

> For more info on creating Service Principals, please check out [this link at Microsoft](https://docs.microsoft.com/nl-nl/azure/active-directory/develop/howto-create-service-principal-portal)

After getting the credentials, the client can be configured:

```
var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription();
```

## Getting all VMs in a resource group

The IAzure interface allows us to list VMs, get information, and a lot more. Please check the recap of the blogpost for links to documentation.

In this example we want to deallocate all VMs in a specific resource group. I’ve skipped all checks and exception handling for sake of readability:

```
var resourcegroup = "your-resourcegroup-name-here"; foreach (var virtualMachine in await azure.VirtualMachines.ListByResourceGroupAsync(resourcegroup)) { log.LogInformation($"Deallocating VM {virtualMachine.Name}..."); await virtualMachine.DeallocateAsync(); log.LogInformation($"Deallocated VM {virtualMachine.Name}"); }
```

That is basically it! This function will now iterate thru all virtualmachines in the resource group and deallocate them

## Recap

This example does not take into account what happens when you have very large numbers of virtual machines. The code also does not take into account what the current PowerState of the machine is. In a more advanced scenario, you should check the PowerState and determine what to do. The easiest way is to check if the machine is actually running:

```
if (virtualMachine.PowerState == PowerState.Running) { // Do something }
```

I’ve used a service principal with clientid and secrets, and set the rights on the managed resource groups. This is a very basic setup to show how to manage the VMs. There are alot of ways to implement security, and maybe your scenario has client certificates or other authentication options. [This post on Github](https://github.com/Azure/azure-libraries-for-java/blob/master/AUTH.md) shows many scenarios on authenticating with this library.

## Links

For this example, I’ve used the namespaces [Microsoft.Azure.Management.Compute.Fluent](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.compute.fluent?view=azure-dotnet) and [Microsoft.Azure.Management.Fluent Namespace](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.fluent?view=azure-dotnet). There are a lot of [examples](https://github.com/Azure/azure-libraries-for-net#virtual-machines)on Github using the two [NuGet](https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent/) [packages](https://www.nuget.org/packages/Microsoft.Azure.Management.Compute.Fluent/).