Search code examples
powershellazure-devopsazure-pipelinesdevopsazure-powershell

Got an error when run a powershell script inside a powershell script in Azure pipeline


I have a pipeline task which is as follow:

- task: PowerShell@2
displayName: 'Script1'      
inputs:
  filePath: '$(System.DefaultWorkingDirectory)/Terraform/helloworld1.ps1'

And it run a helloworld1.ps1 , but the helloworld1.ps1 script call another script. here's the code of helloworld1:

Write-Host 'Hello from 1st File.'

. ./helloworld2.ps1

And the helloworld2.ps1 scripts contains this one line only:

Write-Host 'Hello from 2nd File.'

BUT, when when pipelines triggers, its run the script1 and print its first line and then failed on 2nd line throwing the error:

 | The term './helloworld2.ps1' is not recognized as a name of a
 | cmdlet, function, script file, or executable program. Check
 | the spelling of the name, or if a path was included, verify
 | that the path is correct and try again.

Solution

  • Got the answer of my own question. After using PSScriptRoot, the issue is resolved! so the correct helloworld1.ps1 is as follow:

     Write-Host 'Hello from 1st File.'
    
     & "$PSScriptRoot/helloworld2.ps1"