0

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:

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

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

function printHello()
{
Write-Host 'Hello from 2nd File.'
}

BUT, when when pipelines triggers, its run the Helloworld1.ps1 and print its 2nd line and then failed on 3rd line throwing the error:

 | The term 'PrintHello' 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.
2
  • 3
    The Call Operator (e.g. & .\somescript.ps1) runs a script in a child scope which means that any variables and functions defined inside the script are only available inside that script. If you want functions to still be available after the child script has finished you can use the Dot Sourcing Operator (e.g. . .\somescript.ps1) instead - see learn.microsoft.com/en-us/powershell/module/… Commented May 31, 2022 at 13:30
  • @mclayton you should post that as an Answer. I'd vote for it. Commented Jun 3, 2022 at 12:08

2 Answers 2

1

This document will tell you how to do it:

https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/10-script-modules?view=powershell-7.2#dot-sourcing-functions

And below scripts works on my side:

My structure:

enter image description here

YAML file:

pool:
  name: Azure Pipelines
steps:
- task: PowerShell@2
  displayName: 'PowerShell Script'
  inputs:
    targetType: filePath
    filePath: './p_main.ps1'

main PowerShell script file:

# This is p_main.ps1
. $env:System_DefaultWorkingDirectory\p_sub.ps1
Get-ChildItem -Path Function:\printHello
Write-Host 'Hello from 1st File.'
printHello

How to Use predefined variables:

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken

Another PowerShell script file:

# This is p_sub.ps1
function printHello()
{
Write-Host 'Hello from 2nd File.'
}

Works well:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use the $MyInvocation variable to get the current path of the script running. The MyCommand.Path returns the full path of the script running (C:\path\helloworld1.ps1). Use Split-Path to remove the filename and just get the path.

Add the following to the helloworld1.ps1 file:

$cd = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
. "$cd\helloworld2.ps1"

The advantage of doing it this way is that you aren't dependent of AzDO environment variables. The script works no matter if you run it from a AzDO pipeline, or your local dev PS terminal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.