I'm currently working on a C# application using the Azure Fluent SDK to create a virtual machine from a captured image. The code works well for standard scenarios, but I'm facing challenges when trying to set the Trusted Launch property for the virtual machine. I'm using azure fluent sdk and there is no security type for trusted launch in Microsoft azure management fluent sdk
public void DOCreateVMFromImage()
{
string captureimage = "Image1";
string vmName = "Test-4";
string resourceGrp = "TESTVMGROUP";
string resource = "TESTVMGROUP";
string adminUsername = "Student1";
string adminPassword = "student1Excel";
var location = Region.USEast;
var vNetName = "VNET-Fluent";
var vNetAddress = "172.16.0.0/16";
var subnetName = "Subnet-Fluent";
var subnetAddress = "172.16.0.0/24";
var nicName = "NICVM";
var publicIPName = "Test4publicIp";
var nsgName = "NSGi-Fluent";
var sharedImageGalleryName = "NewImages"; // Replace with your actual gallery name
var sharedImageDefinitionName = "Image1"; // Replace with your actual image definition name
var sharedImageVersion = "0.0.1";
var credentials = SdkContext.AzureCredentialsFactory
.FromFile("../../../azure-configuration.json");
var azure = Azure.Authenticate(credentials).WithDefaultSubscription();
// Get the captured image
var capturedImage = azure.GalleryImages.GetByGallery(resource, sharedImageGalleryName, sharedImageDefinitionName);
if (capturedImage != null)
{
// var resourceGroup = azure.ResourceGroups.Define(resource)
//.WithRegion(capturedImage.Region)
//.Create();
//capturedImage.o
Console.WriteLine($"Creating virtual network {vNetName} ...");
var network = azure.Networks.Define(vNetName)
.WithRegion(capturedImage.Location)
.WithExistingResourceGroup(resource)
.WithAddressSpace(vNetAddress)
.WithSubnet(subnetName, subnetAddress)
.Create();
Console.WriteLine($"Creating public IP {publicIPName} ...");
var publicIP = azure.PublicIPAddresses.Define(publicIPName)
.WithRegion(capturedImage.Location)
.WithExistingResourceGroup(resource)
.Create();
//You need a network security group for controlling the access to the VM
Console.WriteLine($"Creating Network Security Group {nsgName} ...");
Console.WriteLine($"Creating Network Security Group {nsgName} ...");
var nsg = azure.NetworkSecurityGroups.Define(nsgName)
.WithRegion(capturedImage.Location)
.WithExistingResourceGroup(resource)
.DefineRule("Allow-RDP")
.AllowInbound()
.FromAnyAddress()
.FromAnyPort()
.ToAnyAddress()
.ToPort(3389)
.WithProtocol(SecurityRuleProtocol.Tcp)
.WithPriority(100)
.Attach()
.Create();
Console.WriteLine($"Creating network interface {nicName} ...");
var nic = azure.NetworkInterfaces.Define(nicName)
.WithRegion(capturedImage.Location)
.WithExistingResourceGroup(resource)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName)
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIP)
.WithExistingNetworkSecurityGroup(nsg)
.Create();
Console.WriteLine($"Creating a new VM using {captureimage}...");
SecurityProfile objsec = new SecurityProfile();
var newVM = azure.VirtualMachines.Define(vmName)
.WithRegion(capturedImage.Location)
.WithExistingResourceGroup(resource)
.WithExistingPrimaryNetworkInterface(nic)
.WithWindowsCustomImage(capturedImage.Id)
.WithAdminUsername(adminUsername)
.WithAdminPassword(adminPassword)
.WithComputerName(vmName)
.WithSize(VirtualMachineSizeTypes.StandardD2sV3)
.Create();
newVM.Restart();
Console.WriteLine("Successfully created a new VM: {0}!", vmName);
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
}
I'm using the Azure Fluent SDK to create a virtual machine from a captured image in C#. The code is working fine for standard scenarios, but I'm having trouble setting the Trusted Launch property for the virtual machine.
According to this Document the
Microsoft.Azure.Management.Fluentis deprecated. you need to useAzure.ResourceManagerpackage to create a Azure VM withtrusted launch enabledwith Azure SDK in C#.You can use the below C# sample code to create an Azure VM with trusted launch enabled.
Code:
The above code executed and created the Azure VM with using identity and Azure.ResourceManager package.
Output:
Reference: