Over the past few months, I have had a few customer engagements where there there has been a need for conditional release tasks in VSTS.  What is a conditional release statement you ask?  What if you want to run a task during your release but not every time you release?

This is a functionality that is already available on the build side of the house of VSTS.


Once case in particular comes to mind….

A customer had started to use a nuget package that brought in some files that get locked as in use as long as the website is running in IIS.  The easy answer in Azure PaaS, is to utilize slots on the App Service plan to allow for releasing updated code to each slot and then swapping the slots once the new code is up and running.  Here is some good documentation on this topic:


On the Infrastructure as a Service side however this is not always as easy.  It becomes even more troublesome when you are dealing with a single web server which is not behind a load-balancer.  That all adds up to that word that no one likes to hear, down time deployments. Now one way is to solve this is to stop the website, copy up the files and then start the website every time the website is released.  What if the files that are locked in use; however, do not actually need to be updated?

This would be a perfect situation for the aforementioned conditional release tasks, but as i said before that’s not really a thing yet… so what is one to do?  Well why not use the swiss army knife of the Microsoft world…. PowerShell to the rescue.

Let’s use an inline PowerShell script to check the file version of the .DLL that is in use by IIS against the version of the file that is in the deployment artifacts.  This means that if the versions are the same then you go on your merry way and the site just keeps on keeping on.  If however the versions are different, then let’s stop the website in order to unlock the files and only then update the files.

$vstsPath = $(System.Artifacts.Directory)
$path1 = $vstsPath + "ADDS CD Build - Staging/drop/Adds/bin/SqlServerTypes/x64"
$DestServer = $(StagingServer)
$path2 = "\\" + $DestServer + "\C$\inetpub\wwwroot\AAC Web Vendor\bin\SqlServerTypes\x64"
$SourceFiles = Get-ChildItem $path1 -Include *.dll -Recurse
foreach($File in $SourceFiles)
$Path3 = $Path2 + "\" + $File.Name
$DestFile = Get-ChildItem $Path3
if($File.VersionInfo.ProductVersion -eq $DestFile.VersionInfo.ProductVersion)
{$True; Write-host "Files are the same";
Invoke-Command -ComputerName $DestServer -ScriptBlock { appcmd stop apppool /apppool.name:Adds_Staging} -Verbose
Copy-Item -Path $SourceFiles -Destination $path2 -Recurse -Force -Verbose
Invoke-Command -ComputerName $DestServer -ScriptBlock { appcmd start apppool /apppool.name:Adds_Staging} -Verbose


If you have any questions about this or any other DevOps topics. Please feel free to reach out to us.