creating vm from image

Dharshan M 0 Reputation points
2023-11-22T07:49:10.5666667+00:00

i'm trying to create a new vm from a captured image which was creating using c sharp i created now i'm trying to create a new vm from that image using this below code but i'm getting this error/message

"Microsoft.Rest.Azure.CloudException: 'Long running operation failed with status 'Failed'. Additional Info:'OS Provisioning for VM 'Test-3' did not finish in the allotted time. However, the VM guest agent was detected running. This suggests the guest OS has not been properly prepared to be used as a VM image (with CreateOption=FromImage). To resolve this issue, either use the VHD as is with CreateOption=Attach or prepare it properly for use as an image:

"
will anybody help on this ......

  

 public void DOCreateVMFromImage()
        {
            string captureimage = "new_Image";
            string vmName = "Test-3";
            string resourceGrp = "NEW-Test-Grp1";
            string resource = "New_test_grp2"; 
            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 = "NIC-CVM";
            var publicIPName = "TestpublicIp";
            var nsgName = "NSG-Fluent";

            var credentials = SdkContext.AzureCredentialsFactory
                    .FromFile("../../../azure-configuration.json");

            var azure = Azure.Authenticate(credentials).WithDefaultSubscription();

            // Get the captured image
            var capturedImage = azure.VirtualMachineCustomImages.GetByResourceGroup(resourceGrp, captureimage);
            if (capturedImage != null)
            {
               var resourceGroup = azure.ResourceGroups.Define(resource)
             .WithRegion(capturedImage.Region)
             .Create();

                Console.WriteLine($"Creating virtual network {vNetName} ...");
                var network = azure.Networks.Define(vNetName)
                    .WithRegion(capturedImage.Region)
                    .WithExistingResourceGroup(resource)
                    .WithAddressSpace(vNetAddress)
                    .WithSubnet(subnetName, subnetAddress)
                    .Create();

                Console.WriteLine($"Creating public IP {publicIPName} ...");
                var publicIP = azure.PublicIPAddresses.Define(publicIPName)
                    .WithRegion(capturedImage.Region)
                    .WithExistingResourceGroup(resource)
                    .Create();
                //You need a network security group for controlling the access to the VM
                Console.WriteLine($"Creating Network Security Group {nsgName} ...");
                var nsg = azure.NetworkSecurityGroups.Define(nsgName)
                    .WithRegion(capturedImage.Region)
                    .WithExistingResourceGroup(resource)
                    .Create();
                //You need a security rule for allowing the
                //Internet
                Console.WriteLine($"Creating a Security Rule for allowing the remote");
                nsg.Update()
                    .DefineRule("Allow-RDP")
                    .AllowInbound()
                    .FromAnyAddress()
                    .FromAnyPort()
                    .ToAnyAddress()
                    .ToPort(3389)
                    .WithProtocol(SecurityRuleProtocol.Tcp)
                    .WithPriority(100)
                    .Attach()
                    .Apply();


                Console.WriteLine($"Creating network interface {nicName} ...");
                var nic = azure.NetworkInterfaces.Define(nicName)
                         .WithRegion(capturedImage.Region)
                         .WithExistingResourceGroup(resource)
                         .WithExistingPrimaryNetwork(network)
                         .WithSubnet(subnetName)
                         .WithPrimaryPrivateIPAddressDynamic()
                         .WithExistingPrimaryPublicIPAddress(publicIP)
                         .WithExistingNetworkSecurityGroup(nsg)
                         .Create();

                Console.WriteLine($"Creating a new VM using {captureimage}...");

                var newVM = azure.VirtualMachines.Define(vmName)
                                                 .WithRegion(capturedImage.Region)
                                                 .WithExistingResourceGroup(resource)
                                                 .WithExistingPrimaryNetworkInterface(nic)
                                                 .WithWindowsCustomImage(capturedImage.Id)
                                                 .WithAdminUsername(adminUsername)
                                                 .WithAdminPassword(adminPassword)
                                                 .WithComputerName(vmName)
                                                 .WithSize(VirtualMachineSizeTypes.StandardDS2V2)
                                                 .Create();
                ///CheckVMStatus(azure, newVM.Id);
               ///ShutDownVM(azure, newVM.Id);

                Console.WriteLine("Successfully created a new VM: {0}!", vmName);
                Console.WriteLine("Press any key to exit...");
                Console.ReadLine();
            }
        }
       
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,605 questions
0 comments No comments
{count} votes