【问题标题】:PowerShell not recognizing build.ps1 file. How to define the path with out hardcoding itPowerShell 无法识别 build.ps1 文件。如何在不硬编码的情况下定义路径
【发布时间】:2020-08-17 20:02:13
【问题描述】:

我有一个简单的 yml 文件,用于在 Azure devops 中运行管道。 在 YML 中,我定义了 .build.ps1 文件路径,但我不断收到此错误

../build.ps1:术语“../build.ps1”未被识别为 cmdlet、函数、脚本文件或可操作的名称 程序。检查名称的拼写,如果包含路径,请验证路径是否正确并重试

这是我的 build.ps1 文件所在的位置 C:\Users\David.Remin\source\repos\branchName\FirstBranch\build.ps1

这是我在 yml 文件中定义的方式

powershell: ../build.ps1

我不想硬编码文件路径。因为每次都会有所不同。我只想提一下文件名

#This is the YML file

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- powershell: .\build.ps1 -target=Push -nugetUsername='$(nugetUsername)' -nugetPassword='$(nugetPassword)'

【问题讨论】:

  • 没有看到代码这是相当困难的。如果您在 powershell 脚本中,为了调用外部 powershell 脚本,您必须像对待任何其他外部脚本一样对待它并使用Start-process。但是,为了便于阅读,它通常写为& .\build.ps1'。这有帮助吗?
  • 哪个代码.. YML 文件?我已将其更改为 .buildps1,但出现相同的错误 '.\build.ps1' 一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。
  • 你能发布你的 YML 代码吗?有几种方法可以定义这一点,这将有助于了解您目前拥有什么。
  • 确定我发布了
  • 你一直在拼写不同的构建文件的名字,你能写出它的确切名字吗?例如,它在 ps1 前面有一个点吗? (你的评论表明它没有),它是否在开头有一个点(你的问题说两者,你的评论说是)。基本上,哪一个是正确的? .buildps1, .build.ps1, build.ps1 ?

标签: powershell azure-devops


【解决方案1】:

最简单的方法是将您的 build.ps1 文件推送到 Azure devops 存储库。

如果要运行本地构建 .ps1 文件,首先需要指定 private agent pool 而不是托管代理池。其次,需要在yaml中指定powershell任务的workingDirectory参数,否则默认为$(Build.SourcesDirectory)

workingDirectory:指定要在其中运行命令的工作目录。如果将其留空,则工作目录为 $(Build.SourcesDirectory)。详情请参考此document

您可以参考以下示例脚本:

pool: Default
       
jobs:
- job: Build   

  steps:
   - task: PowerShell@2
     inputs:
          filePath: 'build.ps1'
          workingDirectory: 'C:\Users\David.Remin\source\repos\branchName\FirstBranch'
          arguments: '-target=Push -nugetUsername='$(nugetUsername)' -nugetPassword='$(nugetPassword)'

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2012-08-14
    • 2017-05-25
    • 2010-11-23
    相关资源
    最近更新 更多