Workflows and type coercion

Hi guys,

I am experimenting with Powershell Workflows and Microsoft SMA and discovered a type coercion issue described here:
https://support.microsoft.com/en-us/kb/2015543

An simple example, which results in mentioned error, would be:

workflow writeGreeting {

    Param(
        [Parameter(Mandatory=$False)]
        [string]$timeOfDay
    )

    if (!$timeOfDay)
    {
        $timeOfDay = getTimeOfDay
    }

    Write-Verbose "Good $timeOfDay" -Verbose

}


workflow getTimeOfDay {

    Write-Output "Morning"

}


writeGreeting

If I am not mistaken this behavior is caused due to the deserialized format of the objects in the workflow, which means that the returned deserialized object (“Morning”) can’t be converted into a [String] object.

I can workaround this limitation by assigning the return object to a undefined variable and then perform a second assignment to the parameter.

if (!$timeOfDay)
{
    $workFlowReturn = getTimeOfDay
    $timeOfDay = $workFlowReturn
}

However, I wonder if this is best practice. What’s your suggested method for this limitation?

The issue with your example is that recursive calling of workloads in not permitted. See https://technet.microsoft.com/en-us/library/jj574123(v=ws.11).aspx

If you change one of the workflows to a function or nest workflow two inside workflow one the validation error will be resolved.

workflow writeGreeting {

    Param(
        [Parameter(Mandatory=$False)]
        [string]$timeOfDay
    )

    if (!$timeOfDay)
    {
        $timeOfDay = getTimeOfDay
    }

    Write-Verbose "Good $timeOfDay" -Verbose

}


function getTimeOfDay {

    Write-Output "Morning"

}


writeGreeting
VERBOSE: [localhost]:Good Morning

Thank you Jonathan, I was not aware of this but in that case my example was not correct.
Because I am not running those workflows in a script but using SMA. So I have one SMA workflow “getTimeOfDay” and another SMA workflow “writeGreeting” and I am just running the writeGreeting workflow. Not sure what SMA does in the background but I assume it will nest the workflows correctly.

Calling workflows within workflows in SMA is supported but I get the above mentioned error - so for me it looks more like a type coercion problem rather than a nesting issue.

I am curious but I do not have a sma enviroment to test in. My thought is that the root cause is the due to the recursive limitation of workflows. But if each workflow is in a individual runbook then SMA should properly structure the workflow for you. Perhaps someone who has a better insight into the dark art of workflows will offer a suggestion.