Delen via


Een Windows PowerShell-werkstroom importeren en aanroepen

Windows PowerShell 3 kunt u een werkstroom importeren en aanroepen die is verpakt als een Windows PowerShell module. Zie Een Windows PowerShell module schrijven voor meer informatie over Windows PowerShell modules.

De klasse System.Management.Automation.Psjobproxywordt gebruikt als proxy aan de clientzijde voor werkstroomobjecten op de server. In de volgende procedure wordt uitgelegd hoe u een System.Management.Automation.Psjobproxy-objectgebruikt om een werkstroom aan te roepen.

Een PSJobProxy-object maken om werkstroomopdrachten uit te voeren op een externe server.

  1. Maak een System.Management.Automation.Runspaces.Wsmanconnectioninfo-objectom een verbinding met een externe runspace te maken.

  2. Stel de eigenschap System.Management.Automation.Runspaces.Wsmanconnectioninfo.Shelluri* van het object System.Management.Automation.Runspaces.Wsmanconnectioninfoin om een Windows PowerShell-eindpunt op te Microsoft.PowerShell.Workflow geven.

  3. Maak een runspace die gebruikmaakt van de verbinding die is gemaakt door de vorige stappen uit te voeren.

  4. Maak een System.Management.Automation.Powershell-objecten stel de eigenschap System.Management.Automation.Powershell.Runspace* in op de runspace die u in de vorige stap hebt gemaakt.

  5. Importeer de werkstroommodule en de bijbehorende opdrachten in System.Management.Automation.Powershell.

  6. Maak een System.Management.Automation.Psjobproxy-object en gebruik het om werkstroomopdrachten uit te voeren op de externe server.

Voorbeeld

In het volgende codevoorbeeld ziet u hoe u een werkstroom aanroept met behulp van Windows PowerShell.

Voor dit voorbeeld is Windows PowerShell 3 vereist.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace WorkflowHostTest
{

class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Specify path to Workflow module");
                return;
            }

            string moduleFile = args[0];

            Console.Write("Creating Remote runspace connection...");
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo();

            //Set the shellURI to workflow endpoint Microsoft.PowerShell.Workflow
            connectionInfo.ShellUri = "Microsoft.PowerShell.Workflow";

            //Create and open a runspace.
            Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
            runspace.Open();
            Console.WriteLine("done");

            PowerShell powershell = PowerShell.Create();
            powershell.Runspace = runspace;
            Console.Write("Setting $VerbosePreference=\"Continue\"...");
            powershell.AddScript("$VerbosePreference=\"Continue\"");
            powershell.Invoke();
            Console.WriteLine("done");

            Console.Write("Importing Workflow module...");
            powershell.Commands.Clear();

            //Import the module in to the PowerShell runspace. A XAML file could also be imported directly by using Import-Module.
            powershell.AddCommand("Import-Module").AddArgument(moduleFile);
            powershell.Invoke();
            Console.WriteLine("done");

            Console.Write("Creating job proxy...");
            powershell.Commands.Clear();
            powershell.AddCommand("Get-Proc").AddArgument("*");
            PSJobProxy job = powershell.AsJobProxy();
            Console.WriteLine("done");

                Console.WriteLine();
                Console.WriteLine("Using job proxy and performing operations...");
                Console.WriteLine("State of Job :" + job.JobStateInfo.State.ToString());
                Console.WriteLine("Starting job...");
                job.StartJob();
                Console.WriteLine("State of Job :" + job.JobStateInfo.State.ToString());

                // use blocking enumerator to wait for objects until job finishes
                job.Output.BlockingEnumerator = true;
                foreach (PSObject o in job.Output)
                {
                    Console.WriteLine(o.Properties["ProcessName"].Value.ToString());
                }

                // wait for a random time before attempting to stop job
                Random random = new Random();
                int time = random.Next(1, 10);
                Console.Write("Sleeping for {0} seconds when job is running on another thread...", time);
                System.Threading.Thread.Sleep(time * 1000);
                Console.WriteLine("done");
                Console.WriteLine("Stopping job...");
                job.StopJob();
                Console.WriteLine("State of Job :" + job.JobStateInfo.State.ToString());
                Console.WriteLine();
                job.Finished.WaitOne();
                Console.WriteLine("Output from job");
                Console.WriteLine("---------------");

                foreach (PSObject o in job.Output)
                {
                    Console.WriteLine(o.Properties["ProcessName"].Value.ToString());
                }

                Console.WriteLine();
                Console.WriteLine("Verbose messages from job");
                Console.WriteLine("-------------------------");
                foreach (VerboseRecord v in job.Verbose)
                {
                    Console.WriteLine(v.Message);
                }

            runspace.Close();
        }
    }
}