Exemple #1
0
        /// <summary>
        /// This sample uses the RunspaceInvoke class to execute
        /// a script that calls exit. The host application looks at
        /// this and prints out the result.
        /// </summary>
        /// <param name="args">Unused</param>
        static void Main(string[] args)
        {
            // Create an instance of this class so that the engine will have
            // access to the ShouldExit and ExitCode parameters.
            Host01 me = new Host01();

            // Now create the host instance to use
            MyHost myHost = new MyHost(me);

            // Pass this in when creating the runspace and invoker...
            Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost);

            myRunSpace.Open();
            RunspaceInvoke invoker = new RunspaceInvoke(myRunSpace);

            // Now use the runspace invoker to execute the script "exit (2+2)"
            string script = "exit (2+2)";

            invoker.Invoke(script);

            // Check the flags and see if they were set propertly...
            Console.WriteLine(
                "ShouldExit={0} (should be True); ExitCode={1} (should be 4)",
                me.ShouldExit, me.ExitCode);

            // close the runspace...
            myRunSpace.Close();

            Console.WriteLine("Hit any key to exit...");
            Console.ReadKey();
        }
        /// <summary>
        /// This sample uses the PowerShell class to execute
        /// a script that calls exit. The host application looks at
        /// this and prints out the result.
        /// </summary>
        /// <param name="args">Unused</param>
        static void Main(string[] args)
        {
            // Create an instance of this class so that the engine will have
            // access to the ShouldExit and ExitCode parameters.
            Host01 me = new Host01();

            // Now create the host instance to use
            MyHost myHost = new MyHost(me);

            // Pass this in when creating the runspace and invoker...
            Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost);
            myRunSpace.Open();

            // Create a PowerShell to execute our commands...
            PowerShell powershell = PowerShell.Create();
            powershell.Runspace = myRunSpace;

            // Now use the runspace invoker to execute the script "exit (2+2)"
            string script = "exit (2+2)";
            powershell.AddScript(script);
            powershell.Invoke(script);

            // Check the flags and see if they were set propertly...
            Console.WriteLine(
                "ShouldExit={0} (should be True); ExitCode={1} (should be 4)",
                me.ShouldExit, me.ExitCode);

            // close the runspace...
            myRunSpace.Close();

            Console.WriteLine("Hit any key to exit...");
            Console.ReadKey();
        }
Exemple #3
0
 /// <summary>
 /// Create this instance of the console listener.
 /// </summary>
 private PSListenerConsoleSample()
 {
     // Create the host and runspace instances for this interpreter. Note that
     // this application doesn't support console files so only the default snapins
     // will be available.
     this.myHost = new MyHost(this);
     this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost);
     this.myRunSpace.Open();
 }
Exemple #4
0
 /// <summary>
 /// Create this instance of the console listener.
 /// </summary>
 private PSListenerConsoleSample()
 {
     // Create the host and runspace instances for this interpreter. Note that
     // this application doesn't support console files so only the default snapins
     // will be available.
     this.myHost     = new MyHost(this);
     this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost);
     this.myRunSpace.Open();
 }
Exemple #5
0
        /// <summary>
        /// A sample application that uses the PowerShell runtime along with a host
        /// implementation to call get-process and display the results as you
        /// would see them in pwrsh.exe.
        /// </summary>
        /// <param name="args">Ignored</param>
        static void Main(string[] args)
        {
            // Set the current culture to German. We want this to be picked up when the MyHost
            // instance is created...
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-de");

            // Create the runspace, but this time we aren't using the RunspaceInvoke
            // class

            MyHost   myHost     = new MyHost(new Host02());
            Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost);

            myRunSpace.Open();

            // Create a PowerShell to execute our commands...
            PowerShell powershell = PowerShell.Create();

            powershell.Runspace = myRunSpace;

            // Add the script we want to run. The script does two things. It runs get-process with
            // the output sorted by handle count, get-date piped to out-string so we can see the
            // date being displayed in German...

            powershell.AddScript(@"
                get-process | sort handlecount
                # This should display the date in German...
                get-date | out-string
                ");

            // Now add the default outputter to the end of the pipe and indicate
            // that it should handle both output and errors from the previous
            // commands. This will result in the output being written using the PSHost
            // and PSHostUserInterface classes instead of returning objects to the hosting
            // application.

            powershell.AddCommand("out-default");
            powershell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

            // Now just invoke the application - there won't be any objects returned -
            // they're all consumed by out-default so we don't have to do anything more...

            powershell.Invoke();

            System.Console.WriteLine("Hit any key to exit...");
            System.Console.ReadKey();
        }
Exemple #6
0
        /// <summary>
        /// A sample application that uses the PowerShell runtime along with a host
        /// implementation to call get-process and display the results as you
        /// would see them in pwrsh.exe.
        /// </summary>
        /// <param name="args">Parameter not used.</param>
        private static void Main(string[] args)
        {
            // Set the current culture to German. We want this to be picked up when the MyHost
            // instance is created...
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-de");

            // Create the runspace, but this time we aren't using the RunspaceInvoke
            // class
            MyHost myHost = new MyHost(new Host02());
            using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost))
            {
                myRunSpace.Open();

                // Create a PowerShell to execute our commands...
                using (PowerShell powershell = PowerShell.Create())
                {
                    powershell.Runspace = myRunSpace;

                    // Add the script we want to run. The script does two things. It runs get-process with
                    // the output sorted by handle count, get-date piped to out-string so we can see the
                    // date being displayed in German...
                    powershell.AddScript(@"
                        get-process | sort handlecount
                        # This should display the date in German...
                        get-date | out-string
                        ");

                    // Now add the default outputter to the end of the pipe and indicate
                    // that it should handle both output and errors from the previous
                    // commands. This will result in the output being written using the PSHost
                    // and PSHostUserInterface classes instead of returning objects to the hosting
                    // application.
                    powershell.AddCommand("out-default");

                    powershell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

                    // Now just invoke the application - there won't be any objects returned -
                    // they're all consumed by out-default so we don't have to do anything more...
                    powershell.Invoke();
                }
            }

            System.Console.WriteLine("Hit any key to exit...");
            System.Console.ReadKey();
        }
Exemple #7
0
        /// <summary>
        /// Initializes a new instance of the PSListenerConsoleSample class.
        /// </summary>
        public PSListenerConsoleSample()
        {
            // Create the host and runspace instances for this interpreter.
            // Note that this application does not support console files so
            // only the default snap-ins will be available.
            this.myHost = new MyHost(this);
            this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost);
            this.myRunSpace.Open();

            // Create a PowerShell object to run the commands used to create
            // $profile and load the profiles.
            lock (this.instanceLock)
            {
                this.currentPowerShell = PowerShell.Create();
            }

            try
            {
                this.currentPowerShell.Runspace = this.myRunSpace;

                PSCommand[] profileCommands = Microsoft.Samples.PowerShell.Host.HostUtilities.GetProfileCommands("SampleHost06");
                foreach (PSCommand command in profileCommands)
                {
                    this.currentPowerShell.Commands = command;
                    this.currentPowerShell.Invoke();
                }
            }
            finally
            {
                // Dispose the PowerShell object and set currentPowerShell
                // to null. It is locked because currentPowerShell may be
                // accessed by the ctrl-C handler.
                lock (this.instanceLock)
                {
                    this.currentPowerShell.Dispose();
                    this.currentPowerShell = null;
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Initializes a new instance of the PSListenerConsoleSample class.
        /// </summary>
        public PSListenerConsoleSample()
        {
            // Create the host and runspace instances for this interpreter.
            // Note that this application does not support console files so
            // only the default snap-ins will be available.
            this.myHost     = new MyHost(this);
            this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost);
            this.myRunSpace.Open();

            // Create a PowerShell object to run the commands used to create
            // $profile and load the profiles.
            lock (this.instanceLock)
            {
                this.currentPowerShell = PowerShell.Create();
            }

            try
            {
                this.currentPowerShell.Runspace = this.myRunSpace;

                PSCommand[] profileCommands = Microsoft.Samples.PowerShell.Host.HostUtilities.GetProfileCommands("SampleHost06");
                foreach (PSCommand command in profileCommands)
                {
                    this.currentPowerShell.Commands = command;
                    this.currentPowerShell.Invoke();
                }
            }
            finally
            {
                // Dispose the PowerShell object and set currentPowerShell
                // to null. It is locked because currentPowerShell may be
                // accessed by the ctrl-C handler.
                lock (this.instanceLock)
                {
                    this.currentPowerShell.Dispose();
                    this.currentPowerShell = null;
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Create this instance of the console listener.
        /// </summary>
        private PSListenerConsoleSample()
        {
            // Create the host and runspace instances for this interpreter. Note that
            // this application doesn't support console files so only the default snapins
            // will be available.
            this.myHost     = new MyHost(this);
            this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost);
            this.myRunSpace.Open();

            // Create a PowerShell object that will be used to execute the commands
            // to create $profile and load the profiles.
            lock (this.instanceLock)
            {
                this.currentPowerShell = PowerShell.Create();
            }

            try
            {
                this.currentPowerShell.Runspace = this.myRunSpace;

                PSCommand[] profileCommands = Microsoft.Samples.PowerShell.Host.HostUtilities.GetProfileCommands("SampleHost04");
                foreach (PSCommand command in profileCommands)
                {
                    this.currentPowerShell.Commands = command;
                    this.currentPowerShell.Invoke();
                }
            }
            finally
            {
                // Dispose of the pipeline line and set it to null, locked because currentPowerShell
                // may be accessed by the ctrl-C handler...
                lock (this.instanceLock)
                {
                    this.currentPowerShell.Dispose();
                    this.currentPowerShell = null;
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// Create this instance of the console listener.
        /// </summary>
        private PSListenerConsoleSample()
        {
            // Create the host and runspace instances for this interpreter. Note that
            // this application doesn't support console files so only the default snapins
            // will be available.
            this.myHost = new MyHost(this);
            this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost);
            this.myRunSpace.Open();

            // Create a PowerShell object that will be used to execute the commands
            // to create $profile and load the profiles.
            lock (this.instanceLock)
            {
                this.currentPowerShell = PowerShell.Create();
            }

            try
            {
                this.currentPowerShell.Runspace = this.myRunSpace;

                PSCommand[] profileCommands = Microsoft.Samples.PowerShell.Host.HostUtilities.GetProfileCommands("SampleHost04");
                foreach (PSCommand command in profileCommands)
                {
                    this.currentPowerShell.Commands = command;
                    this.currentPowerShell.Invoke();
                }
            }
            finally
            {
                // Dispose of the pipeline line and set it to null, locked because currentPowerShell
                // may be accessed by the ctrl-C handler...
                lock (this.instanceLock)
                {
                    this.currentPowerShell.Dispose();
                    this.currentPowerShell = null;
                }
            }
        }