You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell任务替换Tenants.json时出现路径错误求助

问题描述

执行Azure DevOps PowerShell任务替换Tenants.json文件时,出现如下错误:

##[error]Invalid file path 'C:__w\1\s'. A path to a .ps1 file is required.

对应的YAML配置:

- task: PowerShell@2
  displayName: Override Tenants.json file
  inputs:
    scriptType: inlineScript
    inlineScript: >-
      $json = @'
      $(Deployment.tenantContent)
      '@;
      $json | Out-File -encoding "UTF8" "$(Deployment.FilePath)"

已知信息:

  • Tenants.json的完整路径:C:\__w\1\s\bapiweb-PowerBI\code\powerbi\PowerBi.Web\App_Data\tenants.json
  • 变量Deployment.FilePath的值:web-PBI\code\pobi\PoBi.Web\App_Data\tenants.json
问题分析
  1. 路径不匹配:FilePath的相对路径前缀web-PBI与实际完整路径的前缀bapiweb-PowerBI不一致,导致拼接后的路径无效,任务误判为缺少.ps1文件路径。
  2. 内联脚本的路径拼接方式不够严谨,手动拼接可能引发路径解析异常,触发任务的参数校验错误。
解决方案

1. 修正相对路径变量

将Deployment.FilePath的值修改为与完整路径匹配的相对路径:

bapiweb-PowerBI\code\powerbi\PowerBi.Web\App_Data\tenants.json

2. 优化PowerShell任务脚本

使用Join-Path安全拼接路径,结合代理工作目录环境变量$env:BUILD_SOURCESDIRECTORY确保路径正确:

- task: PowerShell@2
  displayName: Override Tenants.json file
  inputs:
    scriptType: inlineScript
    inlineScript: |
      # 获取传入的JSON内容
      $jsonContent = @"
      $(Deployment.tenantContent)
      "@
      # 拼接完整目标路径
      $targetFilePath = Join-Path -Path "$env:BUILD_SOURCESDIRECTORY" -ChildPath "$(Deployment.FilePath)"
      # 写入文件
      $jsonContent | Out-File -Path $targetFilePath -Encoding UTF8

3. 确认任务参数正确性

确保scriptType参数拼写正确为inlineScript,避免任务误将输入识别为脚本文件路径需求。

内容的提问来源于stack exchange,提问作者Duk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:07:15