首页 > 解决方案 > Azure Devops - PS 脚本

问题描述

我试图通过 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

标签: powershellazure-devops

解决方案


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

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

trigger:
  master

pool:
  name: 'Dev1'

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

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

参考:使用预定义变量


推荐阅读