title: How to set datetime to value of variable using Powershell in VSTS builds?
body: <p><strong>The case</strong>: I have a project where I use nuget packages sharing small libraries between components. I use nuget packages produced by CI builds and once they are done I let the pull request go into master branch and this build will produce a new nuget package which will be used in production.</p>
  
  <p>Nuget package created by CI build has the following properties:</p>
  
  <ul>
  <li>Version number: <strong>X.Y.Z-CI-yyyyMMdd-HHmmss</strong></li>
  <li>Contains pdb files too</li>
  <li>Contains documentation xml file</li>
  </ul>
  
  <p>Nuget package create by Release build has the following properties:</p>
  
  <ul>
  <li>Version number: <strong>X.Y.Z</strong></li>
  <li>Contains documentation xml file</li>
  </ul>
  
  <p>VSTS build provides the option to use datetime in build name, so it will be unique always. The problem is here.</p>
  
  <ul>
  <li>VSTS build uses localtime (I'm in GMT+2)</li>
  <li>NugetCommand task of VSTS uses <a href="https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/NuGetCommand/Common/NuGetPackUtilities.ts" rel="nofollow noreferrer">only UTC</a> and <a href="https://github.com/Microsoft/vsts-tasks/issues/4754" rel="nofollow noreferrer">bug</a> <a href="https://github.com/Microsoft/vsts-tasks/issues/4752" rel="nofollow noreferrer">ticket</a> about it</li>
  </ul>
  
  <p>As a result I have a strangle looking consistency between my builds and nuget packages:</p>
  
  <ul>
  <li>Nuget package: <strong>1.0.0-CI-20170709-201010</strong></li>
  <li>Build: <strong>BuildName-BranchName-20170709-181010</strong></li>
  </ul>
  
  <p>Another problem NugetCommand and VSTS cooperation is that, they take the timestamp in different moment, so there is a difference between the two timestamp beside the two hours. It is not that big deal because I don't kick off builds on same build-branch in every second, but still...</p>
  
  <p>NugetCommand has the ability to read the version number of the package it will create from BuildNumber, which is the same as VSTS going to give the new artifact.</p>
  
  <p>So, I would like to solve this problem until they make it more consistent. I already reported to them a few requests. What I figured out so far is using PowerShell to populate a variable with timestamp and it will be used by VSTS build and NugetCommand task.</p>
  
  <p>I followed the example here but it deals with only strings and not date.</p>
  
  <p>I put together the following script, but it does not provide the result I would like to have.</p>
  
  <p><strong>Powershell task 1</strong>:</p>
  
  <pre><code>Write-Host "Setting up the date time for build variable and nuget package identifiers"
  Write-Host "##vso[task.setvariable variable=dateTimeIdentifier]Get-Date -format yyyyMMdd-Hmmss"
  Write-Host "Get-Date -format yyyyMMdd-Hmmss"
  Get-Date -format yyyyMMdd-Hmmss
  </code></pre>
  
  <p><strong>Powershell task 2</strong> where the argument is: </p>
  
  <blockquote>
    <p>-dateTimeIdentitfier"$(dateTimeIdentifier)"</p>
  </blockquote>
  
  <pre><code>Write-Host "Checking: $(dateTimeIdentifier)"
  </code></pre>
  
  <p>And the result is where you can see that the Powershell command is the value of the variable not the formatted datetime.</p>
  
  <pre><code>Setting up the date time for build variable and nuget package identifiers
  Get-Date -format yyyyMMdd-Hmmss
  20170709-202457
  Checking:  Get-Date -format yyyyMMdd-Hmmss
  </code></pre>
  
  <p>I know how to solve issues like this in bash, but I don't have build agent where bash is available.</p>
  
  <p>Bash like solution would like to be like this, which is not correct, but the command between `` will be executed first and the result will be passed through the remaining command as parameter.</p>
  
  <pre><code>Write-Host "##vso[task.setvariable variable=dateTimeIdentifier]`Get-Date -format yyyyMMdd-Hmmss`"
  </code></pre>
  
  <p>My question is, how to solve this in VSTS build and its task environment? Is there a solution for this? Is my approach correct?</p>