Workflow variable restrictions

Hello,

 

I wonder if someone can help me as I’m confused with the how the output of the following produces what it does. I presume that this is something to do with scopes.

WORKFLOW TEST1 {
$a = 42
Sequence {$b=$a;
$workflow:c = $a
}
“variable a is: [$a]”
“variable b is: [$b]”
“variable c is: [$c]”
}

 

When I run the above, this is the result I get:

PS C:> Test1
variable a is: [42]
variable b is: []
variable c is: [42]

Why is b empty?

Many thanks, Darren

 

 

 

In my understanding. yes it its because of scoping.

Hi kvprasoon,

thank you for your response.

Can you talk me through it please as I’m a bit confused.

Many Thanks, Darren

Scopes are not a thing limited to workflows. Scope affects everything. It’s fully documented in the help files and of course all over the web in general.

about_Scopes

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-6
 

Controlling the Scope of Variables

https://blogs.msdn.microsoft.com/powershell/2007/04/14/controlling-the-scope-of-variables

 


Understanding scope of functions in powershell workflow

https://social.technet.microsoft.com/Forums/office/en-US/6a0b36e6-6fc6-4c46-9792-5d088213ef19/understanding-scope-of-functions-in-powershell-workflow
WorkFlow
https://docs.microsoft.com/en-us/powershell/module/psworkflow/about/about_workflows

https://docs.microsoft.com/en-us/system-center/sma/overview-powershell-workflows?view=sc-sma-1807

https://blogs.technet.microsoft.com/heyscriptingguy/2012/12/26/powershell-workflows-the-basics

https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/09/powershell-workflows-nesting


 

 

Anything declared at the top level of the workflow is persisted and is considered “global” to that workflow. You’ve done that with the $workflow: modifier as well. Anything inside an activity is scoped to that activity, such as a Sequence.

Workflows are not PowerShell per se; they run under WWF. So you’re dealing with a whole different set of rules, and they’re not always readily apparent. It’s why I dislike Workflow quite a bit.