1
votes

I am trying to call a powershell script from another powershell script which is in a sub-directory. Directory structure is as below:

main_script.ps1
./Test:
.  ..  test.ps1

So i am calling main_script.ps1 from the test.ps1 as below:

#Call the main script
../main_script.ps1

When i am doing that from the cloud shell with the same structure, i can call the main_script.ps1 script succesfully. But in azure devops i am having the error below:

##[error]The term '../main_script.ps1' 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.

Do you have any idea about the correct path? What is the default path of azure devops task? I am using Azure Devops Release to create the pipeline.

2
check the "working directory" of the powershell script taskShayki Abramczyk
You should specify the folder where test.ps1 exists in the working directory of the powershell script task. That because the default work folder is System.DefaultWorkingDirectory docs.microsoft.com/en-us/azure/devops/pipelines/build/…Leo Liu-MSFT

2 Answers

1
votes

Do you have any idea about the correct path? What is the default path of azure devops task?

Since you are Azure Devops Release to create the pipeline, the default path of azure devops task should be System.DefaultWorkingDirectory, C:\agent\_work\r1\a.

However, our powershell scripts should locate under the path:

System.DefaultWorkingDirectory\_System.TeamProject

So, to invoke the main_script.ps1 in the test.ps1, we could use following scripts to achieve it:

Param(
 [String]$Workfolder,
 [String]$TeamProject
    )

echo $Workfolder

Invoke-Expression "$Workfolder\_$TeamProject\main_script.ps1"

Note: Do not ignore the short underscore _.

The important things is that pass the parameters System.DefaultWorkingDirectory and System.TeamProject to the main_script.ps1 in the test.ps1:

My powershell task:

enter image description here

Then the test result:

enter image description here

1
votes

It looks that workingDirectory is just appended to a script name without changing root directory. You can overcome this calling script inline:

steps:
- task: PowerShell@2  # this doesn't work
  continueOnError: true
  inputs:
    targetType: 'filePath' # Optional. Options: filePath, inline
    filePath: 'test.ps1'
    workingDirectory:  '$(Build.SourcesDirectory)/stackoverflow/77-powershell/'

- task: PowerShell@2 # this works
  continueOnError: true
  inputs:
    targetType: 'inline' # Optional. Options: filePath, inline
    script: |
        cd '$(Build.SourcesDirectory)/stackoverflow/77-powershell/'
        ./test.ps1