Skip to content

Instantly share code, notes, and snippets.

@palpha
Created September 2, 2015 13:12
Show Gist options
  • Save palpha/d303d4aa1e4a51af5b47 to your computer and use it in GitHub Desktop.
Save palpha/d303d4aa1e4a51af5b47 to your computer and use it in GitHub Desktop.
Simple inline PowerShell task for MSBuild 14
<UsingTask TaskName="PowerShell" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<ScriptBlock ParameterType="System.String" Required="true" />
<ScriptParams ParameterType="System.String" Required="false" />
</ParameterGroup>
<Task>
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"/>
<Code Type="Class" Language="cs"><![CDATA[
public class PowerShell : Microsoft.Build.Tasks.Exec {
public string ScriptParams {
set {
Command = Command + " " + value;
}
}
public string ScriptBlock {
set {
EchoOff = true;
var tmp = System.IO.Path.GetTempFileName();
try {
System.IO.File.WriteAllText(tmp, value);
System.IO.File.Move(tmp, tmp + ".ps1");
Command = string.Format( "@powershell -NoProfile -NoLogo -File {0}", tmp + ".ps1" );
} finally {
System.IO.File.Delete(tmp + ".ps1");
}
}
}
}]]>
</Code>
</Task>
</UsingTask>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment