VisualStudio project build: automating obfuscation by using powerShell

Sometimes we need do run something automatically after or before a project-build is running in Visual Studio. So we can unload the project, and edit the project file and add some build-steps inside there by using the <Target> attribute.

If you want to run a.e. a build-step after the build you can use this:

<Target Name="AfterBuild">
</Target Name>

Now if you want to do something really special, where you need programming support, its really easy to add powerShell support here. What I like is to run a powerShell script that is located inside the solution like this:

03-09-2013 10-11-18

What I want to do is, to run foreach-smartAssembly configuration in the Projects folder the SmartAssembly as a commandline. For this its really simple to create the RunSmartAssembly.ps1 file that looks like this:

$ErrorActionPreference = 'Stop'
$currentDir = Split-Path $MyInvocation.MyCommand.Path
$saDir = "C:\Program Files\Red Gate\SmartAssembly 6"
echo "Current Dir:$currentDir"
foreach($file in Get-ChildItem "$currentDir/Projects" -filter "*.saproj")
 & "$saDir\" /build $file.FullName

We need the $ErrorActionPreference, because when something goes wrong in this script, we need to tell this to the project-build. Otherwice we dont see the build-error in the VisualStudio Output or ErrorList.

Now lets see how to execute this script from inside the .csproj file:

– Unload your project and edit your .csproj file

– Go down to the end of the xml where you see some comments by default

– add this lines to execute the powerShell script after the project build:

    <PowerShellExe Condition=" '$(PowerShellExe)'=='' ">$(WINDIR)\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
   <Target Name="AfterBuild">
    <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy bypass -command &quot;&amp;{./RunSmartAssembly.ps1%3Bexit $LastExitCode}&quot;">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />

In the propertyGroup “PowershellExe” we define the path to the powerShell application. By using Exec in the AfterBuild target we run the powerShell. after -command you could directly execute powerShell code. We are running another powerShell script with ./the_script_to_run.ps1. Next we exit with the $LastExitCode. So if the execution of our script fails, your build will also fail. Otherwice you would get a passing build, also if there where errors occurred in your script.

By using this approach I am able to put how many saproj files int the Project folder, and each one becomes executed. I am not using the way of running the smartAssembly with msbuild, because then I have to always unload the csproj files and edit the code there. By using this way of running the obfuscation you can define the buildorder of the Obfiscation-project and a.e. disable it for the Debug-mode.

I like this automated approach, and I hope this was helpful for someone else too :)

About weiss92

Software Developer
This entry was posted in PowerShell, Visual Studio and tagged , , , , , . Bookmark the permalink.

One Response to VisualStudio project build: automating obfuscation by using powerShell

  1. Just the info I needed. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s