• Stars
    star
    145
  • Rank 246,247 (Top 5 %)
  • Language
    PowerShell
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Invoke-MsBuild PowerShell module to make building projects and solutions with MsBuild.exe easy.

Invoke-MsBuild PowerShell Module

A PowerShell module to make building projects and solutions with MsBuild easy. It provides features such as:

  • Check if the build succeeded or failed
  • Automatically open the build log file when the build fails
  • View the build output in the current console window, a new window, or not at all
  • Fire-and-forget building (via the -PassThru switch)

The module simply passes through any MsBuild command-line parameters you supply, so it supports all functionality (e.g. project types, targets, etc.) that you would get by calling MsBuild directly. The module builds using the Visual Studio Command Prompt when available in order to support more project types that MsBuild.exe alone may not support, such as XNA projects.

Getting Started

You can either download the Invoke-MsBuild.psm1 file from the Releases page directly, or install the module from the PowerShell Gallery.

Here is an example of how to import the Invoke-MsBuild module into your powershell session and call it:

Import-Module -Name "C:\PathTo\Invoke-MsBuild.psm1"
Invoke-MsBuild -Path "C:\Some Folder\MySolution.sln"

When the -PassThru switch is provided, the process being used to run MsBuild.exe is returned.

When the -PassThru switch is not provided, a hash table with the following properties is returned:

  • BuildSucceeded = $true if the build passed, $false if the build failed, and $null if we are not sure.
  • BuildLogFilePath = The path to the build's log file.
  • BuildErrorsLogFilePath = The path to the build's error log file.
  • ItemToBuildFilePath = The item that MsBuild ran against.
  • CommandUsedToBuild = The full command that was used to invoke MsBuild. This can be useful for inspecting what parameters are passed to MsBuild.exe.
  • Message = A message describing any problems that were encountered by Invoke-MsBuild. This is typically an empty string unless something went wrong.
  • MsBuildProcess = The process that was used to execute MsBuild.exe.
  • BuildDuration = The amount of time the build took to complete, represented as a TimeSpan.

Examples

$buildResult = Invoke-MsBuild -Path "C:\Some Folder\MySolution.sln"

if ($buildResult.BuildSucceeded -eq $true)
{
  Write-Output ("Build completed successfully in {0:N1} seconds." -f $buildResult.BuildDuration.TotalSeconds)
}
elseif ($buildResult.BuildSucceeded -eq $false)
{
  Write-Output ("Build failed after {0:N1} seconds. Check the build log file '$($buildResult.BuildLogFilePath)' for errors." -f $buildResult.BuildDuration.TotalSeconds)
}
elseif ($null -eq $buildResult.BuildSucceeded)
{
  Write-Output "Unsure if build passed or failed: $($buildResult.Message)"
}

Perform the default MsBuild actions on the Visual Studio solution to build the projects in it, and returns a hash table containing the results. The PowerShell script will halt execution until MsBuild completes.


$process = Invoke-MsBuild -Path "C:\Some Folder\MySolution.sln" -PassThru

while (!$process.HasExited)
{
  Write-Host "Solution is still building..."
  Start-Sleep -Seconds 1
}

Perform the default MsBuild actions on the Visual Studio solution to build the projects in it. The PowerShell script will not halt execution; instead it will return the process running MsBuild.exe back to the caller while the build is performed. You can check the process's HasExited property to check if the build has completed yet or not.


if ((Invoke-MsBuild -Path $pathToSolution).BuildSucceeded -eq $true)
{
  Write-Output "Build completed successfully."
}

Perform the build against the file specified at $pathToSolution and checks it for success in a single line.


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" -MsBuildParameters "/target:Clean;Build" -ShowBuildOutputInNewWindow

Cleans then Builds the given C# project. A window displaying the output from MsBuild will be shown so the user can view the progress of the build without it polluting their current terminal window.


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" -ShowBuildOutputInCurrentWindow

Builds the given C# project and displays the output from MsBuild in the current terminal window.


Invoke-MsBuild -Path "C:\MySolution.sln" -MsBuildParameters "/target:Clean;Build /property:Configuration=Release;Platform=x64;BuildInParallel=true /verbosity:Detailed /maxcpucount"

Cleans then Builds the given solution, specifying to build the project in parallel in the Release configuration for the x64 platform. Here the shorter "Params" alias is used instead of the full "MsBuildParameters" parameter name.


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" -ShowBuildOutputInNewWindow -PromptForInputBeforeClosing -AutoLaunchBuildLogOnFailure

Builds the given C# project. A window displaying the output from MsBuild will be shown so the user can view the progress of the build, and it will not close until the user gives the window some input after the build completes. This function will also not return until the user gives the window some input, halting the powershell script execution. If the build fails, the build log will automatically be opened in the default text viewer.


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" -BuildLogDirectoryPath "C:\BuildLogs" -KeepBuildLogOnSuccessfulBuilds -AutoLaunchBuildErrorsLogOnFailure

Builds the given C# project. The build log will be saved in "C:\BuildLogs", and they will not be automatically deleted even if the build succeeds. If the build fails, the build errors log will automatically be opened in the default text viewer.


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" -BuildLogDirectoryPath PathDirectory

Builds the given C# project. The keyword 'PathDirectory' is used, so the build log will be saved in "C:\Some Folder", which is the same directory as the project being built (i.e. directory specified in the Path).


Invoke-MsBuild -Path "C:\Database\Database.dbproj" -P "/t:Deploy /p:TargetDatabase=MyDatabase /p:TargetConnectionString=`"Data Source=DatabaseServerName`;Integrated Security=True`;Pooling=False`" /p:DeployToDatabase=True"

Deploy the Visual Studio Database Project to the database "MyDatabase". Here the shorter "P" alias is used instead of the full "MsBuildParameters" parameter name. The shorter alias' of the MsBuild parameters are also used; "/t" instead of "/target", and "/p" instead of "/property".


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" -WhatIf

Returns the result object containing the same property values that would be created if the build was ran with the same parameters. The BuildSucceeded property will be $null since no build will actually be invoked. This will display all of the returned object's properties and their values.


Invoke-MsBuild -Path "C:\Some Folder\MyProject.csproj" > $null

Builds the given C# project, discarding the result object and not displaying its properties.

Full Documentation

For a full list of available parameters, check out the latest documentation in PowerShell by using Get-Help Invoke-MsBuild -Full, or just look at the file in source control here.

Changelog

See what's changed in the application over time by viewing the changelog.

Donate

Buy me some pancakes πŸ₯ž for providing this PowerShell module open source and for free :)

paypal

More Repositories

1

PathLengthChecker

Path Length Checker is a stand-alone app that returns the path and length of all files and directories in a given directory.
C#
244
star
2

AHKCommandPicker

An AutoHotkey (AHK) script that allows you to easily run AHK functions. Instead of having to remember what shortcut key maps to each of your AHK scripts, this displays a list of all your AHK functions and allows you to easily run them.
AutoHotkey
78
star
3

VS.DiffAllFiles

Visual Studio Extension to make comparing files before and after committing them to Git and TFS faster and easier.
C#
36
star
4

Sample.Serilog

Some sample projects showing how to use Serilog
C#
32
star
5

PowerShell.tiPS

PowerShell tips delivered straight to your terminal πŸ’»
PowerShell
24
star
6

MoveFilesIntoDateDirectories

Moves files from a directory into a new directory whose name is based on the file's date.
PowerShell
24
star
7

New-NuGetPackage

New-NuGetPackage.ps1 is a PowerShell script to make creating and publishing NuGet packages quick and easy, using a .nuspec or project file, from File Explorer or PowerShell.
PowerShell
21
star
8

DansUtilityLibraries

A collection of reusable code that I've written or found throughout my programming career.
PowerShell
12
star
9

PowerShellCmdletInCSharpExample

Example of how to build a PowerShell cmdlet in C# and unit test it
C#
11
star
10

DPSF-XNA

Create particle systems in XNA quickly and easily with Dynamic Particle System Framework for XNA.
C#
11
star
11

AzureDevOps.WindowsScheduledTasks

An Azure DevOps extension for installing and uninstalling Windows Scheduled Tasks
PowerShell
10
star
12

AutoUpdateProjectsMinimumRequiredClickOnceVersion

Automatically force your ClickOnce app to update itself without prompting the user; less obtrusive, and enhanced security by ensuring the latest version is used.
PowerShell
9
star
13

SqlScriptRunner

SQL Script Runner is a lightweight program that can be used to quickly run multiple SQL scripts against a database.
C#
8
star
14

NotifyWhenMicrosoftOutlookReminderWindowIsOpen

Make it more apparent that the Microsoft Outlook Reminder window has opened by receiving configurable alerts.
AutoHotkey
7
star
15

New-GitHubRelease

PowerShell module to make automating the creation of new GitHub releases easy.
PowerShell
6
star
16

AzureDevOps.Wait

An Azure DevOps extension for pausing a build or release flow for the specified amount of time
PowerShell
6
star
17

Set-ProjectFilesClickOnceVersion

PowerShell script to update a project file's (.csproj or .vbproj) ClickOnce version.
PowerShell
6
star
18

AzureArtifactsPowerShellModuleHelper

A PowerShell module with cmdlets to make using PowerShell modules in Azure Artifacts easier.
PowerShell
5
star
19

Oh-My-Posh.DeadlydogTheme

Custom theme called 'deadlydog' for oh-my-posh
4
star
20

GreasemonkeyScripts

A collection of Greasemonkey scripts that I've created.
JavaScript
3
star
21

CloseZoomWindowsAfterJoiningMeeting

Automatically close superflous windows that open every time you join a Zoom meeting
AutoHotkey
3
star
22

DPSF

Dynamic Particle System Framework
C#
2
star
23

Template.NewGitRepo

A template repository to use as a starting point for new git repos with basic boilerplate things that every git repo should have
2
star
24

deadlydog.github.io

Daniel Schroeder's Programming Blog - Sharing my tips, tricks, and code to help other developers be more productive. https://blog.danskingdom.com
HTML
2
star
25

GitHub.Experiment.SemanticVersionActions

Experimenting with various Semantic Version actions to see which I like best
2
star
26

PowerShell.Experiment.ClassInModule

An experimental repo to reproduce a PowerShell issue I am seeing when using a class in a module
PowerShell
1
star
27

DansOnePhotoFileGallery

A single PHP file that you can drop into your photos directory to create a gallery and allow you to view them online.
PHP
1
star
28

MoveAndTagMediaFiles

Quickly preview then copy, move, and tag images, videos, and audio files
C#
1
star
29

Presentation.DevContainersAndGitHubCodespaces

Presentation I gave about Dev Containers and GitHub Codespaces for a lunch and learn
1
star