问题
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 :
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.
回答1:
I have just done a quick test which was the following:
- Set up a VSTS Build using Hosted 2017 Build Agent
Added PowerShell Task with the following contents
choco list --local-only
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.
回答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."
来源:https://stackoverflow.com/questions/49294430/how-to-run-or-install-tool-on-hosted-agent-in-vsts