Monday 28 September 2015

Run PowerShell commands in MSBuild

There are many blogs and tutorials available on the internet for using PowerShell Commands in MSbuild Tasks. There are many ways to call the PowerShell Commands including Exec calls:

Picture 1
There is a major drawback with the above approach; whenever the command will be executed it will also render a lot of PowerShell module log on the command prompt and event log.

Picture 2

To overcome this problem I created a Task which suppresses the unwanted logs from being rendered on the Command Prompt Window.



Picture 3

Example Usage:




Picture 4

Above code runs a PowerShell Command using PowerShell.exe. If necessary you can hide the command from the log if it contains sensitive information by using the @HideCommand attribute.

Note that the all powershell errors, whether terminating or non-terminating command will cause the task to fail. Use $ErrorActionPreference to suppress errors or use the @ContinueOnError attribute to ignore any errors.



1 comment: