3

I am trying to parse an ini file using the code below, but I get the following error:

new-variable : A variable with name 'FromConfig' already exists. + new-variable -name $Variable_NME -value $VariableValue_STR + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceExists: (TB1_LKC_PATH:String) [New-Variable], SessionStateException + FullyQualifiedErrorId : VariableAlreadyExists,Microsoft.PowerShell.Commands.NewVariableCommand

$IniFile_NME="$PSScriptRoot\SanityTests\Config\ConfigToParse.ini"

dir $IniFile_NME

########################################
#
# Parse the file
#
########################################


$InputFile = [System.IO.File]::OpenText("$IniFile_NME")

while($InputRecord = $InputFile.ReadLine())
    {
        # Display the current record

        write-host "`$InputRecord=$InputRecord"
        write-host ""

        # Determine the position of the sign (:)

        $Pos = $InputRecord.IndexOf(':')
        write-host "`$Pos=$Pos"

        # Determine the length of the record

        $Len = $InputRecord.Length
        write-host "`$Len=$Len"

        # Parse the record

        $Variable_NME = $InputRecord.Substring(1, $Pos -1)
        $VariableValue_STR = $InputRecord.Substring($Pos + 1, $Len -$Pos -1)

        write-host "`$Variable_NME=$Variable_NME"
        write-host "`$VariableValue_STR=$VariableValue_STR"

        # Create a new variable based on the parsed information

        new-variable -name $Variable_NME -value $VariableValue_STR
        get-variable -name $Variable_NME
    }
$InputFile.Close()

thank you :)

adding the config file:

PROJECT_TO_VALIDATE: J

FW_TESTED: LKC

FW_ALTERNATIVE: BKC

MW_TESTED: LKC

MW_ALTERNATIVE: BKC

TB1_TESTED: BKC

L_LKC_FW_PATH: "PathToFolder"

L_LKC_MW_PATH: "PathToFolder"

L_BKC_PATH: "PathToFolder"

J_LKC_FW_PATH: "PathToFolder"

J_LKC_MW_PATH: "PathToFolder"

J_BKC_PATH: "PathToFolder"

P_LKC_FW_PATH: "PathToFolder"

P_LKC_MW_PATH: "PathToFolder"

P_BKC_PATH: "PathToFolder"

TB1_LKC_PATH: "PathToFolder"

2 Answers 2

3

Well the error pretty well explains what's wrong: you are trying to create a variable which already exists.
So one solution would be to have a look into your .ini file (which you may want to add to your question) if there are duplicate keys and try to get rid of them or just overwrite existing variables with -Force like so

...
New-Variable -Force -Name $Variable_NME -Value $VariableValue_STR
...
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is this line:

new-variable -name $Variable_NME -value $VariableValue_STR

You already have a variable named $variable_nme and in new-variable command you should use -name variable_nme with no $ sign and of course a new name.

2 Comments

This will result in creating variables all named 'variable_nme' which will result in the original error even more quickly as all vars will have the same name
Maybe I didn't make my self clear, Edited the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.