Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Created January 6, 2015 15:32
Show Gist options
  • Save fearthecowboy/ccd5e0694d508900ebf9 to your computer and use it in GitHub Desktop.
Save fearthecowboy/ccd5e0694d508900ebf9 to your computer and use it in GitHub Desktop.
Super Simple PowerShell task for MSBuild.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- @FearTheCowboy's Simple PowerShell Task for MSBuild -->
<UsingTask TaskName="PowerShell" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup><ScriptBlock ParameterType="System.String" Required="true" /></ParameterGroup>
<Task><Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"/><Code Type="Class" Language="cs"><![CDATA[
public class PowerShell : Microsoft.Build.Tasks.Exec {
public string ScriptBlock {set { EchoOff=true; Command = string.Format( "@powershell \"Invoke-Command -ScriptBlock {{ $errorActionPreference='Stop'; {0} ; exit $LASTEXITCODE }} \"", value.Replace("\"","\"\"").Replace("\r\n",";").Replace("\n",";").Replace("\r",";")); } }
}]]></Code></Task>
</UsingTask>
<PropertyGroup>
<MyTestScript>
$foo = 'Hello There'
echo "$foo - script dir : $(MSBuildThisFileDirectory)"
</MyTestScript>
</PropertyGroup>
<Target Name="Build">
<PowerShell ScriptBlock="$(MyTestScript)" />
</Target>
</Project>
@regme
Copy link

regme commented Oct 26, 2015

👍 Thanx!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment