Mengimpor dan Memanggil Alur Kerja Windows PowerShell

Windows PowerShell 3, memungkinkan Anda mengimpor dan memanggil alur kerja yang dikemas sebagai modul Windows PowerShell. Untuk informasi tentang modul Windows PowerShell, lihat Menulis Modul Windows PowerShell.

Kelas System.Management.Automation.Psjobproxydigunakan sebagai proksi sisi klien untuk objek alur kerja di server. Prosedur berikut menjelaskan cara menggunakan objek System.Management.Automation.Psjobproxyuntuk memanggil alur kerja.

Membuat objek PSJobProxy untuk menjalankan perintah alur kerja di server jarak jauh.

  1. Buat objek System.Management.Automation.Runspaces.Wsmanconnectioninfountuk membuat koneksi ke runspace jarak jauh.

  2. Atur properti System.Management.Automation.Runspaces.Wsmanconnectioninfo.Shelluri* dari objek System.Management.Automation.Runspaces.Wsmanconnectioninfoke untuk Microsoft.PowerShell.Workflow menentukan titik akhir Windows PowerShell.

  3. Buat runspace yang menggunakan koneksi yang dibuat dengan menyelesaikan langkah-langkah sebelumnya.

  4. Buat objek System.Management.Automation.Powershelldan atur properti System.Management.Automation.Powershell.Runspace* ke runspace yang dibuat di langkah sebelumnya.

  5. Impor modul alur kerja dan perintahnya ke System.Management.Automation.Powershell.

  6. Buat objek System.Management.Automation.Psjobproxy dan gunakan untuk menjalankan perintah alur kerja di server jarak jauh.

Contoh

Contoh kode berikut menunjukkan cara memanggil alur kerja dengan menggunakan Windows PowerShell.

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