static void Main(string[] args) { // Create a variable for the workflow path. string workflowFileName = "SampleWorkflow.xaml"; // Read the XAML into the variable. string xaml = File.ReadAllText(workflowFileName); // Create a runtime to host the workflow, passing the custom configuration provider. PSWorkflowRuntime runtime = new PSWorkflowRuntime(new SampleConfigurationProvider()); // Parameters to the workflow can be provided in this dictionary. Dictionary <string, object> parameters = new Dictionary <string, object>(); // Pass the ID of the current process, which the sample workflow expects as an input parameter. parameters.Add("ProcessId", (new List <int>() { Process.GetCurrentProcess().Id }).ToArray()); // Create the job, providing the XAML definition. PSWorkflowJob job = runtime.JobManager.CreateJob(Guid.NewGuid(), xaml, "Get-CurrentProcess", "SampleWorkflow", parameters); // Subscribe to the state change event before starting the job. AutoResetEvent wfEvent = new AutoResetEvent(false); job.StateChanged += delegate(object sender, JobStateEventArgs e) { switch (e.JobStateInfo.State) { case JobState.Failed: case JobState.Completed: { wfEvent.Set(); } break; } }; // Start the job. job.StartJob(); // Wait for the state changes event. wfEvent.WaitOne(); if (job.JobStateInfo.State == JobState.Completed) { Console.WriteLine("The job has completed successfully."); Console.WriteLine("Total processes found: " + job.PSWorkflowInstance.Streams.OutputStream.Count); } else { Console.WriteLine("The job has Failed."); } Console.WriteLine("Press <Enter> to continue..."); Console.ReadLine(); }
static void Main(string[] args) { // Set the path to the XAML workflow. string workflowFileName = "SampleWorkflow.xaml"; // Read the XAML from the workflow into the variable string xaml = File.ReadAllText(workflowFileName); // Create a runtime to host the workflow, passing the custom configuration provider to the constructor. PSWorkflowRuntime runtime = new PSWorkflowRuntime(new SampleConfigurationProvider()); // Parameters for the workflow can be provided in this dictionary Dictionary <string, object> parameters = new Dictionary <string, object>(); // Create the job. Because we are using the default file-based store, we need to provide the XAML worklfow definition. PSWorkflowJob job = runtime.JobManager.CreateJob(Guid.NewGuid(), xaml, "Get-CurrentProcess", "SampleWorkflow", parameters); // Subscribe to the state change event before starting the job. AutoResetEvent wfEvent = new AutoResetEvent(false); job.StateChanged += delegate(object sender, JobStateEventArgs e) { switch (e.JobStateInfo.State) { case JobState.Failed: case JobState.Completed: { wfEvent.Set(); } break; } }; // Start the job job.StartJob(); // Wait for the state change event. wfEvent.WaitOne(); if (job.JobStateInfo.State == JobState.Completed) { Console.WriteLine("The job has completed successfully."); Console.WriteLine("Total Commands found: " + job.PSWorkflowInstance.Streams.OutputStream.Count); } else { Console.WriteLine("The job has Failed."); } Console.WriteLine("Press <Enter> to continue..."); Console.ReadLine(); }
static void Main(string[] args) { // Creat a variable for the workflow path. string workflowFileName = "SampleWorkflow.xaml"; // Specify the database server name and instace before executing this command. string dbServer = "ServerName\\InstanceName"; string database = "M3PExtendedStore"; // Read the XAML into the variable string xaml = File.ReadAllText(workflowFileName); string conString = GetConnectionString(dbServer, database); // Create a runtime to host the application, passing the custom configuration provider. PSWorkflowRuntime runtime = new PSWorkflowRuntime(new SampleConfigurationProvider(conString)); // Parameters to the workflow can be provided in this dictionary Dictionary <string, object> parameters = new Dictionary <string, object>(); // Create the job, providing the XAML definition. PSWorkflowJob job = runtime.JobManager.CreateJob(Guid.NewGuid(), xaml, "Get-CurrentProcess", "SampleWorkflow", parameters); // Subscribe to the state change event before starting the job. AutoResetEvent wfEvent = new AutoResetEvent(false); job.StateChanged += delegate(object sender, JobStateEventArgs e) { switch (e.JobStateInfo.State) { case JobState.Failed: case JobState.Completed: case JobState.Suspended: { wfEvent.Set(); } break; } }; // Start the job job.StartJob(); // Wait for the state changes event wfEvent.WaitOne(); // Check whether the workflow is in the suspended state. if (job.JobStateInfo.State == JobState.Suspended) { Console.WriteLine("The job has suspended successfully."); } else { // If not, inform the user that the job was not suspended. Console.WriteLine("The job has not reached a desired state."); Console.ReadLine(); return; } Console.WriteLine("Resuming the job."); // Resume job.ResumeJob(); // Wait for the state changes event wfEvent.WaitOne(); // The workfow should be completed if (job.JobStateInfo.State == JobState.Completed) { Console.WriteLine("The job has completed successfully."); Console.WriteLine("Total Process found: " + job.PSWorkflowInstance.Streams.OutputStream.Count); } else { Console.WriteLine("The job has Failed."); } Console.WriteLine("Press <Enter> to continue..."); Console.ReadLine(); }