azure-devopsyamlazure-pipelinesazure-powershellcicd

The term 'script:' is not recognized as the name of a cmdlet, function, script file, or operable program


I am running a build pipeline YAML file in Azure where I have a powershell script that I want to run but I am facing the following error:

script: : The term 'script:' 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. At D:_work_temp\32585f841520.ps1:7 char:3 script:

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

and the code is bellow:

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'
    }

The code should pass a list of configurations from an array to be Rebuilded. I did try to run it also in CmdLine but without success.


Solution

  • script: : The term 'script:' 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. At D:_work_temp\32585f841520.ps1:7 char:3 script:

    The direct cause is you put script task inside powershell task. The script keyword is read as a command but system cannot find it, then the error reported.

    In DevOps pipeline, it's not supported to include one task in another task.

    You are getting the folder name under Physical, in the 2nd PowerShell task, run location_of_build_agent.exe against the folders. Not sure what's location_of_build_agent.exe.

    Here is the sample for your reference, you can replace Write-host $item to your real command(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'