-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopVMs.cs
29 lines (25 loc) · 902 Bytes
/
StopVMs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System.Threading.Tasks;
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace AzureScheduler
{
public static class StopVMs
{
[FunctionName("StopVMs")]
public static async Task Run([TimerTrigger("0 0 22 * * *")]TimerInfo myTimer, ILogger log)
{
var azure = Configuration.GetAzureApi();
var vms = await azure.VirtualMachines.ListAsync();
await ResourceHelper.ProcessResources(vms,
vm => vm.PowerState == PowerState.Running,
vm => Stop(vm, log),
log);
}
public static Task Stop(IVirtualMachine vm, ILogger log)
{
log.LogInformation("Stopping {name} (current state: {state})", vm.Name, vm.PowerState.ToString());
return vm.DeallocateAsync();
}
}
}