Azure DevOps流水线运行Powershell脚本报ENOENT文件不存在错误如何解决
问题根因
你的报错是因为CopyFiles@2任务未指定源目录,默认没有将$(Build.SourcesDirectory)(对应你提到的s目录)下的脚本文件复制到$(Build.ArtifactStagingDirectory)(对应a目录),导致发布的构件中缺少目标ps1文件,下载后找不到路径。
方案1:修复现有发布+下载构件的流程
你需要调整CopyFiles@2任务的配置,明确指定源路径和要复制的文件内容,修正后的完整流水线配置如下:
steps: # 先执行代码检出 - checkout: self - checkout: IAC - task: CopyFiles@2 inputs: # 新增:指定源目录为代码检出的s目录 SourceFolder: '$(Build.SourcesDirectory)' # 新增:指定要复制的文件,可根据实际需求调整过滤规则 Contents: 'repo1/folder1/Powershell-newscript.ps1' targetFolder: '$(Build.ArtifactStagingDirectory)' - task: PublishBuildArtifacts@1 displayName: 'Building Artifact now' inputs: pathToPublish: '$(Build.ArtifactStagingDirectory)' artifactName: 'drop-self' - task: DownloadBuildArtifacts@0 inputs: artifactName: 'drop-self' downloadPath: '$(System.ArtifactsDirectory)' - task: AzurePowerShell@5 inputs: azureSubscription: ${{parameters.subName}} ScriptType: 'FilePath' ScriptPath: '$(System.ArtifactsDirectory)/repo1/folder1/Powershell-newscript.ps1' azurePowerShellVersion: 'LatestVersion'
修改说明:
- 修正了原配置中
checkout与steps平级的缩进错误 - 给CopyFiles任务新增
SourceFolder参数指定为代码检出的s目录,新增Contents参数明确要复制的目标脚本
方案2:简化流程(仅适用同一条流水线内调用脚本的场景)
如果你的脚本不需要跨流水线使用,可以直接跳过发布、下载构件的步骤,直接调用s目录下的脚本,调整后的配置如下:
steps: - checkout: self - checkout: IAC - task: AzurePowerShell@5 inputs: azureSubscription: ${{parameters.subName}} ScriptType: 'FilePath' # 直接指向源目录下的脚本路径 ScriptPath: '$(Build.SourcesDirectory)/repo1/folder1/Powershell-newscript.ps1' azurePowerShellVersion: 'LatestVersion'
这种方案无需额外复制移动文件,执行效率更高。
内容的提问来源于stack exchange,提问作者kiran jan
相关产品推荐
相关产品推荐

