Get (more) Build Agent Details with PowerShell

Chance are you are already aware that you can use the system.debug variable with your builds to get diagnostics-level information (i.e. verbose) when running your builds. If this is news to you, then try setting system.debug to true in the Variables tab for one of your build definitions and run the build.

image

 

After running the build, check the build log and you will notice a lot of extra information that wasn’t there before. This is great for getting a better understanding of what is going on behind the scenes when your build runs. However, keep in mind that some build tasks provide more (or less) information than others. In some cases you might want to log additional information on your own.

Enter PowerShell

PowerShell can be used to provide all sorts of information about the environment you are running on. Here are a couple examples of how you might make use of PowerShell scripts in your build definitions.

 

List Environment Variables

1. Add the PowerShell task to your build.

image

 

2. Configure the task properties as shown below making sure you set the Type to Inline Script.

image

 

If($env:SYSTEM_DEBUG -eq “true”)
{
(Get-ChildItem Env:).GetEnumerator() | % {
Write-Host (“{0} = {1}” -f $_.key, $_.value) }
}

 

3. Ensure system.debug is set to true (because we are checking for that in our example PowerShell script shown above) and run the build. Once the build is completed, click on the PowerShell section of the log to view the results.

image

 

In the above screenshot you can see a portion of the environment variables listed along with the values as defined on the build agent this particular build was executed on.

 

List Files on the Build Agent

Another example of a script that can be useful is one to list files on the build agent. Recently, I was making use of a 3rd party build task that creates a ZIP file (more on that in a future post) but I couldn’t figure out how it was naming the resulting ZIP file. With a little bit of PowerShell scripting, I was able to see all the files in a particular folder and the problem was solved.

The steps are the same as above but the PowerShell is (obviously) different. There is also one other minor difference. This time, we are passing a parameter into the PowerShell task so we can specify the folder we want to list based on a build variable.

 

You can see in the screenshot below that we are passing in the variable $(Build.SourcesDirectory).

image

Param(
[string]$filePath
)

If($env:SYSTEM_DEBUG -eq “true”)
{
dir $filePath -r  | % { if ($_.PsIsContainer) { $_.FullName + “\” } else { $_.FullName } }
}

These are only a couple of examples of how you might expose information via PowerShell. You are likely thinking of other things you might make use of PowerShell for right now. If so, please share your thoughts with everyone in the comments section below!

 

 

 

 

 

Related Posts