namespace Microsoft.PowerShell.CoreConsoleHost { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Host; using System.Management.Automation.Runspaces; using System.Text; using System.IO; using System.Runtime.InteropServices; using System.Linq; using PowerShell = System.Management.Automation.PowerShell; public static class Program { private enum Options {Help, File, Command, NoProfile, NonInteractive, EncodedCommand, Invalid}; private class CommandOptions { public Options opt; public string optString; public CommandOptions(Options o, string s) { opt = o; optString = s; } } private static CommandOptions[] commandOptions = new CommandOptions[] { new CommandOptions(Options.Help, "help"), new CommandOptions(Options.Help, "?"), new CommandOptions(Options.File, "file"), new CommandOptions(Options.Command, "command"), new CommandOptions(Options.NoProfile, "noprofile"), new CommandOptions(Options.NonInteractive, "noninteractive"), new CommandOptions(Options.EncodedCommand, "encodedcommand") }; /// /// Creates and initiates the listener instance. /// public static void Main(string[] args) { // Setup the Assembly Load Context, which Core PowerShell uses to // analyze the libraries for types, functions, cmdlets, etc. and // provide the ability to load assemblies by file path. Doing this // here eliminates the need for a custom native host. PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); // Argument parsing string initialScript = null; bool loadProfiles = true; if (args.Length > 0) { for (int i = 0; i < args.Length; ++i) { string arg = args[i]; bool hasNext = (i+1) < args.Length; string nextArg = hasNext ? args[i+1] : string.Empty; // options start with "-" or "--" string option; if (arg.StartsWith("-")) { if (arg.StartsWith("--")) { option = arg.Remove(0, 2); } else { option = arg.Remove(0, 1); } Options bestOption = FindOption(option); switch (bestOption) { case Options.Help: Console.WriteLine(@" usage: powershell[.exe] [ (--help | -h | -?) ] [ (--file | -f) ] [