ptallon
ptallon

Reputation: 13

Variable substitution on left hand side of assignment - PowerShell

I am trying to extract the value of a variable on the left-hand side of an assignment statement to use as a new variable in PowerShell v4.0. A brief example follows:

    $ptypeCtr = $ptypeCtr + 1; 
    $prow = ('$partyRow' + $ptypeCtr);
    "$prow"  += "case_ref=" + $key;

This does not work and throws an exception stating the following:

At line:3 char:1
+ "$prow" = "case_ref=" +key;
+ ~~~~~~~

The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.

+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidLeftHandSide

I have also tried using $($prow) and $("$prow") but this just still returns the same error message.

What I was hoping to achieve was that the $prow variable would be evaluated as $partyRow1 and the be assigned the value "case_ref=123".

Has anybody got any suggestions or ideas about how to achieve this in Powershell?

Upvotes: 1

Views: 3165

Answers (1)

mjolinor
mjolinor

Reputation: 68341

If I'm reading the question correctly, I think this might do it:

Set-Variable -Name $prow -Value "case_ref=$key"

Upvotes: 4

Related Questions