locked
Issue using custom class in workflow RRS feed

  • Question

  • I am unable to use a custom class within a workflow. I've brought it down to a small script that illustrates the issue I have. When run as seperate code or a function it works fine, but when run as a workflow it generates an error. Can anyone see what I am doing wrong?

    workflow Test-Workflow{
      #Create class
      class TestClass{
        [string]$FirstName
        [string]$MiddleName
        [string]$LastName
      }
    
      #Test class
      $Test=[TestClass]@{
              FirstName='First'
              MiddleName='middle'
              LastName='Last'
      }
    }
    When run as a workflow it gives the following error:
    Unable to find type [TestClass].
    At line:1 char:4
    + ,([TestClass]@{
    +    ~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (TestClass:TypeName) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : TypeNotFound
        + PSComputerName        : [localhost]



    • Edited by Erik_123 Wednesday, April 1, 2020 1:29 PM
    Tuesday, March 31, 2020 1:23 PM

Answers

  • The syntax is not supported so it must be wrapped in an "inlinescript":

    workflow Test-Workflow{
      inlinescript { 
            class TestClass{
            [string]$FirstName
            [string]$MiddleName
            [string]$LastName
            }
            $test = [TestClass]@{
              FirstName='First'
              MiddleName='middle'
              LastName='Last'
            }
        }
    }


    \_(ツ)_/

    • Proposed as answer by Sokratissz Wednesday, April 1, 2020 3:05 PM
    • Marked as answer by Erik_123 Thursday, April 2, 2020 11:03 AM
    Wednesday, April 1, 2020 2:26 PM

All replies

  • Please edit your post and fix the code:


    \_(ツ)_/

    Tuesday, March 31, 2020 1:49 PM
  • Done
    Wednesday, April 1, 2020 7:54 AM
  • What is wrong?  What is the error?  The code works as written.


    \_(ツ)_/

    Wednesday, April 1, 2020 8:25 AM
  • I've added the error.

    Wednesday, April 1, 2020 1:30 PM
  • This works fine in PowerShell 5.1.  It will not work in PS 5 or earlier.


    \_(ツ)_/

    Wednesday, April 1, 2020 1:51 PM
  • This is the environment I am running in:

    Name                           Value         
    ----                           -----              
    PSVersion                      5.1.18362.752         
    PSEdition                      Desktop                  
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}    
    BuildVersion                   10.0.18362.752                 
    CLRVersion                     4.0.30319.42000             
    WSManStackVersion              3.0                         
    PSRemotingProtocolVersion      2.3                    
    SerializationVersion           1.1.0.1                  
    • Edited by Erik_123 Wednesday, April 1, 2020 2:10 PM
    Wednesday, April 1, 2020 2:09 PM
  • Ok.  I can reproduce the failure now.  


    \_(ツ)_/

    Wednesday, April 1, 2020 2:22 PM
  • The syntax is not supported so it must be wrapped in an "inlinescript":

    workflow Test-Workflow{
      inlinescript { 
            class TestClass{
            [string]$FirstName
            [string]$MiddleName
            [string]$LastName
            }
            $test = [TestClass]@{
              FirstName='First'
              MiddleName='middle'
              LastName='Last'
            }
        }
    }


    \_(ツ)_/

    • Proposed as answer by Sokratissz Wednesday, April 1, 2020 3:05 PM
    • Marked as answer by Erik_123 Thursday, April 2, 2020 11:03 AM
    Wednesday, April 1, 2020 2:26 PM
  • Since I require several sequential processes to run in parallel, using the same custom class based array, I've had to create the class in every inlinescript part and at workflow level. Filling the array at workflow level with the returns from the inlinescripts. So far it looks to work.
    Thursday, April 2, 2020 11:03 AM