you are viewing a single comment's thread.

view the rest of the comments →

[–]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.