Skip to content

Commit aa1a2af

Browse files
PsCustomObjectPsCustomObject
authored andcommitted
Added new post
1 parent 7a03e1d commit aa1a2af

10 files changed

Lines changed: 198 additions & 1 deletion

_posts/2019-02-26-Store-Credentials-in-PowerShell-Script.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ In the article we have seen different approaches to store passwords in PowerShel
142142

143143
Among the methods illustrated I find last one easier to implement as encrypted passwords can be centrally stored on a file and sourced from multiple scripts using the correct one for the application or service that script needs to connect to.
144144

145-
All the code and used functions will be available in my GitHub account shortly and will be made part of my *IT-Toolbox* which will be released soon(tm).
145+
All the functions used in the post are available through my **[IT-ToolBox](https://github.com/PsCustomObject/IT-ToolBox)** module.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
---
2+
title: "Variables in Azure Automation"
3+
excerpt: "In this post we will explore how we can use variables in Azure Automation. We will explore both encrypted and unencrypted variables and how we can leverage them in our scripts."
4+
categories:
5+
- PowerShell
6+
- Azure
7+
8+
tags:
9+
- PowerShell
10+
- Office365
11+
- Azure
12+
- Azure Automation
13+
14+
toc: true
15+
header:
16+
teaser: "/assets/images/Azure_Automation_Logo.png"
17+
---
18+
19+
## Azure Automation Variables as Shared Resources
20+
21+
Shared Resources in Azure Automation allow us to reuse credentials, modules, schedules, connections, certificates and **variables** which will be will be the main focus of this post.
22+
23+
To better understand the importance of shared resources and *variables in Azure Automation* let's go through a practical example. I have a runbook pulling data from a web service which requires to reference an API secret as part of the process to obtain an authentication token.
24+
25+
When running the script through a scheduled task or through on-prem System Center Orchestrator you would either need to store the key in plain text in the script itself or use methods described in my post *[Store Credentials in PowerShell Script](https://pscustomobject.github.io/powershell/howto/Store-Credentials-in-PowerShell-Script/)*
26+
27+
```powershell
28+
# Plain text
29+
[strint]$apiSecrect = '14??!==AbZyC78mk'
30+
31+
# Use New-StringDecryption cmdlet from IT-ToolBox module
32+
New-StringDecryption -EncryptedString $apiSecret
33+
```
34+
35+
While both approaches will work that's not without drawbacks implying security considerations. Another consideration is the fact secret key at some point could change and, if a large number of scripts/runbooks, is using it we would need to update code all of solutions using it. This is were *Azure Automation variables* come into play.
36+
37+
## Azure Automation Variables Types
38+
39+
Azure Automations supports two types of variables *Encrypted* and *Unenrcrypted* with the following types being supported:
40+
41+
- Integers
42+
- Strings
43+
- DateTime
44+
- Boolean
45+
- Null
46+
47+
Full documentation it is available [here](https://docs.microsoft.com/en-us/azure/automation/shared-resources/variables).
48+
49+
### Unencrypted variables
50+
51+
As the name implies an *unencrypted* variables are stored in Azure with their values being *visible* and to both runbooks/scripts and administrators.
52+
53+
An unencrypted variable can be created in the Azure portal going to **[Automation Account] / [Shared Resources] / [Variables]** and selecting **Add Variable** being sure to select *No* under the **Encrypted** section.
54+
55+
<figure>
56+
<a href="https://pscustomobject.github.io//assets/images/Azure_Automation_Unencrypted_Variable.png">
57+
<img src="/assets/images/Azure_Automation_Unencrypted_Variable.png"></a>
58+
</figure>
59+
60+
**Note:** Typo in the above variable value is intended.
61+
{: .notice--primary}
62+
63+
The same operation can be achieved via PowerShell assuming the **Az** module is installed on the system:
64+
65+
```powershell
66+
New-AzAutomationVariable -ResourceGroupName $azResourceGroup –AutomationAccountName $azAccount –Name 'Test Variable' –Encrypted $false –Value 'Test Value'
67+
68+
# Output
69+
Value : Test Value
70+
Encrypted : False
71+
ResourceGroupName : Dev-PsCustomObject-AutomationRsg
72+
AutomationAccountName : Dev-PsCustomObject
73+
Name : Test Variable
74+
CreationTime : 26.09.2020 17:53:23 +02:00
75+
LastModifiedTime : 26.09.2020 17:53:23 +02:00
76+
Description :
77+
```
78+
79+
Once variable has been created it will be visible in the console:
80+
81+
<figure>
82+
<a href="https://pscustomobject.github.io//assets/images/Azure-Automatin-Unencrypted-Variables-Console.png">
83+
<img src="/assets/images/Azure-Automatin-Unencrypted-Variables-Console.png"></a>
84+
</figure>
85+
86+
As you can see values of the variable is visible both in the PowerShell output and the *Value* column of the variables blade in Azure Portal. We will discuss this in more detail in a minute.
87+
88+
### Encrypted variables
89+
90+
Creation *encrypted* variables is identical in terms of steps in the console and via PowerShell simply requires us to specify the *-Encrypted $True* parameter.
91+
92+
```powershell
93+
New-AzAutomationVariable -ResourceGroupName $azResourceGroup –AutomationAccountName $azAccount –Name 'Test Encrypted Variable' –Encrypted $true –Value 'Test Value'
94+
95+
# Output
96+
Value :
97+
Encrypted : True
98+
ResourceGroupName : Dev-PsCustomObject-AutomationRsg
99+
AutomationAccountName : Dev-PsCustomObject
100+
Name : Test Encrypted Variable
101+
CreationTime : 26.09.2020 18:51:53 +02:00
102+
LastModifiedTime : 26.09.2020 18:51:53 +02:00
103+
Description :
104+
```
105+
106+
As you can see when creating an encrypted variable Value is *omitted* in the output, in the *variables* blade it will be displayed like this
107+
108+
<figure>
109+
<a href="https://pscustomobject.github.io//assets/images/Azure_Automation_Encrypted_Variable.png">
110+
<img src="/assets/images/Azure_Automation_Encrypted_Variable.png"></a>
111+
</figure>
112+
113+
This is the expected behavior as encrypted variables are *secured* with a unique key generated for each **automation account**. It goes alone encrypted variables are more secure in nature but it has to be kept in mind that, once created, azure automation variables have been created **values cannot be seen only updated**.
114+
115+
### Update and Retrieve Azure Automation variable values
116+
117+
Now that we know how to create azure automation variables let's see how to work with them and update their values. Recall I had a typo in the unencrypted variable, trailing '[' character, let's fix that from the console
118+
119+
<figure>
120+
<a href="https://pscustomobject.github.io//assets/images/Azure_Automation_Update_Variable_Value.png">
121+
<img src="/assets/images/Azure_Automation_Update_Variable_Value.png"></a>
122+
</figure>
123+
124+
Or from PowerShell
125+
126+
```PowerShell
127+
Set-AzAutomationVariable -ResourceGroupName $azResourceGroup -AutomationAccountName $azAccount -Name 'Test Variable' -Value 'Setting new value from PowerShell' -Encrypted $False
128+
129+
Value : Setting new value from PowerShell
130+
Encrypted : False
131+
ResourceGroupName : Dev-PsCustomObject-AutomationRsg
132+
AutomationAccountName : Dev-PsCustomObject
133+
Name : Test Variable
134+
CreationTime : 26.09.2020 17:53:23 +02:00
135+
LastModifiedTime : 26.09.2020 19:08:28 +02:00
136+
Description : Fixed typo in value
137+
```
138+
139+
When working with an encrypted variable things will be slightly different. **Encrypted variables can only have their value updated but never shown** to do so simply click the *Edit value* button and then save it
140+
141+
<figure>
142+
<a href="https://pscustomobject.github.io//assets/images/Azure_Automation_Update_Encrypted_Variable.png">
143+
<img src="/assets/images/Azure_Automation_Update_Encrypted_Variable.png"></a>
144+
</figure>
145+
146+
From PowerShell command will not be much different but, again, we will not get back the value from the cmdlet
147+
148+
```powershell
149+
Set-AzAutomationVariable -ResourceGroupName $azResourceGroup -AutomationAccountName $azAccount -Name 'Test Encrypted Variable' -Value 'Setting new value from PowerShell' -Encrypted $true
150+
151+
Value :
152+
Encrypted : True
153+
ResourceGroupName : Dev-PsCustomObject-AutomationRsg
154+
AutomationAccountName : Dev-PsCustomObject
155+
Name : Test Encrypted Variable
156+
CreationTime : 26.09.2020 18:51:53 +02:00
157+
LastModifiedTime : 26.09.2020 19:14:46 +02:00
158+
Description
159+
```
160+
161+
## Getting Variable value
162+
163+
Up to this point we've seen how to create and update Azure Automation variables, let's explore how to get values for configured variables.
164+
*Az* module makes available a cmdlet for the purpose
165+
166+
```powershell
167+
PS C:\Users\Lethe> Get-AzAutomationVariable -ResourceGroupName $azureResourceGroup -AutomationAccountName $azureAccount -Name 'Test Variable'
168+
169+
Value : Setting new value from PowerShell
170+
Encrypted : False
171+
ResourceGroupName : Dev-PsCustomObject-AutomationRsg
172+
AutomationAccountName : Dev-PsCustomObject
173+
Name : Test Variable
174+
CreationTime : 26/09/2020 17:53:23 +02:00
175+
LastModifiedTime : 26/09/2020 19:08:28 +02:00
176+
Description : Fixed typo in value
177+
```
178+
179+
**Note:** Omitting the *-Name* parameter will return all configured variables
180+
{: .notice--primary}
181+
182+
As I mentioned it is not possible to retrieve values for encrypted variables as they're available within the runbook at runtime via the **Get-AutomationVariable** cmdlet. Using the *Test Encrypted Variable* as example I've created a small script that will show this behavior
183+
184+
```powershell
185+
$encryptedVariableValue = Get-AutomationVariable -Name 'Test Encrypted Variable'
186+
187+
Write-output "The encrypted variable value is: $encryptedVariableValue"
188+
```
189+
190+
And here the result from Azure Automation test pane:
191+
192+
<figure>
193+
<a href="https://pscustomobject.github.io//assets/images/Azure_Automation_Encrypted_Variable_Runbook.png">
194+
<img src="/assets/images/Azure_Automation_Encrypted_Variable_Runbook.png"></a>
195+
</figure>
196+
197+
As simple as that, the same snippet can be used in production script to safely store *secrets* in our scripts.
28.7 KB
Loading
17.4 KB
Loading
18.9 KB
Loading
9.48 KB
Loading
17.7 KB
Loading
58.5 KB
Loading
69 KB
Loading
79.9 KB
Loading

0 commit comments

Comments
 (0)