Powershell 脚本不再工作,错误“赋值表达式无效。”

问题描述 投票:0回答:1

我有一个已经运行了很长时间的powershell脚本。但今天发生了一些事情。

我现在收到此错误: Errormessage

“赋值表达式无效。赋值运算符的输入必须是能够接受赋值的对象,例如变量或属性”

代码:

        $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
                        }
                    )
                }

“TimezoneId”现在是某种系统范围的变量吗?

powershell syntax hashtable variable-assignment
1个回答
0
投票

boxdog,你自己也发现了问题:

,
末尾有一个迷失的
"Tag"=$Meterpointno,

(逗号)

虽然您的问题可能被认为只是一个拼写错误,但由此产生的症状值得解释:

因为 PowerShell 中的

,
运算符 构造数组,而尾随的
,
是语法上不完整的数组构造操作,因此解析会在下一行继续,直到操作完成,其中包括整个下一行。

也就是说,PowerShell 尝试分配给您订购的

hashtable
"Tag" 条目相当于以下内容(为了概念清晰而重新格式化):

$Meterpointno, "TimezoneId" = "W. Europe Standard Time"

对于 PowerShell,这是一个二元素数组,由

$Meterpointno
的值和字符串文字
"TimezoneId"
组成,要为其分配字符串文字
"W. Europe Standard Time"
的值

PowerShell 从根本上支持对数组进行赋值 - 所谓的 多重赋值 - 但数组的每个元素必须是所谓的 l-value,即能够进行赋值的表达式 (存储值)。 例如,以下是有效的多重赋值:
$a, $b = 1..3

:它将 
1
 存储在 
$a
 中,将 
@(2, 3)
 存储在 
$b
 中。

但是,在您的情况下

"TimezoneId"

 显然不是 
左值,即它不能被分配给,导致您看到错误消息,这本质上抱怨分配的目标不是左值价值(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.)
    

© www.soinside.com 2019 - 2024. All rights reserved.