Search code examples
batch-filetfsbuild-definition

TFS Run batch script on a remote server with admin permission


I currently have Server A which is where my TFS and Build Agent is located. I then have Server B which is when my source code site. I am trying to set up a build definition and copies file from on location in server B to another and then build the solution.

However when I run this batch file as part of a build definition it is not creating folders where it need to be. I believe due to the agent not having correct permissions.

Is there a way to run the following batch script to run with Admin permission from a build definition.

enter image description here


Solution

  • You can try below workarounds:

    1. Convert the batch script to PowerShell script, then copy the PowerShell script to target machine and use the PowerShell on Target Machines task to run the script. You can enter your admin user and password using the task. Reference below screenshot.
    2. Add a PowerShell task and run below script to call the cmd.exe to run the batch script with an admin user account on target machine (Copy the batch script to target machine first, in below sample I copied the batch script to C:\Scripts\Test.bat):

      Param(
        [string]$computerName = "v-tinmo-12r2",
      )
      $Username = "Domain\user"
      $Password = ConvertTo-SecureString "password-here" -AsPlainText -Force
      
      $cred = New-Object System.Management.Automation.PSCredential($Username,$password)
      
      Invoke-Command -ComputerName $computerName  -Credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Scripts\Test.bat'"}
      

    enter image description here