Read value of variable in CSV file that is be initialized in powershell script

Welcome Forums General PowerShell Q&A Read value of variable in CSV file that is be initialized in powershell script

Viewing 3 reply threads
  • Author
    Posts
    • #271543
      Participant
      Topics: 15
      Replies: 13
      Points: 82
      Rank: Member

      How can i  read  value of variable in  CSV file,  which is initialized in my script.

      My config.csv file:

      FileName|Path
      test.properties|.\Folder\$custom

      and Powershell script: test.ps1

      $custom = "MA"

      Import-Csv "C:\Users\MA\Desktop\config.csv" -delimiter "|" | ForEach-Object {
        $fileName = $_.FileName
        $path = $_.Path
        $relativeFilePath = Join-Path $path $fileName

      }

      $filePath = $relativeFilePath
      $pathNew = "C:\Users\MA\Desktop"

      $pathNewFile = (Resolve-Path (Join-Path $pathNew $filePath)).Path

      After i run this code i get:

      “C:\Users\MA\Desktop\Folder\$custom\test.properties”

      but i want get: “C:\Users\MA\Desktop\Folder\MA\test.properties”

       

      • This topic was modified 2 weeks, 3 days ago by Majd.
      • This topic was modified 2 weeks, 3 days ago by Majd.
      • This topic was modified 2 weeks, 3 days ago by Majd.
    • #271573
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      You’re not going to be able to use it the way you’re trying. The best I can come up with at the moment is use a string and then Get-Variable to extract the value.

    • #271582
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      OK here’s another way. You can use a hashtable to store/retrieve the value

      • #271588
        Participant
        Topics: 15
        Replies: 13
        Points: 82
        Rank: Member

        Thanks alot, but i must have separated config.csv file.

    • #271597
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      Yeah? Nothing here says you can’t. I was just using that as an example…

      Here’s the csv file….

      And either of the methods shown work the same way…

      or

      • #271711
        Participant
        Topics: 15
        Replies: 13
        Points: 82
        Rank: Member

        Thanks alot. It works  very well.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.