Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell task factory execution policy issue #98

Open
wants to merge 3 commits into
base: releases/4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,29 +19,66 @@ internal class PowerShellTask : Task, IGeneratedTask, IDisposable
/// <summary>
/// The context that the Windows PowerShell script will run under.
/// </summary>
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<WarningRecord> c:
this.Log.LogWarning(c[e.Index].Message);
break;
case PSDataCollection<DebugRecord> c:
this.Log.LogMessage(MessageImportance.Low, c[e.Index].Message);
break;
case PSDataCollection<VerboseRecord> c:
this.Log.LogMessage(MessageImportance.Normal, c[e.Index].Message);
break;
case PSDataCollection<InformationRecord> c:
this.Log.LogMessage(MessageImportance.High, c[e.Index].MessageData.ToString());
break;
case PSDataCollection<ErrorRecord> 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;
}

Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(WinDir)\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.PowerShell.5.ReferenceAssemblies.1.1.0\lib\net4\System.Management.Automation.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -68,6 +67,7 @@
<Compile Include="Properties\ExtraAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="XmlSamples\PowerShellTaskFactory.proj" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.PowerShell.5.ReferenceAssemblies" version="1.1.0" targetFramework="net40" />
</packages>