Powershell Workflow DynamicActivity Compiler Error ‘.’ Expected Using $Date Variable


Powershell Workflow DynamicActivity Compiler Error ‘.’ Expected Using $Date Variable

Usually powershell errors give me something to go on, but this one has thrown me through a loop. To test, execute the following code:
workflow test-date{
$Date = Get-Date -format yyyyMMddHHmm -Verbose

Write-Output $Date

The error I get is:
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

The part that really get’s me is that it says it’s expecting a “.” somewhere. I don’t know where it’s expecting to find a ‘.’. I’ve googled the error which didn’t produce anything relevant to my situation. I’d like to know more about what this error means, and why I’m getting it. It’d be nice if someone knows the solution too.


Solution 1:

I don’t know the actual cause, but if you rename the variable $date to $something_else, the workflow works.

Such as:

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

    Write-Output $aDate
    Write-Output $New_Date

I assume it’s triggering a keyword in .NET during the conversion.

Solution 2:

From : 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

Don’t know how the error pertains to this but this would certainly be an issue since

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

So what you should have to do is use an inlinescript block

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


PS C:\Users\mcameron> test-date