public virtual void Dispose(bool disposing) { if (disposing) { _runspace.AvailabilityChanged -= CheckIfRunspaceIsAvailable; _runspace.StateChanged -= CheckIfRunspaceIsOpening; WaitForAvailable(); if (_currentCommand != null) { _currentCommand.Dispose(); _currentCommand = null; } if (_runspaceIsOwned) { if (_runspace != null) { ((IDisposable)_runspace).Dispose(); } _runspace = null; } if (_availableEvent != null) { _availableEvent.Dispose(); } if (_opened != null) { _opened.Dispose(); } } }
public DynamicPowershellResult NewTryInvokeMemberEx(string name, string[] argumentNames, params object[] args) { // make sure that we're clear to drop the last DPSC. WaitForAvailable(); try { // command _currentCommand = new DynamicPowershellCommand(CreatePipeline(), new Command(GetPropertyValue(LookupCommand(name), "Name"))); // parameters var unnamedCount = args.Length - argumentNames.Length; var namedArguments = argumentNames.Select((each, index) => new KeyValuePair <string, object>(each, args[index + unnamedCount])); _currentCommand.SetParameters(args.Take(unnamedCount), namedArguments); #if DETAILED_DEBUG try { Console.WriteLine("[DynamicInvoke] {0} {1}", _currentCommand.Command.CommandText, _currentCommand.Command.Parameters != null && _currentCommand.Command.Parameters.Any() ? _currentCommand.Command.Parameters.Select(each => (each.Name.Is() ? "-" + each.Name : " ") + " " + each.Value.ToString()).Aggregate((each, current) => current + " " + each) : ""); } catch { } #endif // invoke return(_currentCommand.InvokeAsyncIfPossible()); } catch (Exception e) { e.Dump(); return(null); } }
public DynamicPowershellResult NewTryInvokeMemberEx(string name, string[] argumentNames, params object[] args) { // make sure that we're clear to drop the last DPSC. WaitForAvailable(); try { // command // FYI: This is the most expensive part of the call (the first time.) // I've thought about up-fronting this, but it's probably just not worth the effort. _currentCommand = new DynamicPowershellCommand(CreatePipeline(), new Command(LookupCommand(name).Name)); // parameters var unnamedCount = args.Length - argumentNames.Length; var namedArguments = argumentNames.Select((each, index) => new KeyValuePair <string, object>(each, args[index + unnamedCount])); _currentCommand.SetParameters(args.Take(unnamedCount), namedArguments); #if DETAILED_DEBUG try { Console.WriteLine("[DynamicInvoke] {0} {1}", _currentCommand.Command.CommandText, _currentCommand.Command.Parameters != null && _currentCommand.Command.Parameters.Any() ? _currentCommand.Command.Parameters.Select(each => (each.Name.Is() ? "-" + each.Name : " ") + " " + each.Value.ToString()).Aggregate((each, current) => current + " " + each) : ""); } catch { } #endif // invoke return(_currentCommand.InvokeAsyncIfPossible()); } catch (Exception e) { e.Dump(); return(null); } finally { } }
public IEnumerable <PSModuleInfo> TestModuleManifest(string path) { var cmd = new DynamicPowershellCommand(CreatePipeline(), new Command("Test-ModuleManifest")); cmd["Path"] = path; // this call needs to be synchronous. return(cmd.InvokeAsyncIfPossible().Select(each => each as PSModuleInfo).Where(each => each != null).ToArray()); }
public IEnumerable <CommandInfo> GetCommand() { var cmd = new DynamicPowershellCommand(CreatePipeline(), new Command("Get-Command")); cmd["ListImported"] = true; cmd["CommandType"] = new[] { "Cmdlet", "Alias", "Function" }; // this call needs to be synchronous. return(cmd.InvokeAsyncIfPossible().Select(each => each as CommandInfo).Where(each => each != null).ToArray()); }
public DynamicPowershellResult ImportModule(string name, bool passThru) { var cmd = new DynamicPowershellCommand(CreatePipeline(), new Command("Import-Module")); cmd["Name"] = name; if (passThru) { cmd["PassThru"] = true; } return(cmd.InvokeAsyncIfPossible()); }