如何 运行 或在 VSTS 中的托管代理上安装工具

How to run or install tool on hosted agent in VSTS

我想 运行 在 VSTS 上以管理员身份执行命令。

实际上我正在尝试在 VSTS 托管代理上使用巧克力工具管理器安装 git-tfs,所以我 运行 在 VSTS 命令行任务上执行以下命令:

@"%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"

或者,

或者 VSTS 还提供了安装的巧克力任务,您可以在屏幕截图中看到:

以上两种方法给出相同的错误:

[错误]System.Management.Automation.RuntimeException:将 Chocolatey 安装到默认文件夹需要管理权限。请从提升的提示中 运行。如果需要以非管理员身份安装,请参阅 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 了解详细信息和替代方法。

你通常不能。如果某事需要管理员访问权限,而您使用的是托管代理,则不能执行此操作。

对于您的具体问题,我首先会查看错误消息提供给您的资源:

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

我刚刚做了一个快速测试,内容如下:

  1. 使用 Hosted 2017 Build Agent 设置 VSTS 构建
  2. 添加了具有以下内容的 PowerShell 任务

    巧克力列表--local-only

  3. 运行 构建

此命令正确输出当前通过 Chocolatey 安装的 Chocolatey 软件包列表。

这告诉我 Chocolatey 已经安装在 Hosted 2017 Build Agent 上,因此您不需要再次安装它。相反,您应该能够使用它安装其他应用程序。

注意:您尝试安装的软件包仍将受到相同权限的约束。因此,如果您尝试安装需要管理权限的应用程序,那么您可能会 运行 遇到同样的问题。