this is my code to create vm in azure using azure sdk fluent my question is i need to set auto shutdown option while creating vm i need to set the time while creating the vm so in that case the vm will get poweroff when the time arrives is that possible pls let me know if possible ??
public async Task<VmResponse> DoCreateVm(VMConfigurationParameter objvm)
{
VmResponse response = new VmResponse();
LogDAL objlog = new LogDAL();
var usernameWithoutSpaces = objvm.UserName.Replace(" ", "");
var vNetName = $"{char.ToUpper(usernameWithoutSpaces.Replace(".", "")[0])}{usernameWithoutSpaces.Replace(".", "").Substring(1, Math.Min(5, usernameWithoutSpaces.Replace(".", "").Length - 1))}{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}-vnet";
var vNetAddress = "172.16.0.0/16";
var subnetName = "default";
var subnetAddress = "172.16.0.0/24";
var nicName = $"{char.ToUpper(objvm.UserName.Replace(" ", "").Replace(".", "")[0])}{objvm.UserName.Replace(" ", "").Replace(".", "").Substring(1, Math.Min(5, objvm.UserName.Replace(" ", "").Length - 1))}{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}";
var publicIPName = $"{char.ToUpper(objvm.UserName.Replace(" ", "").Replace(".", "")[0])}{objvm.UserName.Replace(" ", "").Replace(".", "").Substring(1, Math.Min(5, objvm.UserName.Replace(" ", "").Length - 1))}{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}";
var nsgName = $"{char.ToUpper(objvm.UserName.Replace(" ", "").Replace(".", "")[0])}{objvm.UserName.Replace(" ", "").Replace(".", "").Substring(1, Math.Min(5, objvm.UserName.Replace(" ", "").Length - 1))}{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}";
var username = $"{char.ToUpper(objvm.UserName.Replace(" ", "").Replace(".", "")[0])}{objvm.UserName.Replace(" ", "").Replace(".", "").Substring(1, Math.Min(5, objvm.UserName.Replace(" ", "").Length - 1))}{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}";
var userpassword = $"{char.ToUpper(objvm.UserName.Replace(" ", "").Replace(".", "")[0])}{objvm.UserName.Replace(" ", "").Replace(".", "").Substring(1, Math.Min(5, objvm.UserName.Replace(" ", "").Length - 1))}@{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}";
var vmname = $"{char.ToUpper(objvm.UserName.Replace(" ", "").Replace(".", "")[0])}{objvm.UserName.Replace(" ", "").Replace(".", "").Substring(1, Math.Min(5, objvm.UserName.Replace(" ", "").Replace(".", "").Length - 1))}{objvm.UserNumber.ToString().Substring(Math.Max(0, objvm.UserNumber.ToString().Length - 6))}-VM";
try
{
string filePath = ConfigurationManager.AppSettings["Credentials"];
var credentials = SdkContext.AzureCredentialsFactory.FromFile(filePath);
filePath = string.Empty;
var azure = Azure.Authenticate(credentials).WithDefaultSubscription();
string vm_ResGroup = "SSIS_Student";
var resourceGroupName = await DoGetResourceGroupName(azure, vm_ResGroup);
var details_resgrp = await DoGetResourceGroupName(azure, objvm.ExamName); //getting main details from the main resource group
var (galleryname, imagename) = await DoGetGalleryImageDetails(azure, objvm.ExamName);
if (resourceGroupName != "" && galleryname != "" && imagename != "" && details_resgrp!="")
{
var captureImage = azure.GalleryImages.GetByGallery(details_resgrp, galleryname, imagename);
if (captureImage != null)
{
//var security = captureImage.RecommendedVirtualMachineConfiguration;
//var regionName = await getRegion();
var network = azure.Networks.Define(vNetName)
.WithRegion(captureImage.Location)
.WithExistingResourceGroup(resourceGroupName)
.WithAddressSpace(vNetAddress)
.WithSubnet(subnetName, subnetAddress)
.Create();
var nsg = azure.NetworkSecurityGroups.Define(nsgName)
.WithRegion(captureImage.Location)
.WithExistingResourceGroup(resourceGroupName)
.DefineRule("Allow-RDP")
.AllowInbound()
.FromAnyAddress()
.FromAnyPort()
.ToAnyAddress()
.ToPort(3389)
.WithProtocol(SecurityRuleProtocol.Tcp)
.WithPriority(100)
.Attach()
.Create();
var publicIP = azure.PublicIPAddresses.Define(publicIPName)
.WithRegion(captureImage.Location)
.WithExistingResourceGroup(resourceGroupName)
.WithStaticIP()
.Create();
//You need a network security group for controlling the access to the VM
var nic = azure.NetworkInterfaces.Define(nicName)
.WithRegion(captureImage.Location)
.WithExistingResourceGroup(resourceGroupName)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName)
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIP)
.WithExistingNetworkSecurityGroup(nsg)
.Create();
//var security = security
var newVM = azure.VirtualMachines.Define(vmname)
.WithRegion(captureImage.Location)
.WithExistingResourceGroup(resourceGroupName)
.WithExistingPrimaryNetworkInterface(nic)
.WithWindowsCustomImage(captureImage.Id)
.WithAdminUsername(username)
.WithAdminPassword(userpassword)
.WithComputerName(vmname)
.WithSize(VirtualMachineSizeTypes.StandardD4sV3)
.Create();
// newVM.Restart();
response.responseCode = "200";
response.responseMessage = "VM Created";
response.ipAddress = publicIP.Inner.IpAddress.ToString();
response.userName = username;
response.userPassword = userpassword;
response.vmname = vmname;
response.region = newVM.RegionName;
response.vmId = newVM.Id;
if (newVM != null)
{
DateTime shutdown = DateTime.Now.AddHours(4);
ScheduleShutdown(newVM.Id, shutdown, azure);
}
}
else
{
response.responseCode = "100";
response.responseMessage = "VM Not able to create";
}
}
else
{
response.responseCode = "400";
response.responseMessage = "Resource-Group , Image Not exist";
}
}
catch (Exception ex)
{
var message = ex.Message.ToString();
//objlog.LogExceptionToFile(message);
response.responseCode = "500";
response.responseMessage = message;
}
return response;
}
i need to solution to autoshut down while creating vm using azure sdk fluent c sharp