powerhsell script which calls another script with function arguments in azure devops

Thangavel Daniel Tamilarasan 271 Reputation points
2021-05-21T15:35:22.457+00:00

I have two scripts as below
Script1.ps1:

function getkey ($param1, $param2)
{
return $result
}

trying to call the Script 1 in mainscript , but the below argument doesn't work.
mainscript.ps1:

$result = Invoke-Expression "$FileDir\script1.ps1 getkey -param1 'Value1' -SecretName 'Value2'"

I would like to get the value of $result from Script1. But this doesn't work , please help.

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. BhargaviAnnadevara-MSFT 5,466 Reputation points
    2021-05-24T09:52:47.437+00:00

    @Thangavel Daniel Tamilarasan In order to invoke script1.ps1 from mainscript.ps1, you would have to source it first.

    This would need you to make some modifications to your mainscript.ps1 script as follows:

       Invoke-Expression ". $env:System_DefaultWorkingDirectory\script1.ps1"  
       $result = Invoke-Expression "getkey -param1 'Value1' -param2 'Value2'"  
       Write-Host "$result"  
    

    script1.ps1:

       function getkey ($param1, $param2)  
       {  
           # Compute $result  
           return $result  
       }  
    

    You will also notice that I've used the predefined system variable System.DefaultWorkingDirectory to reference the local path on the agent where your source code files are downloaded.

    Then, you could use the built-in PowerShell task if you want to invoke mainscript.ps1 from your Azure DevOps pipeline.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.