I want to output objects based on names from a collection.
The questions has its origin is a script that outputs Active Directory DNS records, parsed by RecordType. But I generalized the code so that testing is easier ... This method would be great for automatic creation of objects by referencing data from object members.
But I don't know how to reference the variable to add the data from the input object. Please see the line with comment "# works not"
Here is an example:
# add example values to create object
Set-Content -Path .\My.csv -Value "Letter;Color;Orientation;Direction"
Add-Content -Path .\My.csv -Value "A;;Top;Left"
Add-Content -Path .\My.csv -Value "A;;Bottom;Right"
Add-Content -Path .\My.csv -Value "B;Green;;Left"
Add-Content -Path .\My.csv -Value "B;Red;;Left"
Add-Content -Path .\My.csv -Value "C;Black;Mid;"
Add-Content -Path .\My.csv -Value "C;Red;Top;"
# define members
$m_A = @("Letter", "Orientation", "Direction")
$m_B = @("Letter", "Color", "Direction")
$m_C = @("Letter", "Color", @{n="Richtung";e={"Orientation"}})
# define output collections
$c_A = @()
$c_B = @()
$c_C = @()
# fill output collections
$in = Import-csv -Path .\My.csv -Delimiter ";"
$in | ForEach-Object{
$c_A += $_ | Select-Object (Get-Variable -Name $("m_" + $_.Letter)).Value # Works
$c_$_.Letter += $_ | Select-Object (Get-Variable -Name $("m_" + $_.Letter)).Value # Works not
}
@("A", "B", "C") | ForEach-Object{
(Get-Variable -Name $("c_" + $_)).Value | Export-Csv -Path .\$_ -Delimiter ";" -Encoding UTF8
}