/// <summary> /// Created a new <see cref="AppDomain"/> and runs the given PowerShell script. /// </summary> /// <param name="fileName">The name of the PowerShell script to run.</param> /// <param name="configFileName">The name of the configuration file. If you set it to null it will default to <code><paramref name="fileName"/>.config</code>.</param> /// <param name="appDomainName">The name of the AppDomain.</param> /// <returns>The output of the script as an array of strings.</returns> public static string[] RunScriptInNewAppDomain(ADPRConfig configuration) { var assembly = Assembly.GetExecutingAssembly(); var setupInfo = new AppDomainSetup { ApplicationName = configuration.AppDomainName, ConfigurationFile = configuration.ConfigFile, // TODO: Perhaps we should setup an even handler to reload the AppDomain similar to ASP.NET in IIS. ShadowCopyFiles = configuration.ShadowCopyFiles.ToString() }; var appDomain = AppDomain.CreateDomain(string.Format("AppDomainPoshRunner-{0}", configuration.AppDomainName), null, setupInfo); try { #if NET_35 var runner = appDomain.CreateInstanceFromAndUnwrap(assembly.Location, typeof(AppDomainPoshRunner).FullName, false, 0, null, new object[] { configuration }, null, null, null); #else var runner = appDomain.CreateInstanceFromAndUnwrap(assembly.Location, typeof(AppDomainPoshRunner).FullName, false, 0, null, new object[] { configuration }, null, null); #endif return(((AppDomainPoshRunner)runner).RunScript(new Uri(Path.GetFullPath(configuration.Script)))); } finally { AppDomain.Unload(appDomain); } }
/// <summary> /// Created a new <see cref="AppDomain"/> and runs the given PowerShell script. /// </summary> /// <param name="fileName">The name of the PowerShell script to run.</param> /// <param name="configFileName">The name of the configuration file. If you set it to null it will default to <code><paramref name="fileName"/>.config</code>.</param> /// <param name="appDomainName">The name of the AppDomain.</param> /// <returns>The output of the script as an array of strings.</returns> public static string[] RunScriptInNewAppDomain(ADPRConfig configuration) { var assembly = Assembly.GetExecutingAssembly(); var setupInfo = new AppDomainSetup { ApplicationName = configuration.AppDomainName, ConfigurationFile = configuration.ConfigFile, // TODO: Perhaps we should setup an even handler to reload the AppDomain similar to ASP.NET in IIS. ShadowCopyFiles = configuration.ShadowCopyFiles.ToString() }; var appDomain = AppDomain.CreateDomain(string.Format("AppDomainPoshRunner-{0}", configuration.AppDomainName), null, setupInfo); try { #if NET_35 var runner = appDomain.CreateInstanceFromAndUnwrap(assembly.Location, typeof(AppDomainPoshRunner).FullName, false, 0, null, new object[] {configuration}, null, null, null); #else var runner = appDomain.CreateInstanceFromAndUnwrap(assembly.Location, typeof(AppDomainPoshRunner).FullName, false, 0, null, new object[] {configuration}, null, null); #endif return ((AppDomainPoshRunner)runner).RunScript(new Uri(Path.GetFullPath(configuration.Script))); } finally { AppDomain.Unload(appDomain); } }
public AppDomainPoshRunner(ADPRConfig config) { _config = config; Stream xmlConfigStream = null; switch (config.Log4NetConfigType) { case Log4NetConfigType.Custom: xmlConfigStream = File.OpenRead(config.Log4NetConfigFile); break; case Log4NetConfigType.Console: xmlConfigStream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("JustAProgrammer.ADPR.ColoredConsoleAppender.log4net.config"); break; case Log4NetConfigType.File: xmlConfigStream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("JustAProgrammer.ADPR.RollingFileAppender.log4net.config"); break; default: throw new ArgumentOutOfRangeException(); } XmlConfigurator.Configure(xmlConfigStream); xmlConfigStream.Close(); xmlConfigStream.Dispose(); }