private PowerShellPackageProvider Create(string psModule) { dynamic ps = new DynamicPowershell(); try { // load the powershell provider functions into this runspace. var psf = ps.ImportModule(Name: PowerShellProviderFunctions, PassThru: true); DynamicPowershellResult result = ps.ImportModule(Name: psModule, PassThru: true); if (!result.LastIsTerminatingError) { var providerModule = result.Value as PSModuleInfo; if (result.Success && providerModule != null) { try { return(new PowerShellPackageProvider(ps, providerModule)); } catch (Exception e) { e.Dump(); } } } } catch (Exception e) { // something didn't go well. // skip it. e.Dump(); } // didn't import correctly. ps.Dispose(); return(null); }
public virtual object Execute(T cmdlet) { var restCommand = RestService.ReverseLookup[cmdlet.GetType()]; var result = new PowershellReponse(); AsynchronouslyEnumerableList <ErrorRecord> errors = null; using (var dps = new DynamicPowershell(RestService.RunspacePool)) { if (cmdlet.Session != null && cmdlet.Session.HasRole("password_must_be_changed") && typeof(T) != typeof(SetServicePassword)) { result.Warnings = new string[] { "WARNING: USER PASSWORD SHOULD BE CHANGED.\r\n" }; } var dynamicResult = dps.Invoke(restCommand.Name, _persistableElements, cmdlet, restCommand.DefaultParameters, restCommand.ForcedParameters, out errors); result.Output = dynamicResult.ToArray(); result.LastIsTerminatingError = dynamicResult.LastIsTerminatingError; } if (errors != null && errors.Any()) { result.Error = errors.Select(e => new RemoteError { Category = e.CategoryInfo.Category, Message = e.FullyQualifiedErrorId, ExceptionType = e.Exception.GetType().Name, ExceptionMessage = e.Exception.Message, }).ToArray(); } return(result); }
public void Invoke(string command, params string[] arguments) { var p = PowerShell.Create(); p.Runspace.SessionStateProxy.SetVariable("request", _request); p.AddScript(_request.HelperModuleText,false); p.AddScript(command); foreach (var result in p.Invoke()) { // dunno what to do with the result yet. } p.Dispose(); p = null; using (dynamic ps = new DynamicPowershell()) { // grant access to the current call request. ps["request"] = _request; // import our new helpers DynamicPowershellResult result = ps.ImportModule(Name: _request.HelperModulePath, PassThru: true); if (!result.Success) { throw new Exception("Unable to load helper module for install script."); } result = ps.InvokeExpression(command); if (!result.Success) { foreach (var i in result.Errors) { _request.Error(i.CategoryInfo.Reason, i.Exception.Message, null); } throw new Exception("Failed executing chocolatey script."); } ps["request"] = null; } }
public PowerShellProviderBase(DynamicPowershell ps, PSModuleInfo module) { if (module == null) { throw new ArgumentNullException("module"); } _powershell = ps; _module = module; // combine all the cmdinfos we care about // but normalize the keys as we go (remove any '-' '_' chars) foreach (var k in _module.ExportedAliases.Keys) { _allCommands.AddOrSet(k.Replace("-", "").Replace("_", ""), _module.ExportedAliases[k]); } foreach (var k in _module.ExportedCmdlets.Keys) { _allCommands.AddOrSet(k.Replace("-", "").Replace("_", ""), _module.ExportedCmdlets[k]); } foreach (var k in _module.ExportedFunctions.Keys) { _allCommands.AddOrSet(k.Replace("-", "").Replace("_", ""), _module.ExportedFunctions[k]); } }
public virtual object Execute(T cmdlet) { // credential gets set by the filter. var restCommand = RestService.ReverseLookup[cmdlet.GetType()]; using (var dps = new DynamicPowershell(RestService.RunspacePool)) { return(dps.Invoke(restCommand.Name, _persistableElements, cmdlet, restCommand.DefaultParameters, restCommand.ForcedParameters)); } }
public void TestPipeline() { dynamic ps = new DynamicPowershell(); DynamicPowershellResult result = ps.Dir(@"c:\"); DynamicPowershellResult result2 = ps.TestPath(result); foreach (var r in result2) { Console.WriteLine(r); } }
internal IEnumerable <string> ScanForModules(Request request) { // two places we search for modules // 1. in this assembly's folder, look for all psd1 and psm1 files. // // 2. modules in the PSMODULEPATH // // Import each one of those, and check to see if they have a OneGet.Providers section in their private data using (dynamic ps = new DynamicPowershell()) { // load the powershell functions into this runspace in case something needed it on module load. var psf = ps.ImportModule(Name: PowerShellProviderFunctions, PassThru: true); #if DEBUG var testProviders = Directory.EnumerateFiles(BaseFolder, "*.psm1", SearchOption.AllDirectories); foreach (var provider in testProviders) { yield return(provider); } #endif // scan all the ps modules in the folders provided foreach (var each in AlternativeModuleScan(request)) { DynamicPowershellResult result; result = ps.TestModuleManifest(each); var values = result.ToArray(); foreach (var v in values) { var moduleData = v as PSModuleInfo; if (moduleData == null) { continue; } foreach (var m in GetOneGetModules(moduleData)) { yield return(m); } } } } }
public void Invoke(string command, params string[] arguments) { var p = PowerShell.Create(); p.Runspace.SessionStateProxy.SetVariable("request", _request); p.AddScript(_request.HelperModuleText, false); p.AddScript(command); foreach (var result in p.Invoke()) { // dunno what to do with the result yet. } p.Dispose(); p = null; using (dynamic ps = new DynamicPowershell()) { // grant access to the current call request. ps["request"] = _request; // import our new helpers DynamicPowershellResult result = ps.ImportModule(Name: _request.HelperModulePath, PassThru: true); if (!result.Success) { throw new Exception("Unable to load helper module for install script."); } result = ps.InvokeExpression(command); if (!result.Success) { foreach (var i in result.Errors) { _request.Error(i.CategoryInfo.Reason, i.Exception.Message, null); } throw new Exception("Failed executing chocolatey script."); } ps["request"] = null; } }
internal IEnumerable <string> ScanForModules(PsRequest request) { // two places we search for modules // 1. in this assembly's folder, look for all psd1 and psm1 files. // // 2. modules in the PSMODULEPATH // // Import each one of those, and check to see if they have a OneGet.Providers section in their private data using (var ps = new DynamicPowershell()) { // load the powershell functions into this runspace in case something needed it on module load. var psf = ps.ImportModule(PowerShellProviderFunctions, true); // scan all the ps modules in the folders provided foreach (var each in AlternativeModuleScan(request)) { foreach (var ogModule in ps.TestModuleManifest(each).SelectMany(GetOneGetModules)) { yield return(ogModule); } } } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (_powershell != null) { _powershell.Dispose(); _powershell = null; } if (_result != null) { _result.Dispose(); _result = null; } if (_reentrancyLock != null) { _reentrancyLock.Dispose(); _reentrancyLock = null; } _module = null; } }
public override void Configure(Container container) { _configured = true; // Feature disableFeatures = Feature.Jsv | Feature.Soap; SetConfig(new EndpointHostConfig { // EnableFeatures = Feature.All.Remove(disableFeatures), //all formats except of JSV and SOAP DebugMode = true, //Show StackTraces in service responses during development WriteErrorsToResponse = false, //Disable exception handling DefaultContentType = ContentType.Json, //Change default content type AllowJsonpRequests = true, //Enable JSONP requests ServiceName = "RestService", }); LogManager.LogFactory = new DebugLogFactory(); using (dynamic ps = new DynamicPowershell(SharedRunspacePool)) { foreach (var commandName in _commands) { PSObject command = ps.ResolveCommand(commandName); if (command != null) { var cmdletInfo = (command.ImmediateBaseObject as CmdletInfo); if (cmdletInfo != null) { Routes.Add(cmdletInfo.ImplementingType, "/"+commandName+"/", "GET"); } } } } }
public PowerShellPackageProvider(DynamicPowershell ps, PSModuleInfo module) : base(ps, module) { }
internal IEnumerable <string> ScanForModules(Request request) { // two places we search for modules // 1. in this assembly's folder, look for all psd1 and psm1 files. // // 2. modules in the PSMODULEPATH // // Import each one of those, and check to see if they have a OneGet.Providers section in their private data using (dynamic ps = new DynamicPowershell()) { if (BaseFolder != null) { var files = Directory.EnumerateFiles(BaseFolder, "*.psd1", SearchOption.AllDirectories); // load the powershell functions into this runspace in case something needed it on module load. var psf = ps.ImportModule(Name: PowerShellProviderFunctions, PassThru: true); #if DEBUG var testProviders = Directory.EnumerateFiles(BaseFolder, "*.psm1", SearchOption.AllDirectories); foreach (var provider in testProviders) { yield return(provider); } #endif foreach (var each in files) { DynamicPowershellResult items = ps.ImportModule(Name: each, PassThru: true); items.WaitForCompletion(); var errors = items.Errors.ToArray(); if (errors.Any()) { request.Debug("\r\n\r\n==================================================================================\r\n===In Module '{0}'", each); foreach (var error in errors) { try { switch (error.CategoryInfo.Category) { case System.Management.Automation.ErrorCategory.ResourceUnavailable: // file not found continue; default: request.Debug(" PowerShell {0} {1} ", error.CategoryInfo.Category, error.Exception.Message); break; } } catch (Exception e) { e.Dump(); } } continue; } foreach (var onegetModule in items.OfType <PSModuleInfo>().SelectMany(GetOneGetModules)) { yield return(onegetModule); } } } foreach (var onegetModule in ModulesFromResult((DynamicPowershellResult)ps.GetModule(ListAvailable: true)).SelectMany(GetOneGetModules)) { yield return(onegetModule); } } }