コード例 #1
0
        private static async Task SetupAndTearDownVirtualMachine(VMManagementControllerParameters managementControllerParameters)
        {
            using (VMManagementController controller = new VMManagementController(managementControllerParameters))
            {
                // Retrieve a list of available images and display a choice of Windows images

                Console.WriteLine("Please choose an OS image to use:");

                var images = await controller.GetImagesList();

                var windowsImages = images.Where(i => i.OperatingSystemType == "Windows" && i.PublisherName == "Microsoft Windows Server Group");
                for (int i = 0; i < windowsImages.Count(); i++)
                {
                    Console.WriteLine(i + 1 + ". " + windowsImages.ElementAt(i).Label);
                }
                var imageId = Console.ReadLine();

                // Keep a reference to the chosen image

                var imageToGet = windowsImages.ElementAt(int.Parse(imageId) - 1);

                // Start the steps to create the VM

                Console.WriteLine("1. Creating Storage Account named {0} in Region {1}...", managementControllerParameters.StorageAccountName, managementControllerParameters.Region);

                // Create the Storage Account
                await controller.CreateStorageAccount();

                Console.WriteLine("...Complete");

                Console.WriteLine("2. Creating a Cloud Service named {0} in Region {1}", managementControllerParameters.CloudServiceName, managementControllerParameters.Region);
                ConsoleContinuePrompt("Create the Cloud Service");

                // Create the Cloud Service
                await controller.CreateCloudService();

                Console.WriteLine("...Complete");

                Console.WriteLine("3. Create the Virtual Machine");
                ConsoleContinuePrompt("Create the VM");

                // Create the Virtual Machine
                await controller.CreateVirtualMachine(imageToGet.Name);

                // Wait for the Virtual Machine to be ready
                controller.PollVMStatus("ReadyRole", 5, (s) =>
                {
                    Console.WriteLine("Waiting... Current status: " + s);
                });

                Console.WriteLine("...Complete. You can now log on the Virtual Machine.");

                Console.WriteLine("4. Shut down the Virtual Machine and deallocate resources");
                ConsoleContinuePrompt("Shutdown the VM");

                // Shutdown the Virtual Machine
                await controller.StopVirtualMachine(true);

                // Wait for the Virtual Machine to be stopped
                controller.PollVMStatus("StoppedDeallocated", 5, (s) =>
                {
                    Console.WriteLine("Waiting... Current status: " + s);
                });

                Console.WriteLine("...Complete.");

                Console.WriteLine("5. Start the Virtual Machine again");
                ConsoleContinuePrompt("Start the VM");

                // Shutdown the Virtual Machine
                await controller.StartVirtualMachine();

                // Wait for the Virtual Machine to be ready
                controller.PollVMStatus("ReadyRole", 5, (s) =>
                {
                    Console.WriteLine("Waiting... Current status: " + s);
                });

                Console.WriteLine("...Complete. You can now log back on the Virtual Machine.");

                Console.WriteLine("6. Delete Virtual Machine");
                ConsoleContinuePrompt("Delete the VM");

                // Delete the Virtual Machine
                await controller.DeleteVirtualMachine();

                // Wait for the disk to disappear
                controller.PollVHDBlob(5, () =>
                {
                    Console.WriteLine("Waiting...");
                });

                Console.WriteLine("...Complete");

                Console.WriteLine("7. Delete Storage Account {0}", managementControllerParameters.StorageAccountName);
                ConsoleContinuePrompt("Delete the Storage Account");

                // Delete the Storage Account
                await controller.DeleteStorageAccount();

                Console.WriteLine("...Complete");
            }
        }
コード例 #2
0
        private static async Task SetupAndTearDownVirtualMachine(VMManagementControllerParameters managementControllerParameters)
        {
            using (VMManagementController controller = new VMManagementController(managementControllerParameters))
            {
                // Retrieve a list of available images and display a choice of Windows images

                Console.WriteLine("Please choose an OS image to use:");

                var images = await controller.GetImagesList();
                var windowsImages = images.Where(i => i.OperatingSystemType == "Windows" && i.PublisherName == "Microsoft Windows Server Group");
                for (int i = 0; i < windowsImages.Count(); i++)
                {
                    Console.WriteLine(i + 1 + ". " + windowsImages.ElementAt(i).Label);
                }
                var imageId = Console.ReadLine();

                // Keep a reference to the chosen image

                var imageToGet = windowsImages.ElementAt(int.Parse(imageId) - 1);

                // Start the steps to create the VM

                Console.WriteLine("1. Creating Storage Account named {0} in Region {1}...", managementControllerParameters.StorageAccountName, managementControllerParameters.Region);

                // Create the Storage Account
                await controller.CreateStorageAccount();

                Console.WriteLine("...Complete");

                Console.WriteLine("2. Creating a Cloud Service named {0} in Region {1}", managementControllerParameters.CloudServiceName, managementControllerParameters.Region);
                ConsoleContinuePrompt("Create the Cloud Service");

                // Create the Cloud Service
                await controller.CreateCloudService();

                Console.WriteLine("...Complete");

                Console.WriteLine("3. Create the Virtual Machine");
                ConsoleContinuePrompt("Create the VM");

                // Create the Virtual Machine
                await controller.CreateVirtualMachine(imageToGet.Name);

                // Wait for the Virtual Machine to be ready
                controller.PollVMStatus("ReadyRole", 5, (s) =>
                {
                    Console.WriteLine("Waiting... Current status: " + s);
                });

                Console.WriteLine("...Complete. You can now log on the Virtual Machine.");

                Console.WriteLine("4. Shut down the Virtual Machine and deallocate resources");
                ConsoleContinuePrompt("Shutdown the VM");

                // Shutdown the Virtual Machine
                await controller.StopVirtualMachine(true);

                // Wait for the Virtual Machine to be stopped
                controller.PollVMStatus("StoppedDeallocated", 5, (s) =>
                {
                    Console.WriteLine("Waiting... Current status: " + s);
                });

                Console.WriteLine("...Complete.");

                Console.WriteLine("5. Start the Virtual Machine again");
                ConsoleContinuePrompt("Start the VM");

                // Shutdown the Virtual Machine
                await controller.StartVirtualMachine();

                // Wait for the Virtual Machine to be ready
                controller.PollVMStatus("ReadyRole", 5, (s) =>
                {
                    Console.WriteLine("Waiting... Current status: " + s);
                });

                Console.WriteLine("...Complete. You can now log back on the Virtual Machine.");

                Console.WriteLine("6. Delete Virtual Machine");
                ConsoleContinuePrompt("Delete the VM");

                // Delete the Virtual Machine
                await controller.DeleteVirtualMachine();

                // Wait for the disk to disappear
                controller.PollVHDBlob(5, () =>
                {
                    Console.WriteLine("Waiting...");
                });

                Console.WriteLine("...Complete");

                Console.WriteLine("7. Delete Storage Account {0}", managementControllerParameters.StorageAccountName);
                ConsoleContinuePrompt("Delete the Storage Account");

                // Delete the Storage Account
                await controller.DeleteStorageAccount();

                Console.WriteLine("...Complete");
            }
        }