0

I am new to Powershell.I have a csv file named samplemap.csv with values . I have to read each value from the file, like example a variable to define the value of paris_App.

CntryApp Version
paris_App 2.19
ila_App 5.3
sga_App 3.10

The code I used is printing all the CntryApp version , not printing each CntryApp version separately

Import-CSV "H:\samplemap.csv"
$CntryApp = @()
$Version = @()
 Import-CSV "H:\samplemap.csv" -Delimiter ';'
ForEach-Object {
        $CntryApp += $_.CntryApp
        $Version += $_.Version
}

Expected 2.19,5.3,3.10 to be assigned to a indivdual variable . I mean each CntryApp version to be assigned to a variable.

5
  • So you want $Mapping to have paris_App, ila_App and sga_App and $version to have all three versions? From the code I personally have no idea what you want to achieve... Commented Jun 14, 2019 at 19:10
  • Please add the info of what you want to achieve to the question. Commented Jun 14, 2019 at 19:20
  • hey sorry I edited the code @robdy Commented Jun 14, 2019 at 19:24
  • I still don't follow, please add an example of what's the expected output (unless Matt guessed correctly what you mean). Commented Jun 14, 2019 at 19:26
  • Sorry for the confusion @robdy, my csv files contains cntry_App,version like key-value pairs , what i am trying to achieve is extract each cntry_app's version to a individual variable like to get value of paris_app $xyz=$cntryApp.version[0] Commented Jun 14, 2019 at 19:39

2 Answers 2

1

If I understand you correctly you want individual variables for each entry in your input file e.g. $paris_App? While that is possible using something like Set-Variable a better approach would be creating a hashtable of those key value pairs. That was you could call the version if you know the name.

$apps = @{}
Import-CSV "H:\samplemap.csv" -Delimiter ';' | ForEach-Object{
    $apps[$_.CntryApp] = $_.Version
}

$apps['sga_app']

This approach would fail if those CntryApp contained duplicates.

Sign up to request clarification or add additional context in comments.

Comments

0

To import data from .csv file (space-delimited in that case) you have to specify delimiter (unless it matches the default one which might be dependent on your regional settings and I'm pretty sure that space is not the default one).

If you want to use that data later, it's good to save the imported values to variable:

$t = Import-CSV "$($pwd.path)\samplemap.csv" -Delimiter ' '

Once you save it, you can access it using the following structure

# All values of CntryApp
$t.CntryApp

# Output
paris_App
ila_App
sga_App

# CntryApp and version of first object
$t[0]

# Output
CntryApp  Version
--------  -------
paris_App 2.19

# Version of first app
$t[0].Version

# Output 
2.19

As you mentioned key:value scenario, it'd be natural (although it might look a bit more complex) to consider hastable as @Matt suggested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.