// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. // // Copyright (c) Microsoft. All rights reserved. namespace Microsoft.Samples.HyperV.PVM { using System; using System.Globalization; using System.Management; using System.Reflection; using System.Collections.Generic; class Program { /// /// Entry point of the program. /// /// Command line arguments. static void Main( string[] args) { if (args.Length > 1 && args.Length < 5) { if (string.Equals(args[0], "ImportVm", StringComparison.OrdinalIgnoreCase)) { string vmDefinitionPath = string.Empty; string snapshotFolderPath = string.Empty; bool newId = true; if (args.Length < 2 || args.Length > 4) { ShowUsage(); return; } if (args.Length >= 2) { vmDefinitionPath = args[1]; } if (args.Length >= 3) { snapshotFolderPath = args[2]; } if (args.Length == 4) { if (string.Equals(args[3], "True", StringComparison.OrdinalIgnoreCase)) { newId = true; } else if (string.Equals(args[3], "False", StringComparison.OrdinalIgnoreCase)) { newId = false; } else { ShowUsage(); } } using (ManagementObject pvm = ImportUtilities.ImportVm(vmDefinitionPath, snapshotFolderPath, newId)) { Console.WriteLine("Imported Planned VM: {0} ({1}).\n", pvm["ElementName"], pvm["Name"]); } } else if (string.Equals(args[0], "ImportSnapshots", StringComparison.OrdinalIgnoreCase)) { if (args.Length == 3) { string pvmName = args[1]; string snapshotFolderPath = args[2]; IList snapshots = ImportUtilities.ImportSnasphotDefinitions(pvmName, snapshotFolderPath); Console.WriteLine("Imported snapshots: \n"); foreach (ManagementObject snapshot in snapshots) using (snapshot) { Console.WriteLine(" {0} ({1})\n", snapshot["ElementName"], snapshot["InstanceID"]); } } else { ShowUsage(); } } else if (string.Equals(args[0], "ValidatePvm", StringComparison.OrdinalIgnoreCase)) { if (args.Length == 2) { string pvmName = args[1]; ImportUtilities.ValidatePvm(pvmName); } else { ShowUsage(); } } else if (string.Equals(args[0], "RealizePvm", StringComparison.OrdinalIgnoreCase)) { if (args.Length == 2) { string pvmName = args[1]; using (ManagementObject vm = ImportUtilities.RealizePvm(pvmName)) { Console.WriteLine("Realized Virtual Machine: {0} ({1})", vm["ElementName"], vm["Name"]); } } else { ShowUsage(); } } else if (string.Equals(args[0], "RemovePvm", StringComparison.OrdinalIgnoreCase)) { if (args.Length == 2) { string pvmName = args[1]; ImportUtilities.RemovePvm(pvmName); } } else if (string.Equals(args[0], "FixVHDPaths", StringComparison.OrdinalIgnoreCase)) { if (args.Length == 3) { string pvmName = args[1]; string vhdFolderPath = args[2]; FixUpUtilities.FixVhdPaths(pvmName, vhdFolderPath); } else { ShowUsage(); } } else { ShowUsage(); } } else { ShowUsage(); } } /// /// Displays the command line usage for the program. /// static void ShowUsage() { string moduleName = Assembly.GetExecutingAssembly().GetModules()[0].Name; Console.WriteLine("Usage:\t{0} \n", moduleName); Console.WriteLine("Supported SampleNames and Arguments:\n"); Console.WriteLine(" ImportVm [SnapshotFolderPath] [NewId]\n"); Console.WriteLine(" ImportSnapshots \n"); Console.WriteLine(" ValidatePvm \n"); Console.WriteLine(" RealizePvm \n"); Console.WriteLine(" RemovePvm \n"); Console.WriteLine(" FixVHDPaths \n"); } } }