Search code examples
powershellazure-powershell

How to use the use single variable from Get-content command


I have a tfvar file in which I have mutiple variables. I have get the content of it by using this command in powershell.

    $product_list = (Get-Content "../templates/variables/dev.terraform.tfvars" -Raw)
    Write-Host $product_list.virtual_network_address_space

And I got this output:

    client_name_prefix = "f"
    resource_group_postfix = "rg"
    environment_name = "dev"
    instance = "04"

    # Storage Account
    storage_account_tier = "Standard"
    account_replication_type = "GRS"
    container_access_type = "private"
    storage_account_container_name1= "drivemate"
    storage_account_container_name2= "fair-private"
    storage_account_container_name3= "fair-public"

What I want now is to get the value of "resource_group_postfix"


Solution

  • Use ConvertFrom-StringData:

    $Product_list |ConvertFrom-StringData
    
    Name                           Value
    ----                           -----
    instance                       "04"
    storage_account_container_nam… "fair-public"
    storage_account_tier           "Standard"
    resource_group_postfix         "rg"
    environment_name               "dev"
    storage_account_container_nam… "drivemate"
    storage_account_container_nam… "fair-private"
    client_name_prefix             "f"
    account_replication_type       "GRS"
    container_access_type          "private"
    
    ($Product_list |ConvertFrom-StringData).resource_group_postfix
    "rg"
    

    And in case you want to lose the double quotes:

    ($Product_list |ConvertFrom-StringData).resource_group_postfix.trim('"')
    rg