importing variable from csv

Ondrej Pristach 61 Reputation points
2020-09-29T09:06:52.097+00:00

Hi,
i have a script which fills two comb boxes. I would like to import the values from csv instead.
The main combo is however referring to the variables already set, and i cannot load the variable name from CSV.

$1001=[collections.arraylist]@(
    [pscustomobject]@{Name='1SP1';Value='1SP01'}
    [pscustomobject]@{Name='1SP2';Value='1SP02'}
)
$1002=[collections.arraylist]@(
    [pscustomobject]@{Name='2SP1';Value='2SP01'}
    [pscustomobject]@{Name='2SP2';Value='2SP02'}
    [pscustomobject]@{Name='2SP3';Value='2SP04'}
)
$1003=[collections.arraylist]@(
    [pscustomobject]@{Name='3SP1';Value='3SP01'}
    [pscustomobject]@{Name='3SP2';Value='3SP02'}
)
$maintable=[collections.arraylist]@(
 [pscustomobject]@{Name='1SP';Value=$1001}
 [pscustomobject]@{Name='2SP';Value=$1002}
 [pscustomobject]@{Name='3SP';Value=$1003}
)

What would be the right way? Import only names and runing a foreach to define the variable name?
Can i use variable name defined with dynamic name $($)?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2020-09-30T18:53:52.453+00:00

    To accomplish this, use Invoke-Expression.

    You can ignore everything in the code below up to line 32 (it's just creating the pseudo-CSV data):

    $CSV1001 = @'  
    Name,Value  
    1SP1,1SP01  
    1SP2,1SP02"  
    '@  
      
    $CSV1002 = @'  
    Name,Value  
    2SP1,2SP01  
    2SP2,2SP02  
    2SP3,2SP03  
    '@  
      
    $CSV1003 = @'  
    Name,Value  
    3SP1,3SP01  
    3SP2,3SP02  
    '@  
      
      
    [collections.arraylist]$1001 = $CSV1001 | ConvertFrom-Csv  
    [collections.arraylist]$1002 = $CSV1002 | ConvertFrom-Csv  
    [collections.arraylist]$1003 = $CSV1003 | ConvertFrom-Csv  
      
    $CSVmaintable = @'  
    Name,Value  
    1SP,$1001  
    2SP,$1002  
    3SP,$1003  
    '@  
      
    [collections.arraylist]$maintable = $CSVmaintable |   
        ConvertFrom-Csv |   
            ForEach-Object{  
                [PSCustomObject]@{  
                    Name = $_.Name  
                    Value = invoke-Expression $_.Value  
                }  
            }  
    
    
      
      
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2020-09-29T14:21:59.447+00:00

    How about trying something simple?

    [collections.arraylist]$1001=import-csv C:\junk\1001.csv
    [collections.arraylist]$1002=import-csv C:\junk\1002.csv
    

    The CSV's would contain two columns: Name and Value. Each row of the imported CSV is represented as a PSCustomObject.

    0 comments No comments

  2. Ondrej Pristach 61 Reputation points
    2020-09-30T16:47:43.747+00:00

    Hello RichMatheisen-8856.
    It seems i have not explained myself correctly. I know how to import CSV, that is not the issue.
    I have an issue importing the maintable where the value is taking the defined arrays.
    Maintable has different names and uses existing arrays $1001, $1002 and $1003 as values.
    Maintabe is one combobox, and depending on what is selected, it loads names and values from the for example $1001 and shows them in second combobox.
    I am not able to import the maintable csv because the value is just a string.
    If anyone can suggest how to define the value in the maintable bby importing from csv, or after import with a foreach ill be happy.

    0 comments No comments

  3. Ondrej Pristach 61 Reputation points
    2020-10-02T08:14:18.143+00:00

    Thank you RichMatheisen-8856, you have helped me quite a lot!
    i have to shamefully admit i have not thought about using invoke-expression.
    Thanks for the lesson.

    0 comments No comments