Home > front end >  How to access Azure DevOps Stage name in Powershell?
How to access Azure DevOps Stage name in Powershell?

Time:02-16

I'm trying to write some Powershell logic that looks at the release stage name and implements some code based on the environment being released to.

For instance, if my stage is named "DEV", I'm going to do xyz, or if my stage is "QA" I'm going to do abc...

I've tried something like this:

 $s = $(Release.EnvironmentName)

 $s = $(System.StageDisplayName)

 if ($s -like "DEV") 
 {
   // execute some code
 }

but every time, it blows up because it says "DEV" is not a recognized command outlet, etc. I've tried injecting the predefined variables in the if block, but it also blows up.

Any help on syntax?

DEV : The term 'DEV' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
2022-02-14T19:50:59.5173313Z spelling of the name, or if a path was included, verify that the path is correct and try again.
2022-02-14T19:50:59.5174118Z At D:\_agent1\_work\_temp\....ps1:37 char:21
2022-02-14T19:50:59.5174759Z                   if (DEV -like DEV)
2022-02-14T19:50:59.5175946Z                       ~~~
2022-02-14T19:50:59.5176689Z       CategoryInfo          : ObjectNotFound: (DEV:String) [], ParentContainsErrorRecordException
2022-02-14T19:50:59.5177416Z       FullyQualifiedErrorId : CommandNotFoundException

CodePudding user response:

my Powershell scripts wasn't work without quotes around azure devops variables, try something like this:

$branchSource = "$(Build.SourceBranch)"
$branchSourcePath = $branchSource -replace "refs/heads/", ""

switch -Wildcard ( $branchSourcePath )
{
    "rc" {}
    "develop" {}
}
  • Related