Julia
Julia

Reputation: 145

Powershell parse error

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"

Upvotes: 3

Views: 1724

Answers (2)

Farhad Farahi
Farhad Farahi

Reputation: 39457

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.

Upvotes: 0

DAXaholic
DAXaholic

Reputation: 35398

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
...

Upvotes: 3

Related Questions