using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management.Automation.Runspaces; using System.Collections.ObjectModel; using System.IO; using Lobster.Components.PowerShell; namespace Lobster.PowerShell { [System.ComponentModel.Composition.Export(typeof(IShellScript))] public abstract class ShellScriptBase : IShellScript { protected Runspace Runspace {get; private set;} protected Pipeline Pipeline { get; private set; } public Collection<object> Results { get; protected set; } public abstract event Action<IShellScript> OperationCompleted; public bool IsValid { get; protected set; } public string ValidityMessage { get; protected set; } public ShellScriptBase() { try { Results = null; // create Powershell runspace Runspace = RunspaceFactory.CreateRunspace(); // open it Runspace.Open(); // create a pipeline and feed it the script text Pipeline = Runspace.CreatePipeline(); IsValid = true; } catch (Exception e) { ValidityMessage = "Error in ShellScriptBase.Ctor: " + e.ToString(); IsValid = false; } } public void AddParameter(string name, object value) { try { if (!IsValid) return; Runspace.SessionStateProxy.SetVariable(name, value); } catch (Exception e) { ValidityMessage = "Error in ShellScriptBase.AddParameter: " + e.ToString(); IsValid = false; } } public void AddInput(object value) { try { if (!IsValid) return; Pipeline.Input.Write(value); } catch (Exception e) { ValidityMessage = "Error in ShellScriptBase.AddInput: " + e.ToString(); IsValid = false; } } public bool AddScriptFromFile(string pathName) { try { if (!IsValid) return false; var script = File.ReadAllText(pathName); Pipeline.Commands.AddScript(script); } catch (Exception e) { ValidityMessage = "Error in ShellScriptBase.AddScriptFromFile: " + e.ToString(); IsValid = false; } return IsValid; } public abstract bool Run(); public abstract ShellScriptType Type { get; } public abstract object Clone(); } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)