# How to save complex PowerShell variables as CliXML instead of  Newtonsoft.Json.Linq.JProperty in the Azure Automation

Azure Automation Account has a handy feature to define (shared) [Variables](https://learn.microsoft.com/en-us/azure/automation/shared-resources/variables?tabs=azure-powershell) that can be used across all your Runbooks. What is not so nice at least for me was finding that complex objects like hashtables, arrays, psobjects, etc are stored as [Newtonsoft.Json.Linq.JProperty](https://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json_Linq.htm)

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669704377640/5SExmxxx7.png align="left")

This makes retrieving the content of the variables quite unfriendly if you are not familiar with this `Newtonsoft.Json.Linq.JProperty`.

---

# Solution

<div data-node-type="callout">
<div data-node-type="callout-emoji">💥</div>
<div data-node-type="callout-text">Because of Runbook string variable size limitations, this approach is suitable only for variables of small size. To overcome this limitation check <a target="_blank" rel="noopener noreferrer nofollow" href="" style="pointer-events: none">Create persistent Azure Automation Runbook variables using Azure Blob Storage</a></div>
</div>

How to "solve" this? Save variables using old, and good `Export-CliXML` and then retrieve them using `Import-CliXML` 😉. If you do so, nothing will change for you when you try to work with the saved variable. The array will be still array, psobject will be still psobject, etc. **But beware that you have to save the output of** `Export-CliXML` **as a string**! Otherwise Azure will again use `Newtonsoft.Json.Linq.JProperty` to save the variable.

To make this easier I've created two proxy functions `Set-AutomationVariable2` and `Get-AutomationVariable2` (part of my [AzureResourceStuff](https://www.powershellgallery.com/packages/AzureResourceStuff/) module).

## Step by step

### Import the required module to your Automation account

To be able to use my functions, you need to import the module [AzureResourceStuff](https://www.powershellgallery.com/packages/AzureResourceStuff/) to your Automation account. You can do it manually or use `New-AzureAutomationModule` (check [this](https://doitpsway.com/import-new-or-update-existing-powershell-module-including-its-dependencies-into-azure-automation-account-using-powershell) for more details)

```powershell
New-AzureAutomationModule -moduleName 'AzureResourceStuff' -resourceGroupName <resourceGroupName> -automationAccountName <automationAccountName>
```

### Create a new Automation variable

Create a **new**, **empty** variable of a **string type** that will be used to store our results.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711448758176/df7cea4a-22ac-4462-be44-f15d01077dce.png align="center")

### Save the output to the Automation variable

To save the output to the Automation variable use the function `Set-AutomationVariable2` like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711449163291/b00b330b-95c9-4493-8a8d-11f6c4b98f75.png align="center")

How the variable will look like:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669705875776/QeHfHZxjQ.png align="left")

### Retrieve the Automation variable content and convert it back

To retrieve variable content and convert it back to the original object use the function `Get-AutomationVariable2` like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711449207185/f2e61db0-1a23-4fbc-9089-e8100b06220b.png align="center")

### Tip

Don't forget you have to **authenticate** to Azure before working with Automation variables!

```powershell
Connect-AzAccount -Identity
```
