In PowerShell Tools for Visual Studio you can create PowerShell projects that allow for some special functionality for PowerShell solutions. Not only does it allow you to organize your script and module projects but you can also create forms, package scripts as executables and run scripts during pre and post build events. In this blog post, we will be looking at how to take advantage of the latter.

Configuring Build Events

Build events happen when you execute a build in Visual Studio or MSBuild. PowerShell projects, like any Visual Studio project, are a set of MSBuild tasks and properties that tell MSBuild what to run during a build. PowerShell projects don’t actually have a build step per-se (unless you are using the packager) but you can still execute scripts when you run a Build in Visual Studio.

To configure a build event, right click on your project and select properties.

When the property page for your project opens, you’ll have some tabs to choose from. There are some settings for debugging, module manifests, build events and advanced settings for configuring packaging and bundling. If you select the Build Events tab you will be presented with two text boxes that allow you to configure the pre and post build events. The pre build event will run before the packaging and the post will run after it. If you are not using packaging, then the events will just run one after another.

In the above example, I’m just executing the script in my project during the build.

Using MSBuild Variables

The build event scripts support expanding MSBuild variables. There are a lot of built-in variables that are set during builds in both Visual Studio and MSBuild. You can use these variables directly in your script. They will automatically be expanded by MSBuild.

  • Name – Name of the project
  • Configuration – Current configuration of the project (Release\Debug)
  • SolutionDir – The solution directory
  • DevEnvDir – The installation directory of Visual Studio

You can even use the values set by the other pages in your scripts. For example, if you set the module manifest path and description, you will be able to use those in your script. My build event could look like this.

 Write-Host '$(ManifestPath)'
 Write-Host '$(Description)'

One caveat is that you may have to configure this within the PSSProj file itself. The build events need to be run after the properties are set.

<PropertyGroup>
<Configuration Condition=” ‘$(Configuration)’ == ” “>Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>6CAFC0C6-A428-4d30-A9F9-700E829FEA51</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>MyApplication</RootNamespace>
<AssemblyName>MyApplication</AssemblyName>
<Name>BuildSteps</Name>
<ManifestPath>Module.psd1</ManifestPath>
<Description>Description of module</Description>
<Guid>e10b2721-6551-4f48-811a-f765701b8d6d</Guid>
<ProcessorArchitecture />
<PreBuildScript>
Write-Host ‘$(Name)’
Write-Host ‘$(Configuration)’
Write-Host ‘$(ManifestPath)’
Write-Host ‘$(Description)’
Write-Host ‘$(SolutionDir)’
Write-Host ‘$(DevEnvDir)’
</PreBuildScript>
</PropertyGroup>

Looking in the PSSProj file is another good way to see which variables you have available.

A Practical Example: Signing a Script

A practical example of build events is signing a script during the build. This can be accomplished with Set-AuthenticodeSignature. You can use the $(SolutionDir) variable to allow you to find the script on disk in a dynamic fashion and then sign it in the post-build event.

 $cert = Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert 
 Set-AuthenticodeSignature -Certificate $cert -FilePath '$(SolutionDir)BuildSteps\Script.ps1'

Conclusion

In this post we looked at how to use PowerShell Tools for Visual Studio to execute pre and post build events in MSBuild and Visual Studio. We also showed how to use MSBuild variables and sign a script after a build.