I have a powershell script that has been working for a long time. But today something happended.
I now get this error: Errormessage
"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"
The code:
$Body = [ordered]@{
"SeriesName" = $SeriesName
"TerminalIndex"=1
"QuantityTypesId"=3
"PeriodType"=4
"Active"="true"
"DaylightSaving"="false"
"Tag"=$Meterpointno,
"TimezoneId"="W. Europe Standard Time"
"UniqueTimeZone"="false"
"ValidPeriods"= [ordered]@{
"GroupName"="SE.EL"
"Source"="EBIX"
"ValidPeriodList"= @(
[ordered]@{
"PeriodStart"=$ValidFrom
"PeriodEnd"=$ValidTo
}
)
}
Is "TimezoneId" now some kind of systemwide variable?
boxdog and you yourself have found the problem: a stray ,
(comma) at the end of "Tag"=$Meterpointno,
While your problem could therefore be considered a mere typo, the resulting symptom is worth explaining:
Because the ,
operator in PowerShell constructs arrays, and a trailing ,
at the end of a line is a syntactically incomplete array-construction operation, parsing continued on the next line, until the operation was complete, which included the entire following line.
That is, what PowerShell tried to assign to the "Tag"
entry of your ordered hashtable was equivalent to the following (reformatted for conceptual clarity):
$Meterpointno, "TimezoneId" = "W. Europe Standard Time"
To PowerShell, this is a two-element array, composed of the value of $Meterpointno
and the string-literal "TimezoneId"
, to which the value of string literal "W. Europe Standard Time"
is to be assigned.
PowerShell does fundamentally support assigning to arrays - a so-called multi-assignment - but each element of the array must be a so-called l-value, i.e. an expression capable of being assigned to (storing a value).
E.g., the following is a valid multi-assignment: $a, $b = 1..3
: it stores 1
in $a
, and @(2, 3)
in $b
.
However, in your case "TimezoneId"
is obviously not an l-value, i.e. it cannot be assigned to, resulting in the error message you saw (which is a verbose way of saying: not an l-value):
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.