public void Execute(params string[] scripts) { if (scripts == null) { throw new ArgumentNullException(nameof(scripts)); } if (scriptDomain == null) { Evidence securityInfo = new Evidence(AppDomain.CurrentDomain.Evidence); AppDomainSetup info = new AppDomainSetup { ApplicationBase = ExecutingDirectory }; scriptDomain = AppDomain.CreateDomain(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", new object[2] { GetType(), Guid.NewGuid() }), securityInfo, info); messageQueues.AddDebug("AppDomain Created: " + scriptDomain.FriendlyName); CmdletMarshal data = new CmdletMarshal { MessageQueues = messageQueues }; scriptDomain.SetData("cmdMarshal", data); } messageQueues.AddDebug("Creating proxy instance of " + assemblyFullName + " :: type: " + typeFullName); using (PowerShellExecutor powershellExecutor = (PowerShellExecutor)scriptDomain.CreateInstanceAndUnwrap(assemblyFullName, typeFullName)) { messageQueues.AddDebug("Proxy instance created"); foreach (string text in scripts) { messageQueues.AddDebug("Executing: " + text); try { powershellExecutor.Execute(text, Variables); } catch (Exception exception) { messageQueues.AddError(new ErrorRecord(exception, "Proxy Execute Failed", ErrorCategory.OperationStopped, this)); throw; } } } }
public PowerShellExecutor() { Cmdlet = (CmdletMarshal)AppDomain.CurrentDomain.GetData("cmdMarshal"); }