共用方式為


匯入並叫用 Windows PowerShell 工作流程

Windows PowerShell 3,可讓您匯入並叫用封裝為Windows PowerShell模組的工作流程。 如需Windows PowerShell模組的相關資訊,請參閱撰寫Windows PowerShell模組

System.Management.Automation.Psjobproxy類別是做為伺服器上工作流程物件的用戶端 Proxy。 下列程式說明如何使用 System.Management.Automation.Psjobproxy物件來叫用工作流程。

建立 PSJobProxy 物件,以在遠端伺服器上執行工作流程命令。

  1. 建立 System.Management.Automation.Runspaces.Wsmanconnectioninfo物件,以建立遠端 Runspace 的連線。

  2. System.Management.Automation.Runspaces.Wsmanconnectioninfo.Shelluri*屬性設定為System.Management.Automation.Runspaces.Wsmanconnectioninfo物件, Microsoft.PowerShell.Workflow 以指定Windows PowerShell端點。

  3. 建立 Runspace,以使用完成先前步驟所建立的連線。

  4. 建立 System.Management.Automation.Powershell物件,並將其 System.Management.Automation.Powershell.Runspace* 屬性設定為上一個步驟中建立的 Runspace。

  5. 將工作流程模組及其命令匯入 System.Management.Automation.Powershell

  6. 建立 System.Management.Automation.Psjobproxy 物件,並用它來在遠端伺服器上執行工作流程命令。

範例

下列程式碼範例示範如何使用 Windows PowerShell叫用工作流程。

此範例需要 Windows PowerShell 3。

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();
        }
    }
}