title: Azure DevOps Build Pipeline - Unable to access $(Build.BuildNumber) in Script
body: <p>In my build pipeline I am doing the following:</p>
  
  <ol>
  <li>Restore from Git</li>
  <li>Powershell Script - to retrieve the Build
  number and write that to a json file BEFORE.... </li>
  <li>Build solution </li>
  <li>Archive Files</li>
  <li>Publish Artifact.</li>
  </ol>
  
  <p>In step 2 the Powershell script is pretty simple:</p>
  
  <p>DEFINED ENV VARIABLES:</p>
  
  <pre><code>Name: buildNumber  Value: $(Build.BuildNumber)
  Name: rootPath Value:$(Build.ArtifactStagingDirectory)
  </code></pre>
  
  <p>CODE:</p>
  
  <pre><code>$theFile = Get-ChildItem -Path $rootPath -Recurse -Filter "host.json" | Select-Object -First 1
  $propertyName = "BuildNumber"
  
  if($theFile)
  {
      $json = Get-Content "$theFile" | Out-String | ConvertFrom-Json
      if($json.$propertyName)
      { 
          $json.$propertyName = $buildNumber
      }else{    
          Add-Member -InputObject $json -MemberType NoteProperty -Name $propertyName -Value $buildNumber
      }
      $json | ConvertTo-Json -depth 100 | Out-File "$theFile"
  
  }
  else
  {
      Write-Warning "Found no files."
  }
  </code></pre>
  
  <p>For some reason my $buildNumber is coming back null.  The $rootPath is working.
  Am I not able to access the $(Build.BuildNumber) outside the build step?  The build number format is defined in the Options for the Pipeline and it works fine when stamping the build, but I am unable to access it in my powershell script.</p>
  
  <p>Any Thoughts?</p>