【问题标题】:Azure Devops - PS ScriptsAzure Devops - PS 脚本
【发布时间】:2020-12-05 06:37:00
【问题描述】:

我试图通过 Azure devops Pipeline 在 PS Inline 脚本下运行。但是我在代理日志上遇到错误 管道代码:

trigger:

master
pool:
name: 'Dev1'

steps:

task: PowerShell@2
inputs:
targetType: 'inline'
script: |
    # Write your PowerShell commands here.

    New-Item -Path "C:\Manoj" -Force

代理错误:

Starting: PowerShell
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.165.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
Generating script.
========================== Starting Command Output ===========================
"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\tmp\vsts-agent-win-x64-2.165.2_work_temp\65dd1488-c132-4b9a-8403-0604d37f43a4.ps1'"
New-Item : Access to the path 'C:\Manoj' is denied.
At C:\tmp\vsts-agent-win-x64-2.165.2_work_temp\65dd1488-c132-4b9a-8403-0604d37f43a4.ps1:4 char:1

New-Item -Path "C:\Manoj" -Force
  + CategoryInfo          : PermissionDenied: (C:\Manoj:String) [New-Item], UnauthorizedAccessException
  + FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

##[error]PowerShell exited with code '1'.
Finishing: PowerShell

【问题讨论】:

  • 该错误表明存在访问问题。您是否朝那个方向进行了调查?

标签: powershell azure-devops


【解决方案1】:

使用 Microsoft 托管代理,代码对我有用。

trigger:
  branches:
    include:
      - '*'

pool:
  vmImage: 'windows-latest'

steps:
- task: PowerShell@2
  inputs:
    targetType: inline
    script: |
        New-Item -Path "C:\Manoj" -Force

如果您使用的是自托管代理,请验证运行 azure devops 服务的用户帐户是否满足您的权限要求。

【讨论】:

    【解决方案2】:

    错误显示运行 azdo 代理的帐户无权在系统驱动器的根目录中创建对象。这种限制听起来合乎逻辑,因为代理应该在多用户/多项目场景中服务。

    因此,请考虑构建仅在工作目录范围内的管道逻辑。 例如:Pipeline.Workspace

    trigger:
      master
    
    pool:
      name: 'Dev1'
    
    steps:
    - task: PowerShell@2
      inputs:
        targetType: inline
        script: |
            New-Item -Path "$(Pipeline.Workspace)\Manoj" -Force
    

    在这种情况下,不同管道的工件相互隔离。

    参考:Use predefined variables

    【讨论】:

      【解决方案3】:

      如果您使用的是自托管代理,请尝试为代理服务用户添加管理员(或至少磁盘写入器)权限,或允许其为文件夹写入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-11
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多