humblebee
humblebee

Reputation: 1394

variable not getting invoked in azure powershell

I have set a variable in a release pipeline in azure-devops as myFlag : true. I tried to invoke myFlag in a powershell task but am not getting any result. My inline script:

If ( $myFlag -eq 'false' ) {
  Write-Host "Hit!"
} Else {
  Write-Host "Not Hit!"
}

$myFlag is not having any value and hence else block is getting hit.

Upvotes: 0

Views: 60

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 18958

If ( $myFlag -eq 'false' )

The caused reason of no value in $myFlag is this is a definition format of dynamic variable which just exists in current job. In one world, it is creating a new variable named myFlag rather than getting the value from the variable you pre-defined. That's why no value in $myFlag.

According to your logic of script, you are getting value of myFlag which pre-defined in Variables tab, then compare it with "false". If the value of myFlag is False, print out Hit! in log, otherwise print Not Hit!. So, just change your script as:

If ('$(myFlag)' -eq 'false' ) {
  Write-Host "Hit!"
} Else {
  Write-Host "Not Hit!"
}

Or:

$myFlag = '$(myFlag)'

If ($myFlag -eq 'false') {
  Write-Host "Hit!"
} Else {
  Write-Host "Not Hit!"
}

Upvotes: 1

Related Questions