Understanding scope of functions in powershell workflow

前端 未结 4 2082
情歌与酒
情歌与酒 2020-12-16 16:13

Copy and paste the following into a new Powershell ISE script and hit F5:

workflow workflow1{
    \"in workflow1\"
    func1
}
function func1 {
    \"in func         


        
4条回答
  •  抹茶落季
    2020-12-16 17:00

    Think of Workflows as short-sighted programming elements.

    A Workflow cannot see beyond what's immediately available in the scope. So nested functions are not working with a single workflow, because it cannot see them.

    The fix is to nest workflows along with nested functions. Such as this:

    workflow workflow1
    {
        function func1 
        {
            "in func1"
            workflow workflow2
            {
                function func2 
                {
                    "in func2"
                }
                func2
            }
            "in workflow2"
            workflow2
        }
        "in workflow1"
        func1
    }
    workflow1
    

    Then it sees the nested functions:

    in workflow1
    in func1
    in workflow2
    in func2
    

    More about it here

提交回复
热议问题