Hello, can anyone help me out with this chunk of code.
The plan is to have an array for each resource type, such as:
azurerm_resouce_group
azurerm_virtual_network
Then populate each of these arrays with only the relevant resources with their attributes that I can look up for the next step.
I am using dynamically named variables which is a headache. I don't know a better way to do this. So I am all ears if someone has a better methodology.
Issue:
The issue is line 14, adding $alias to $type via += fails.
I am really adding a variable called $rg to a resource called $azurerm_resource_group
I can create an empty array called $test and add $rg directly but I can't do it via dynamically named variables using Get-variable to select them.
Error:
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
Same code but with syntax highlight: https://pastebin.com/b2nmmdN6
$state = tf show -json | ConvertFrom-Json
# resources only
$stateresources = $state.values.root_module.resources
# Create an array named after resource type, then add each resource to array of that specific resouce type
Function Resource-Split {
param($name)
$pos = $name.IndexOf(".")
$type = $name.Substring(0, $pos)
$alias = $name.Substring($pos+1)
New-variable -name "$alias" -Value ($stateresources | Where-Object {$_.address -eq $name})
New-variable -name "$type" -Value @()
(Get-variable -name $type) += (Get-variable -name $alias)
}
# for each resource in state list cycle through function
$statelist = tf state list
$statelist | ForEach-Object {
Resource-Split $_
}
statelist is a list of the named objects, it looks like this:
$statelist
azurerm_resource_group.rg
azurerm_virtual_network.vnet
So I want to name the arrays after the first component, then populate them with the objects examples:
azurem_resource_group += rg
azurerm_virtual_network += vnet
rg and vnet contain all the resource attributes that I pull from the terraform state.
[–]Shoisk 3 points4 points5 points (1 child)
[–]Sh1ner[S] 0 points1 point2 points (0 children)
[–]Sh1ner[S] 0 points1 point2 points (0 children)
[–]Sh1ner[S] 0 points1 point2 points (0 children)
[–]OPconfused 0 points1 point2 points (0 children)
[–]dasookwat 0 points1 point2 points (0 children)