术语“脚本:”不被识别为 cmdlet、函数、脚本文件或可操作程序的名称

问题描述 投票:0回答:1

我正在 Azure 中运行构建管道 YAML 文件,其中我有一个想要运行的 powershell 脚本,但遇到以下错误:

script: :术语“script:”不被识别为 cmdlet、函数、脚本文件或可操作程序的名称。 检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 在 D:_work_temp�85f841520.ps1:7 字符:3 脚本:

CategoryInfo          : ObjectNotFound: (script::String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : CommandNotFoundException

代码如下:

steps:
- checkout: self
  submodules: true
- powershell: |
        $arr = Get-ChildItem '$(Build.SourcesDirectory)\Physical' |
        Where-Object {$_.PSIsContainer} |
        Foreach-Object {$_.Name}

            Write-Host "##vso[task.setvariable variable=arr;]$arr"
  displayName: 'Powershell Script'

- powershell: |
    $string = "$(arr)"
    $Data=$string.split(" ")
    foreach($item in $Data){
      script:
      C:\C:\location_of_build_agent.exe "$(Build.SourcesDirectory)\file_to_be_build" -c $item -buildMode "Rebuild"
      if (%errorlevel%==1) {exit 0} else {exit %errorlevel%}
      displayName: 'Run Build script for $item'
    }

代码应该从要重建的数组中传递配置列表。 我也尝试在 CmdLine 中运行它,但没有成功。

azure-devops yaml azure-pipelines azure-powershell cicd
1个回答
0
投票

script: :术语“script:”不被识别为 cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在 D:_work_temp�85f841520.ps1:7 char:3 脚本:

直接原因是你把

script
任务放在
powershell
任务里面。
script
关键字被读取为
command
但系统找不到它,则报告错误。

在 DevOps 管道中,支持将一项任务包含在另一项任务中。

您将在第二个 PowerShell 中的

folder name
下看到
Physical
任务,对文件夹运行
location_of_build_agent.exe
。不知道什么是
location_of_build_agent.exe

这里是示例供您参考,您可以将

Write-host $item
替换为您的真实命令(location_of_build_agent.exe...)。

- powershell: |
    $arr = Get-ChildItem '$(Build.SourcesDirectory)\Physical' |
    Where-Object {$_.PSIsContainer} |
    Foreach-Object {$_.Name}
    
    Write-Host $arr            # check the value
    Write-Host "##vso[task.setvariable variable=arr;]$arr"
  displayName: 'Powershell Script'

- powershell: |
    $string = "$(arr)"
    $Data=$string.split(" ")
    foreach($item in $Data){
      Write-host $item
    }
  displayName: 'Run Build script'
© www.soinside.com 2019 - 2024. All rights reserved.