protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _psRunspace.Dispose(); } _disposed = true; } }
private static Runspace getRunspace() { if (runspace != null) return runspace; RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); PSSnapInException ex = null; PSSnapInInfo pSSnapInInfo = null; // Exchange 2007 try { pSSnapInInfo = runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out ex); } catch {} // Exchange 2010 try { pSSnapInInfo = runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out ex); } catch {} // Exchange 2013 try { pSSnapInInfo = runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.SnapIn", out ex); } catch {} if (pSSnapInInfo != null) { Exception except = null; try { runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); } catch (Exception exc) { except = exc; runspace.Dispose(); runspace = null; } if (except != null) throw except; } else { throw new ExchangeServerException("Couldn't initialize PowerShell Runspace"); } return runspace; }
public static string ExecPowerShellCommand(string sCommand, bool bRemoveEmptyLines) { if (runspace == null) { PSSnapInInfo info = null; PSSnapInException ex = null; bool error = false; try { runspace = RunspaceFactory.CreateRunspace(RunspaceConfiguration.Create()); runspace.Open(); foreach (string pssnapin in ExchangePsSnapin) { if (Execute("$(Get-PSSnapin -Registered | Select-String " + pssnapin + ") -ne $null", true).Trim() == "True") { info = runspace.RunspaceConfiguration.AddPSSnapIn(pssnapin, out ex); } } } catch (Exception) { error = true; } if (ex != null || info == null || error) { if (runspace != null) { runspace.Dispose(); runspace = null; } throw new ExchangeServerException("Couldn't initialize PowerShell runspace."); } } return Execute(sCommand, bRemoveEmptyLines); }
protected void DestroyRunspace(Runspace runspace) { runspace.Events.ForwardEvent -= new EventHandler<PSEventArgs>(this.OnRunspaceForwardEvent); runspace.Close(); runspace.Dispose(); lock (this.runspaceList) { this.runspaceList.Remove(runspace); this.totalRunspaces = this.runspaceList.Count; } }
public virtual void Dispose() { runspace.Dispose(); }
internal Runspace getAnotherRunspace(Runspace existingRunspace) { returnRunspace(existingRunspace, false); existingRunspace.Dispose(); return acquireRunspace(); }
internal static void CloseRunspace(Runspace runspace) { runspace.Dispose(); }
private void RecommitToRunspacePool(Runspace runspace) { if (runspacePool.Count > 3) { runspace.Dispose(); return; } runspacePool.Add(runspace); }