5

I want to run cmd as an administrator on VSTS.

Actaully I am trying to install git-tfs with chocolatey tool manager on VSTS hosted agent, So I am running the following command on VSTS command line task:

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

or,

Alternatively VSTS also provides the chocolatey task for installation, you can see this in the screenshot :

enter image description here

Both of the above approaches giving the same error:

[error]System.Management.Automation.RuntimeException: Installation of Chocolatey to default folder requires Administrative permissions. Please run from elevated prompt. Please see https://chocolatey.org/install for details and alternatives if needing to install as a non-administrator. ---> System.Management.Automation.RuntimeException: Installation of Chocolatey to default folder requires Administrative permissions. Please run from elevated prompt. Please see https://chocolatey.org/install for details and alternatives if needing to install as a non-administrator.

Twinkle
  • 394
  • 4
  • 17

2 Answers2

3

I have just done a quick test which was the following:

  1. Set up a VSTS Build using Hosted 2017 Build Agent
  2. Added PowerShell Task with the following contents

    choco list --local-only

  3. Ran the build

This command correctly output the list of Chocolatey Packages that are currently installed via Chocolatey.

This tells me that Chocolatey is already installed on the Hosted 2017 Build Agent, and as a result, you shouldn't need to install it again. Instead, you should be able to install additional applications using it.

NOTE: The packages that you try to install will still be subject to the same permissions though. So if you are trying to install an application that requires administrative permissions, then you will likely run into the same problems.

Gary Ewan Park
  • 17,610
  • 5
  • 42
  • 60
  • After successfully installing the tool git-tfs. the command is not recognized as internale or external command. i have also set the PATH=C:/tools/gittfs:$PATH. Any Idea? – Twinkle Mar 19 '18 at 12:48
  • Are you trying to do this within the same PowerShell/Command session? If so, those modifications likely won't take effect until after you start a new session. – Gary Ewan Park Mar 19 '18 at 13:56
  • As a side note.. according to this: https://learn.microsoft.com/en-us/vsts/build-release/concepts/agents/hosted#software is already installed on the hosted agent – Gary Ewan Park Mar 19 '18 at 14:06
  • 1
    Update, aside from the fact that chocolatey is already installed, you now are admin when running tasks on a hosted VS agent. – Bernard Vander Beken Mar 20 '18 at 09:24
  • @BernardVanderBeken Do you have a reference to documentation that states where this is the case? Thanks! – Gary Ewan Park Mar 20 '18 at 09:42
  • 1
    @chris-patterson mentions this at https://github.com/Microsoft/vsts-image-generation/issues/54#issuecomment-363294441 – Bernard Vander Beken Mar 20 '18 at 10:17
  • @GaryEwanPark:I am using the git-tfs in another command task of hosted agent after the installation. Do I need to do the same on same task? – Twinkle Mar 21 '18 at 05:29
2

You generally can't. If something requires admin access and you're using the hosted agent, you can't do that thing.

For your specific problem, I'd start by looking at this resource, which the error message gave to you:

"Please see https://chocolatey.org/install for details and alternatives if needing to install as a non-administrator."

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • I tried the non-admin script too. It is giving the below error: Set-ExecutionPolicy : Access to the registry key .2950386Z 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser". At D:\a\_temp\be447637-8899-4496-bf7b-2362b2cf30c7.ps1:10 char:1 2018-03-16T07:12:01.2952386Z + Set-ExecutionPolicy Bypass – Twinkle Mar 16 '18 at 07:19
  • Update, aside from the fact that chocolatey is already installed, you now are admin when running tasks on a hosted VS agent. – Bernard Vander Beken Mar 20 '18 at 09:24