0
votes

I am working on script where I want to take result from first script and run second script accordingly.

This is my first script -

$result = <command 1>

if($result)

{

< run command 2>

return $true

}

else {

return $false

}

Here is second script

if($return -eq $true)

{

<run Command 3>

}

else{

<run command 4>

}

I have 2 separate tasks in Azure Devops for performing these 2 scripts .

I am using Azure PowerShell task in my pipeline and output variable - return

Que - first script works good. Its returning true or false value but second script is not working. its just preforming else conditions whether return value from first script true or false. How can I make second script work according to true false result returned from first script

2
Hi @megha. Could you please check if the answers could solve this issue? Feel free to let me know if you have any questions. Just a remind of this. - Kevin Lu-MSFT

2 Answers

0
votes

If these are truly separate scripts you would need to save the return value to a Variable to retain the value from the first Script for the Second Script to process. I'm surprised that you are not getting an error:

PS> .\Test\Get-RetVal.ps1 -RetVal $True  #Equiv of Script 1
True

#Equiv of Script 2
PS> If ($RetVal) {
  "Previous Return Value = $RetVal`n" +
  "Execute Command 3"
}
Else {
  "Previous Return Value = $RetVal`n" +
  "Execute Command 4"

}

#Script 2 Output w/o saved Variable

The variable '$RetVal' cannot be retrieved because it has not been set.
At line:1 char:5
+ If ($RetVal) {
+     ~~~~~~~
    + CategoryInfo          : InvalidOperation: (RetVal:String) [], RuntimeExc 
   eption
    + FullyQualifiedErrorId : VariableIsUndefined
 
#Saving the Value
PS> $RetVal = .\Test\Get-RetVal.ps1 -RetVal $True

#Rerun Script 2 when saved value = True

#Script 2 Output:
Previous Return Value = True
Execute Command 3

#Rerun Script 1 to set $RetVal to False
PS> $RetVal = .\Test\Get-RetVal.ps1 -RetVal $False

#Rerun Script 2 when saved value = False

#Script 2 Output:
Previous Return Value = False
Execute Command 4

PS> 

If the above is not the case you need to post more of your actual scripts

HTH

0
votes

Since they are two independent tasks, you need to set a variable to save the first script result, then you could use the value in the second task.

Here is the script to set the variable in Azure Devops:

echo "##vso[task.setvariable variable=variablename;]value"

You could add this script to the If statement

Here is an example:

Azure PowerShell Task 1

$result = command 1

if($result)

{

 echo "##vso[task.setvariable variable=testvar;]$true"    
   return $true
    
}

else {

    echo "##vso[task.setvariable variable=testvar;]$false"    
    return $false 

}

In this script, it will create a pipeline variable based on conditions. Then in the second powershell task, you could use $variablename to get it.

Azure PowerShell Task 2

For example:

if($testvar = $true)

{
   <run Command 3>
}

else{

<run command 4>

}