Pre and post build events in VS C# projects

When you’re doing build automation in TFS you could end up with a non working build because the developers added some non-standard / non-compatible pre or postbuild events to the solution.

So if you want to know if there’s some Visual Studio type PreBuildEvent or PostBuildEvent defined in your solution, then here’s a short PowerShell snippet that you could run:

 

gci $folder\*.csproj -Recurse | %{
  [xml]$rows = gc $_.FullName
  if($rows.Project.PropertyGroup.PreBuildEvent -ne $null)
  {
    Write-Warning ("Contains PreBuild event: {0}" -f $_.FullName)
    $rows.Project.PropertyGroup.PreBuildEvent
  }
  if($rows.Project.PropertyGroup.PostBuildEvent -ne $null)
  {
    Write-Warning ("Contains PostBuild event: {0}" -f $_.FullName)
    $rows.Project.PropertyGroup.PostBuildEvent
  }
}

If on the other hand they are defined as MSBuild type BeforeBuild and AfterBuild directly in the csproj (with support for parameters etc), the script should look like this instead:

gci $folder\*.csproj -Recurse | %{
  [xml]$rows = gc $_.FullName
  if(($rows.Project.Target | ? {$_.Name -match "BeforeBuild"}) -ne $null)
  {
    Write-Warning ("Contains BeforeBuild event: {0}" -f $_.FullName)
    ($rows.Project.Target | ? {$_.Name -match "BeforeBuild"}).OuterXml
  }
  if(($rows.Project.Target | ? {$_.Name -match "AfterBuild"}) -ne $null)
  {
    Write-Warning ("Contains AfterBuild event: {0}" -f $_.FullName)
    ($rows.Project.Target | ? {$_.Name -match "AfterBuild"}).OuterXml
  }
}

Both snippets will start in the directory identified by $folder and recurse through all folders searching for *.csproj. For each folder found they will read the content to find the identifiers for these kind of events.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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