Archive
[Code Snippet] PowerShell | Azure DevOps | Query Variable Group using API
In Azure DevOps, Variable groups store values and secrets that you might want to be passed into a YAML pipeline or make available across multiple pipelines.
Variable Group is a collection of variables with Name and Value pair.
Below is my variable Group ‘BillingSystemDictionary’ with 3 variables in my DevOps organization ‘RajeevPentyala0011’ and project ‘Demo’ (These details are required in PowerShell script).
In DevOps pipeline scenarios, you may need to fetch the variables under a variable group. Following is the PowerShell script which calls DevOps API and fetch variables by variable group name.
# Form API authentication Header
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $(System.AccessToken)")
$headers.Add("Content-Type", "application/json")
# Pass Variable Group Name and read variables
$variableGroupName = "BillingSystemDictionary"
# 'projectid' pattern is https://dev.azure.com/{organization_name}/{projectname}
$projectid = "https://dev.azure.com/RajeevPentyala0011/Demo"
$variableGroupGetUrl = "$projectid/_apis/distributedtask/variablegroups?groupName=$variableGroupName*&queryOrder=IdDescending&api-version=6.0-preview.2"
$queryResponseObject = Invoke-RestMethod -Uri $variableGroupGetUrl -Method GET -Headers $headers
# $queryResponseObject will be of type pscustomobject
# To fetch the variable value of "isactive"
$variableName = "isactive"
$variableValue = $queryResponseObject.value.variables.$variableName.value
Write-Host "$variableName value is $variableValue"
🙂
Canvas Apps | Optimization | Quick way to find and clean unused variables
Many a times, in Canvas Apps we would have declared variables but no longer used them. In a complex App with substantial screens, its tedious to identify and clean up the unused variables.
In this article, lets learn how to identify unused variables and clean up. If you are unversed with ‘Variables’, refer this article.
On a high level, Set() is used to define Global variable and UpdateContext() or Navigate() are used to define Context variable (i.e., Scope limited to a screen).
To understand unused variables and clean better, I’ve built a simple Canvas App using below steps.
- Create a new Canvas App from Power Apps portal.
- On App ‘OnStart’ event, declared 4 Global variables using Set().
- Add a new Screen with 3 buttons. As buttons Text, used the one of the global variables(i.e.,btnLabel1) declared in App ‘OnStart’. Repeated the same for other 2 buttons.
- To understand Context variables, on “btn1’s” OnSelect declared 2 context variables using UpdateContext().
- Now that we got both Global and Context variables, lets understand about variables in-depth.
- Go to File -> Variables, you will find 2 tabs ‘Global’ and ‘Screen1’ (this is for Screen1 Context variables).
- Now click on one of the Variables, and you will find 3 tabs:
- Definitions : Statement where this variable was declared. In our case its App ‘OnStart’.
- Uses : Where this Variable used.
- Indirect uses : Any control indirectly refers the variable. Lets see this in detail in next step.
- Click on ‘Uses‘, you will find where ‘btnLabel3’ directly referred (i.e., Text of button 3).
- To understand ‘Indirect uses‘, lets add a new Label and set the Text as “This is indirect label of a Button : ” & btn3.Text“.
- In the above statement, ‘Button 3’ text was ‘Directly’ set to ‘btnLabel3’ variable, here new Label’s text was set to ‘Button 3’ text, which indirectly referring ‘btnLabel3’ variable.
- Now, click on ‘Indirect uses‘ of “btnLabel3” variable and you would see the new Label’s text statement.
- Coming back to the crux of this article, to identify a Unused variable, click on Variable and go to ‘Uses‘ tab. If its blank, you can navigate to the declaration statement from ‘Uses‘ and remove the statement.
🙂
Power Apps – Understanding ‘Variables’
If you are a ‘Power Apps’ beginner and wonder how to declare and use variables like the way you do in any of your favorite programming language (C#, Java, PHP, etc..) this article is for you.
Before we jump in to ‘Power Apps’ Variables, lets first understand following things:
- ‘Power Apps’ is different and works more like Excel.
- Power Apps and Excel both automatically recalculate formulas as the input data changes, without the need of Variables.
- In below screen,
- Sum of A and B is being calculated and set to the Label just by summing up the Text values directly like a formula.
- If either A or B value changes, Label will recalculate the value.
Why we need variables in ‘Power Apps’?
- In general, its recommended to avoid using variables in Power Apps.
- But sometimes only a variable can enable the experience you want.
- To understand this, lets tweak the previous screen by adding a Button and Sum up the values on button click.
- Here we can’t use formulas (i.e., Like screen 1) to calculate the Sum because its value depends on button Click.
- We require a variable to hold the Sum before its set to the Variable to display.
Types of Variables:
Power Apps has three types of variables:
Global Variable:
- You set the value of the global variable with the Set function.
- Set( Var_Name, 0 ) sets the global variable to a value of 0.
- Global variables can hold any value, including strings, numbers, records, and tables
- In my example, on ‘OnSelect’ of ‘Calculate’ button, using ‘Set’ function, I am setting Sum of txt1.Text and txt2.Text to a Global variable ‘globalVar‘.
- And setting the ‘Text’ of ‘Label’ to Global variable ‘globalVar‘.
- Global variable scope is ‘App’ level, can be references from anywhere in the app. Which means, I can read the ‘globalVar‘ from a different screen (i.e., Screen2).
Context Variable:
You implicitly establish and set context variables by using the UpdateContext or Navigate function.
UpdateContext:
- UpdateContext( { Var_Name: 0} ) sets the context variable to a value of 0.
- Context variables can hold any value, including strings, numbers, records, and tables
- In my example, on ‘OnSelect’ of ‘Calculate’ button, using ‘UpdateContext’ function, I am setting Sum of txt1.Text and txt2.Text to Context variable ‘ctxVar‘.
- And setting the ‘Text’ of ‘Label’ to Context variable ‘ctxVar‘.
- ‘OnSelect’ of ‘Clear Context Variable’ button, I am setting ‘ctxVar‘ to 0.
- Context variable set using ‘UpdateContext’ function’s scope is ‘Screen’ level, can only be references with in the screen.
Navigate:
- You can also set a context variable when you use the Navigate function to show a screen.
- In the example below, lets add a new button ‘Navigate to Screen2’ and on ‘OnSelect’, navigate to ‘Screen2’ by passing ‘ctxvar‘ as argument.
- Navigate(Screen2,ScreenTransition.Fade,{ctxvar:txt1.Text+txt2.Text})
- On ‘Screen2’ set the Label’s text to ‘ctxvar’.
- Run the application, click on ‘Navigate to Screen2’
- On the ‘Screen2’ we get ‘ctxvar’ value displayed in label.
- Important thing to notice is, Except for Navigate, context variables are limited to the context of a single screen, which is where they get their name. You can’t use or set them outside of this context.
Use Collection:
- Collection holds a table that is easy to modify.
- Create and set collections by using the ClearCollect function.
- You can use the Collect function instead, but it will effectively require another variable instead of replacing the old one.
- In my example, on ‘OnSelect’ of ‘Add to Collection’ button, collect the ‘txt1.Text’ values to a collection ‘collSum‘ using Collect(collSum,txt1.Text)
- We clear the Collection ‘collSum‘ using Clear(collSum) function.
- To display the ‘collSum’ values, add a ‘DataTable’ control and set ‘Data source’ to ‘collSum’.
- Run the application and keep adding the values. You should see collection values in a table as below.
Notes:
- You can see the Variables and Collection on the File menu of your App.
- If you give a context variable the same name as a global variable or a collection, the context variable takes precedence. However, you can still reference the global variable or collection if you use the disambiguation operator @[Var_Name].
- All variables are held in memory while the app runs. After the app closes, the values that the variables held are lost.
- When the user opens the app, all variables have an initial value of blank.
🙂