Powershell 工作流 DynamicActivity 编译器错误“。”预期使用 $Date 变量

Powershell Workflow DynamicActivity Compiler Error '.' Expected Using $Date Variable

通常 powershell 错误会让我继续下去,但这个错误让我陷入了困境。要进行测试,请执行以下代码:

workflow test-date{
    $Date = Get-Date -format yyyyMMddHHmm -Verbose

    Write-Output $Date
}

我得到的错误是:

The workflow 'test-date' could not be started: The following errors were encountered while processing the workflow tree:
'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "Date".
'.' expected.
At line:383 char:21
+                     throw (New-Object System.Management.Automation.ErrorRecord $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.Manageme...etersDictionary:PSBoundParametersDictionary) [], RuntimeException
    + FullyQualifiedErrorId : StartWorkflow.InvalidArgument

真正让我明白的部分是它说它期待一个“。”某处。我不知道它期望在哪里找到“.”。我已经用谷歌搜索了与我的情况无关的错误。我想知道更多关于这个错误的含义,以及为什么我会得到它。如果有人也知道解决方案,那就太好了。

发件人:http://blogs.msdn.com/b/powershell/archive/2012/07/21/new-workflow-makeiteasy-authoring-workflows-using-powershell-extended-syntax.aspx

By default, each command in a workflow is executed with no PowerShell state sharing

不知道错误与此有何关系,但这肯定是个问题,因为

Variables created by one command are not visible to the next command.

所以你应该做的是使用 inlinescript

workflow test-date{
    inlinescript{
        $Date = Get-Date -format "yyyyMMddHHmm" -Verbose
        Write-Output $Date
    }
}

输出:

PS C:\Users\mcameron> test-date
201501231144

我不知道真正的原因,但如果您将变量 $date 重命名为 $something_else,工作流程就可以了。

如:

Workflow Test-Date {
    $aDate = Get-Date Get-Date -format yyyyMMddHHmm
    $New_Date = Get-Date -format yyyyMMddHHmm

    Write-Output $aDate
    Write-Output $New_Date
}

我假设它在转换过程中触发了 .NET 中的关键字。