Create array or dictionary type variable in runbook of powershell automation

Perumal, Janakiraman 241 Reputation points
2020-10-26T12:47:25.853+00:00

Hi All,

I need to extract the list of disk type based the SKU in my runbook, Is there an option available to update the list of disk name to an variable like an List or Dictionary in Python?

Set-AzAutomationVariable command updates the value, but it is only one at any given point of time, not all the disk.

Regards,
Janakiraman.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,177 questions
0 comments No comments
{count} votes

Accepted answer
  1. Perumal, Janakiraman 241 Reputation points
    2020-10-27T14:26:59.303+00:00

    Hi @tbgangav-MSFT ,

    I have deleted the old variable and created new one as per the above syntax. I could see the commands executed successfully. However, when i checked the values, i didnt get the expected results.

    'i have just printed the @array variable and the value from the above. Results are attached in the below screen shot.

    $array

    New-AzAutomationVariable -AutomationAccountName "TestAutomation" -Name "VMLIST" -Encrypted $False -Value "My String" -ResourceGroupName "AUTOMATION_TESTRG"
    Set-AzAutomationVariable -AutomationAccountName "TestAutomation" -Name "VMLIST" -ResourceGroupName "AUTOMATION_TESTRG" -Value $array -Encrypted $False
    $Variable = Get-AzAutomationVariable -AutomationAccountName "TestAutomation" -Name "VMLIST" -ResourceGroupName "AUTOMATION_TESTRG"
    $Variable.Value

    35298-image.png

    35465-image.png


4 additional answers

Sort by: Most helpful
  1. tbgangav-MSFT 10,416 Reputation points
    2020-10-27T09:19:51.427+00:00

    Hi @Perumal, Janakiraman ,

    Currently, from UI you would not be able to create an array of objects under a single automation variable (this is a related feature request already raised in UserVoice or feedback forum) but as shown below you can store multiple values to a single variable by creating an array or hashtable and saving it to the variable.

    $array = "one",2,"3","four4",5.0  
    Set-AzAutomationVariable -AutomationAccountName "<YourAutomationAccountName>" -ResourceGroupName "<YourAutomationAccountResourceGroupName>" -Name "<AutomationVariableName>" -Value $array -Encrypted $False  
    

    Then to post validate if array of objects are stored in single variable you may follow below steps.

    $GetAAVar = Get-AzAutomationVariable -AutomationAccountName "<YourAutomationAccountName>" -ResourceGroupName "<YourAutomationAccountResourceGroupName>" -Name "<AutomationVariableName>"  
    $GetAAVar.Value.Count  
    $Test1 = [string]$GetAAVar.Value[0]  
    $Test2 = [string]$GetAAVar.Value[1]  
    $Test3 = [string]$GetAAVar.Value[2]  
    $Test4 = [string]$GetAAVar.Value[3]  
    $Test5 = [string]$GetAAVar.Value[4]  
    #If the array is huge and if you want to fetch objects one by one to use in next steps of the script then you may use foreach or for loop as required.  
    
    0 comments No comments

  2. Perumal, Janakiraman 241 Reputation points
    2020-10-27T13:10:45.787+00:00

    Hi,

    I am getting the below error when i run my script, however i could see the variable in portal.

    Set-AzAutomationVariable : The variable was not found. Variable name VMLIST.
    At line:22 char:1

    • Set-AzAutomationVariable -AutomationAccountName "TestAutomation" -Nam ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : CloseError: (:) [Set-AzAutomationVariable], ResourceNotFoundException
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.Automation.Cmdlet.SetAzureAutomationVariable

    Get-AzAutomationVariable : The variable was not found. Variable name VMLIST.
    At line:24 char:13

    • $Variable = Get-AzAutomationVariable -AutomationAccountName "TestAuto ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : CloseError: (:) [Get-AzAutomationVariable], ResourceNotFoundException
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.Automation.Cmdlet.GetAzureAutomationVariable

    code


    Set-AzAutomationVariable -AutomationAccountName "TestAutomation" -Name "VMLIST" -ResourceGroupName "AUTOMATION_TESTRG" -Value $array -Encrypted $False
    $Variable = Get-AzAutomationVariable -AutomationAccountName "TestAutomation" -Name "VMLIST" -ResourceGroupName "AUTOMATION_TESTRG"
    $Variable.Value.Count


  3. Perumal, Janakiraman 241 Reputation points
    2020-10-27T15:20:18.417+00:00

    Here is the screenshot

    35483-out1.png35300-out2.png


  4. Perumal, Janakiraman 241 Reputation points
    2020-10-27T16:22:54.283+00:00

    Hi @tbgangav-MSFT ,

    Thanks a lot for all the support you have provided, the code has worked as expected. As you pointed correctly, it was the variable type issues which caused this. Great experience in working with you.

    Regards,
    Janakiraman.