-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cs
28 lines (24 loc) · 998 Bytes
/
code.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
//Get all VMs
var vms = azure.VirtualMachines.List();
//Loop through VMs
foreach(IVirtualMachine vm in vms){
log.LogInformation("VM: " + vm.Name.ToString() + " - " + vm.PowerState.ToString());
//Check if VM is running, if running check tags
if (vm.PowerState == PowerState.Running){
var poweroff = true;
//Loop through Tags when not none
foreach(KeyValuePair<string, string> tag in vm.Tags){
//If not tag - powerstate = alwayson -> deallocate VM
if(tag.Key.Equals("powerstate") && (tag.Value.Equals("alwayson"))){
log.LogInformation("VM: " + vm.Name.ToString() + " is set to 'always on'");
poweroff = false;
break; //No use to loop through other tags.
}
}
//Check what to do.
if (poweroff){
log.LogInformation("Deallocating VM: " + vm.Name.ToString());
vm.DeallocateAsync(); //Don't wait.
}
}
}