diff --git a/Solutions/Main/TaskFactories/PowershellTaskFactory/PowerShellTask.cs b/Solutions/Main/TaskFactories/PowershellTaskFactory/PowerShellTask.cs index 0eeaa1f1..a83a7bba 100644 --- a/Solutions/Main/TaskFactories/PowershellTaskFactory/PowerShellTask.cs +++ b/Solutions/Main/TaskFactories/PowershellTaskFactory/PowerShellTask.cs @@ -6,6 +6,7 @@ namespace MSBuild.ExtensionPack.TaskFactory { using System; using System.Diagnostics.Contracts; + using System.Management.Automation; using System.Management.Automation.Runspaces; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; @@ -18,29 +19,66 @@ internal class PowerShellTask : Task, IGeneratedTask, IDisposable /// /// The context that the Windows PowerShell script will run under. /// - private Pipeline pipeline; + private PowerShell powerShell; internal PowerShellTask(string script) { - this.pipeline = RunspaceFactory.CreateRunspace().CreatePipeline(); - this.pipeline.Commands.AddScript(script); - this.pipeline.Runspace.Open(); - this.pipeline.Runspace.SessionStateProxy.SetVariable("log", this.Log); + // ExecutionPolicy Bypass + InitialSessionState initial = InitialSessionState.CreateDefault(); + + // Replace PSAuthorizationManager with a null manager + // which ignores execution policy + initial.AuthorizationManager = new AuthorizationManager("Microsoft.PowerShell"); + + var runspace = RunspaceFactory.CreateRunspace(initial); + runspace.Open(); + runspace.SessionStateProxy.SetVariable("log", this.Log); + + powerShell = PowerShell.Create(); + powerShell.Runspace = runspace; + powerShell.Streams.Information.DataAdded += StreamOnDataAdded; + powerShell.Streams.Verbose.DataAdded += StreamOnDataAdded; + powerShell.Streams.Debug.DataAdded += StreamOnDataAdded; + powerShell.Streams.Warning.DataAdded += StreamOnDataAdded; + powerShell.Streams.Error.DataAdded += StreamOnDataAdded; + powerShell.AddScript(script); + } + + private void StreamOnDataAdded(object sender, DataAddedEventArgs e) + { + switch (sender) + { + case PSDataCollection c: + this.Log.LogWarning(c[e.Index].Message); + break; + case PSDataCollection c: + this.Log.LogMessage(MessageImportance.Low, c[e.Index].Message); + break; + case PSDataCollection c: + this.Log.LogMessage(MessageImportance.Normal, c[e.Index].Message); + break; + case PSDataCollection c: + this.Log.LogMessage(MessageImportance.High, c[e.Index].MessageData.ToString()); + break; + case PSDataCollection c: + this.Log.LogError(c[e.Index].ErrorDetails?.Message ?? c[e.Index].Exception.Message, c[e.Index].Exception); + break; + } } public object GetPropertyValue(TaskPropertyInfo property) { - return this.pipeline.Runspace.SessionStateProxy.GetVariable(property.Name); + return this.powerShell.Runspace.SessionStateProxy.GetVariable(property.Name); } public void SetPropertyValue(TaskPropertyInfo property, object value) { - this.pipeline.Runspace.SessionStateProxy.SetVariable(property.Name, value); + this.powerShell.Runspace.SessionStateProxy.SetVariable(property.Name, value); } public override bool Execute() { - this.pipeline.Invoke(); + this.powerShell.Invoke(); return !Log.HasLoggedErrors; } @@ -54,10 +92,10 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - if (this.pipeline.Runspace != null) + if (this.powerShell.Runspace != null) { - this.pipeline.Runspace.Dispose(); - this.pipeline = null; + this.powerShell.Runspace.Dispose(); + this.powerShell = null; } } } diff --git a/Solutions/Main/TaskFactories/PowershellTaskFactory/PowershellTaskFactory.csproj b/Solutions/Main/TaskFactories/PowershellTaskFactory/PowershellTaskFactory.csproj index 6d363718..43cef385 100644 --- a/Solutions/Main/TaskFactories/PowershellTaskFactory/PowershellTaskFactory.csproj +++ b/Solutions/Main/TaskFactories/PowershellTaskFactory/PowershellTaskFactory.csproj @@ -53,9 +53,8 @@ - - False - $(WinDir)\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll + + ..\..\packages\Microsoft.PowerShell.5.ReferenceAssemblies.1.1.0\lib\net4\System.Management.Automation.dll @@ -68,6 +67,7 @@ + diff --git a/Solutions/Main/TaskFactories/PowershellTaskFactory/packages.config b/Solutions/Main/TaskFactories/PowershellTaskFactory/packages.config new file mode 100644 index 00000000..3a6ca975 --- /dev/null +++ b/Solutions/Main/TaskFactories/PowershellTaskFactory/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file