static void Main(string[] args) { var app = Cli.NewApp(); app.Authors.Add("r.uchiyama"); app.Usage = "Hyper-V Command Tools"; // Global Flags var outputOption = new StringFlag("output"); outputOption.Alias = new string[] { "out", "o" }; outputOption.Usage = "Choose Output format."; outputOption.SetDefaultValue("json"); var serverOption = new StringFlag("server"); serverOption.Alias = new string[] { "svr", "s" }; serverOption.Usage = "Choose Execute Server"; serverOption.SetDefaultValue(""); var loggerOption = new BoolFlag("logger"); loggerOption.Usage = "Use Logger"; loggerOption.SetDefaultValue(false); app.Flags.Add(outputOption); app.Flags.Add(serverOption); app.Flags.Add(loggerOption); #region vm command // vm command var vmCommand = new Command("vm"); vmCommand.Usage = "Virtual Machine Action"; // vm list var vm_ListCommand = new Command("list"); vm_ListCommand.Action = (ctx) => { PowerShellResult result; using (var p = PowerShellProcess.Default("Get-VM")) { ApplyLogger(p, ctx.Bool("logger")); p.RemoteServer = ctx.String("server"); result = Execute(p, Output.GetOutputs(ctx.String("output"))); } WriteResult(result); }; // vm start var vm_StartCommand = new Command("start"); var nameOption = new StringFlag("name"); nameOption.Alias = new string[] { "n" }; nameOption.Usage = "The name of the Virtual Machine."; vm_StartCommand.Flags.Add(nameOption); vm_StartCommand.Action = (ctx) => { PowerShellResult result; string name = ctx.String("name"); if (string.IsNullOrEmpty(name)) { Console.WriteLine("name not found"); return; } using (var p = PowerShellProcess.Default("Start-VM")) { p.AddFlag("-Name", name); p.RemoteServer = ctx.String("server"); ApplyLogger(p, ctx.Bool("logger")); result = Execute(p, Output.GetOutputs(ctx.String("output"))); } WriteResult(result); }; // vm stop var vm_StopCommand = new Command("stop"); vm_StopCommand.Flags.Add(nameOption); vm_StopCommand.Action = (ctx) => { PowerShellResult result; string name = ctx.String("name"); if (string.IsNullOrEmpty(name)) { Console.WriteLine("name not found"); return; } using (var p = PowerShellProcess.Default("Stop-VM")) { p.AddFlag("-Name", name); p.RemoteServer = ctx.String("server"); ApplyLogger(p, ctx.Bool("logger")); result = Execute(p, Output.GetOutputs(ctx.String("output"))); } WriteResult(result); }; // vm list-ip-addresses var vm_ListIpAddressesCommand = new Command("list-ip-addresses"); vm_ListIpAddressesCommand.Action = (ctx) => { PowerShellResult result; using (var p = PowerShellProcess.Default("Get-VM")) { p.AddPipe().AddCommand("select").AddFlag("-ExpandProperty", "NetworkAdapters").AddPipe().AddCommand("select").AddParameters("VMName", ",IPAddresses"); p.RemoteServer = ctx.String("server"); ApplyLogger(p, ctx.Bool("logger")); result = Execute(p, Output.GetOutputs(ctx.String("output"))); } WriteResult(result); }; vmCommand.SubCommands.Add(vm_ListCommand); vmCommand.SubCommands.Add(vm_StartCommand); vmCommand.SubCommands.Add(vm_StopCommand); vmCommand.SubCommands.Add(vm_ListIpAddressesCommand); app.Commands.Add(vmCommand); #endregion app.Run(args); }