/// <summary> /// Override EndProcessing. /// </summary> protected override void EndProcessing() { if (_powerShell != null) { _powerShell.Dispose(); } }
private void RunService() { InitialSessionState initalState = InitialSessionState.CreateDefault(); List <string> modulesToLoad = Common.ProcessManifest(initalState); lock (psLocker) { powershellHost = new Host(); powershellEngine = System.Management.Automation.PowerShell.Create(); powershellEngine.Runspace = RunspaceFactory.CreateRunspace(powershellHost, initalState); powershellEngine.Runspace.ApartmentState = System.Threading.ApartmentState.MTA; powershellEngine.Runspace.Open(); } try { RunScript("Set-ExecutionPolicy Unrestricted -Scope Process"); } catch { } foreach (string module in modulesToLoad) { try { RunScript(string.Format("Import-Module {0} -Scope Global", module)); } catch (Exception e) { EventLog.WriteEntry(PSService.PSServiceName, string.Format("Error occured while initializing modules.{0}{0}Name: {1}{0}Error: {2}", Environment.NewLine, module, e.Message), EventLogEntryType.Error); } } RunScript(Common.GetScript("ScriptData")); RunScript("if (Get-Command OnStart -ea SilentlyContinue) { OnStart }"); RunScript("Main"); lock (psLocker) { powershellEngine.Dispose(); powershellEngine = null; } thread.Abort(); thread.Join(); Common.CleanUp(); }
protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { runspace.Dispose(); powershell.Dispose(); } powershell.Streams.Debug.DataAdded -= DebugDataAdded; powershell.Streams.Verbose.DataAdded -= VerboseDataAdded; powershell.Streams.Information.DataAdded -= InformationDataAdded; powershell.Streams.Warning.DataAdded -= WarningDataAdded; powershell.Streams.Error.DataAdded -= ErrorDataAdded; powershell.Streams.Progress.DataAdded -= ProgressDataAdded; disposedValue = true; } }
public void Dispose() { _pwrSh.Dispose(); }