Advertisement
Guest User

PowerShellInvoker

a guest
Jul 20th, 2012
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management.Automation;
  5. using System.Management.Automation.Runspaces;
  6. using System.Text;
  7.  
  8. namespace OpenIdProvider.Shared
  9. {
  10.     public class PowerShellInvoker : IDisposable
  11.     {
  12.         private readonly Runspace _runspace;
  13.         private readonly RunspaceInvoke _invoker;
  14.  
  15.         public PowerShellInvoker(params string[] modules)
  16.         {
  17.             var iss = InitialSessionState.CreateDefault();
  18.             iss.ImportPSModule(modules);
  19.             iss.ThrowOnRunspaceOpenError = true;
  20.  
  21.             _runspace = RunspaceFactory.CreateRunspace(iss);
  22.             _runspace.Open();
  23.             _invoker = new RunspaceInvoke(_runspace);
  24.         }
  25.  
  26.         public ICollection<PSObject> ExecuteScript(string scriptText)
  27.         {
  28.             return _invoker.Invoke(scriptText).ToList();
  29.         }
  30.  
  31.         public ICollection<PSObject> ExecuteCommand(string cmdlet, object parameters = null)
  32.         {
  33.             using (var pipeline = _runspace.CreatePipeline())
  34.             {
  35.                 var cmd = new Command(cmdlet);
  36.  
  37.                 if (parameters != null)
  38.                 {
  39.                     foreach (var property in parameters.GetType().GetProperties())
  40.                     {
  41.                         cmd.Parameters.Add(property.Name, property.GetValue(parameters, null));
  42.                     }
  43.                 }
  44.  
  45.                 pipeline.Commands.Add(cmd);
  46.                
  47.                 var result = pipeline.Invoke();
  48.  
  49.                 if (pipeline.Error.Count > 0)
  50.                 {
  51.                     var psError = (PSObject) pipeline.Error.Read();
  52.                     var error = (ErrorRecord) psError.BaseObject;
  53.                     throw error.Exception;
  54.                 }
  55.  
  56.                 return result.ToList();
  57.             }
  58.         }
  59.  
  60.         public void SetVariable(string name, object value)
  61.         {
  62.             _runspace.SessionStateProxy.SetVariable(name, value);
  63.         }
  64.  
  65.         #region Implementation of IDisposable
  66.  
  67.         public void Dispose()
  68.         {
  69.             if (_invoker != null)
  70.             {
  71.                 _invoker.Dispose();
  72.             }
  73.  
  74.             if (_runspace != null)
  75.             {
  76.                 _runspace.Close();
  77.                 _runspace.Dispose();
  78.             }
  79.         }
  80.  
  81.         #endregion
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement