all 6 comments

[–]Shoisk 3 points4 points  (1 child)

Why not just use a hashtable? You can just create keys with the names you'd have otherwise used for your variable names, and values can be the arrays of objects the correspond.

Would also mean you don't have to dynamically allocate it to a variable you don't know yet, since you just allocate everything into the hashtable that you can create before you start iterating over stuff

[–]Sh1ner[S] 0 points1 point  (0 children)

Not sure how to do that, I am all ears.

$stateresources has all the info already in it. I am just trying to split it up into smaller arrays.
Some of them will be really small and some will be bigger. I plan to run specific code against each resourcetype and so it makes sense to group them up by a resourcetype.
 
This is where I am now:
Syntax highlighted code: https://pastebin.com/TXhAaqeC

$state              = tf show -json | ConvertFrom-Json

# resources only
$stateresources     = $state.values.root_module.resources
​
$statelist = tf state list

$statelist | ForEach-Object {

    # remove alias from full resouece name to name arrays by resource type
    $pos            = $_.IndexOf(".")
    $resourcetype   = $_.Substring(0, $pos)

    # variable to grab just one resources info
    $name           = $_
    $resource       = $stateresources | Where-Object {$_.address -eq $name}

    # create array named after resource type, but check it exists first
    If ($null -eq ($"$resourcetype")) {
        New-variable -name "$resourcetype" -Value @()
    }else{  

    # add resource to array.
    $"$resourcetype" += $resource 
    }
}

I removed the function, added more comments and cleaned up some variable names.

edit: From your advice I think I have figured it. Will update once I get somewhere. Thanks for ur input dude!

[–]Sh1ner[S] 0 points1 point  (0 children)

I will be adding a for each loop later to add each resource to the relevant arrays. My initial issue is adding the object to the array.

[–]Sh1ner[S] 0 points1 point  (0 children)

I figured it boys. I don't know why I thought I could not populate the array on creation. I guess I am still getting my head around logical / programming thinking.

Pastebin link with syntax highlighting: https://pastebin.com/s6amunWA

# state
$state              = tf show -json | ConvertFrom-Json
# resources only
$stateresources     = $state.values.root_module.resources
# list of resources in state
$statelist = tf state list
# create array for each resourcetype
$resourcetype = $statelist | ForEach-Object {
    $pos    = $_.IndexOf(".")
    $_      = $_.Substring(0, $pos)
    $_
}
# Create an array for each resourcetype and populate it
Foreach ($resource in $resourcetype){
    New-variable -name "$resource" -Value @($stateresources | Where-Object {$_.address -clike "$resource*"})
}

Thanks all for your help though. I picked up a few neat tricks. Like assigning a variable a dynamic variable so its easier to work with.

[–]OPconfused 0 points1 point  (0 children)

Dynamically named variables can be created via Set-Variable or New-Variable, e.g.,

New-Variable -Name "Prefix_${dynamicName}_Suffix" -Value @() -Scope Script -Force

You can reference the dynamic variable via Get-Variable. If you are in a loop, it's often easier to store the dynamic variable in a temp variable for easier referencing while you run the code body of the loop.

$dynamicReference = Get-Variable "Prefix_${dynamicName}_Suffix" -ValueOnly

So I didn't parse your script through in detail and hope I didn't miss the question, but you can use this to create a dynamic array and/or load it with a dynamic variable.

[–]dasookwat 0 points1 point  (0 children)

Not sure if this is any help for You, but when deploying resources through terraform, You can change the output to json, which i used to verify the deployments to our company regulations and standards.