-1

I am running one batch script while running the VSTS build definition. In that batch script I have maven path which is taken from the hosted agent capabilities section. I don't want that path to be hard coded in that batch file. I want to call instead that path using an environmental variable in the script while running the build definition. The path I have taken from hosted agent capabilities section. Below is the batch script.

Using batch script task in VSTS I am calling the below abc.bat file in VSTS build definition.

Batch script:

abc.bat:

call C:\java\maven\apache-maven-3.2.2\bin\mvn.bat install:install-file -Dfile=DevOps/proj_Dep_libs/application-agent-1.0.3.jar -DgroupId=application-agent -DpomFile=DevOps/Pss_Dep_libs/application-agent-1.0.3.pom -DartifactId=application-agent -Dversion=1.0.3 -Dpackaging=jar

Please help me on how to pass the path as a variable in the batch script while running the VSTS build definition.

PDBR
  • 331
  • 1
  • 7
  • 17
  • Why don't you just use the Maven build task? – Daniel Mann Jun 01 '18 at 12:55
  • @DanielMann,I want to run batch script which is having the lot of jars to be installed in VSTS local repo for running the pom.xml dependencies.In my organization will not accept VSTS proxy so what I am doing is I am calling those specific jars using batch script and installing those jars into VSTS local repo .So I am running the batch script before maven task.I can't install many jars using the maven task option in VSTS that's why I am running the batch script which will install the required jars into VSTS local repo so that my pom will run without any issues. – PDBR Jun 01 '18 at 13:04
  • Simply I want to replace that maven path in batch script with environmental variable which is available in hosted agent capabilities section.Please suggest me how can I use agent variables in that batch script instead calling maven path. – PDBR Jun 01 '18 at 13:06

1 Answers1

0

You can retrieve maven path from the hosted agent capabilities section, then create the variable using the Logging Commands. Then you can use the variable in batch script instead calling maven path.

  1. Create a PowerShell script to set the avariables (Reference below sample, you can also Use the OAuth token to access the REST API), then check in the script into VSTS.

  2. Add a PowerShell task in your definition to run the PS script

3.Use the variable in steps which behind the set variable step

$collectionurl = "https://xxx.visualstudio.com"
$user = "username"
$token = "password"

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$baseUrl = "$collectionurl/_apis/distributedtask/pools/2/agents/1?includeCapabilities=true"          
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

#Retrieve values 
$maven = $response.systemCapabilities.maven

#Set variable
Write-Host "##vso[task.setvariable variable=maven]$maven"

#Then you can use the variable in the next step: $(maven) in TFS, $env:maven in PowerShell, %maven% in batch script.

UPDATE:

Well, you can use PAT without the username with below script (If you don't want to hardcode the token in the script, then you can create a secret variable and set the token as the value of the variable, then use the variable in script):

$PAT = "nvkoa6qrdrtrxweruiodfiamwd3ya2dkt7r6cx3xxxxw5pyxxxxq" 

# Base64-encodes the Personal Access Token (PAT) appropriately 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT))) 
$baseUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)_apis/distributedtask/pools/2/agents/1?includeCapabilities=true"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 

#Retrieve values 
$maven = $response.systemCapabilities.maven 
Write-Host "##vso[task.setvariable variable=maven]$maven"

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • ,As you said, Before running the batch script I have added powershell task in VSTS to run the above script.But In the above script what is the user name? Is it my VSTS login email ID or just name and also confirm token means PAT or my personal password which can be used to access VSTS ?.Please tell me. – PDBR Jun 12 '18 at 05:41
  • @PDBR You can use the email ID ( `xxx@xxx.com `) as the username, for the password you can use the personal password (login password to VSTS) or PAT, both working. – Andy Li-MSFT Jun 12 '18 at 06:03
  • In future if my user name is revoked/changed then how to make sure this script can be run? – PDBR Jun 12 '18 at 07:22
  • @PDBR You can create and use variables in build definition for the username and password. Then you just need to update the value of the variables once they changed. Alternately you can try to [Use the OAuth token to access the REST API](https://learn.microsoft.com/zh-cn/vsts/pipelines/scripts/powershell?view=vsts#use-the-oauth-token-to-access-the-rest-api) to enable your script to use the build process OAuth token, – Andy Li-MSFT Jun 12 '18 at 07:33
  • Thanks for the solution.Can we access the %maven% variable without user name as we are using token and collection url.Why we need user name.Without user name can we access? – PDBR Jun 20 '18 at 12:08
  • I tried by removing user name but its failing with the below error. 2018-06-21T06:02:42.8179191Z ##[error]Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.. At D:\a\1\s\DevOps\Build_Scripts\Read_Agent_Capabilities.ps1:7 char:1 + $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.Ge ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ({0}:{1}:String) [], RuntimeException + FullyQualifiedErrorId : FormatError – PDBR Jun 21 '18 at 06:14
  • Can you please edit the script without user name? So that I will use the above script.I have checked the OAuth token option and tried the below script by removing the index still its throwing error.Please help me out. – PDBR Jun 21 '18 at 06:15
  • param( [string]$collectionurl, [string]$user, [string]$PAT ) # Base64-encodes the Personal Access Token (PAT) appropriately $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}" -f $user,$token))) $baseUrl = "$collectionurl/_apis/distributedtask/pools/2/agents/1?includeCapabilities=true" $response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) #Retrieve values $maven = $response.systemCapabilities.maven Write-Host "##vso[task.setvariable variable=maven]$maven" – PDBR Jun 21 '18 at 06:16
  • Please check whether the above script is modified properly by removing user name and index value ({1}) or else kindly edit my script. – PDBR Jun 21 '18 at 06:17
  • @PDBR Well, please see the updated answer using PAT without username, you can use the script directly, it works on my side. – Andy Li-MSFT Jun 21 '18 at 07:17
  • Thanks a lot Andy..Much appreciating for your answer and response.I am accepting this answer. – PDBR Jun 21 '18 at 08:05
  • ,Can you able to help me out on this issue as well ?:https://stackoverflow.com/questions/50948036/how-to-disable-or-stop-and-enable-the-scheduler-jobs-in-azure-from-vsts-powershe?noredirect=1#comment88898162_50948036 – PDBR Jun 21 '18 at 08:05
  • @PDBR OK, if that works for you, please vote it up and [Accept it as an Answer (Mark the answer with ✔)](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), This can be beneficial to other community members reading this thread. – Andy Li-MSFT Jun 21 '18 at 08:15
  • Can you able to help me out on this issue as well ?:https://stackoverflow.com/questions/50948036/how-to-disable-or-stop-and-enable-the-scheduler-jobs-in-azure-from-vsts-powershe/50963615#50963615 – PDBR Jun 21 '18 at 12:55