首页 > 解决方案 > 如何在 azure devops 中调用另一个目录的 powershell 脚本

问题描述

我正在尝试从子目录中的另一个 powershell 脚本调用 powershell 脚本。目录结构如下:

main_script.ps1
./Test:
.  ..  test.ps1

所以我从 test.ps1 调用 main_script.ps1 如下:

#Call the main script
../main_script.ps1

当我从具有相同结构的云外壳执行此操作时,我可以成功调用 main_script.ps1 脚本。但在 azure devops 中,我遇到以下错误:

##[error]The term '../main_script.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

您对正确的路径有任何想法吗?azure devops 任务的默认路径是什么?我正在使用 Azure Devops Release 来创建管道。

标签: powershellazure-devopsazure-pipelines

解决方案


看起来 workingDirectory 只是附加到脚本名称而不更改根目录。您可以内联克服此调用脚本:

steps:
- task: PowerShell@2  # this doesn't work
  continueOnError: true
  inputs:
    targetType: 'filePath' # Optional. Options: filePath, inline
    filePath: 'test.ps1'
    workingDirectory:  '$(Build.SourcesDirectory)/stackoverflow/77-powershell/'

- task: PowerShell@2 # this works
  continueOnError: true
  inputs:
    targetType: 'inline' # Optional. Options: filePath, inline
    script: |
        cd '$(Build.SourcesDirectory)/stackoverflow/77-powershell/'
        ./test.ps1

推荐阅读