Environment Variables in a Workflow

Welcome Forums General PowerShell Q&A Environment Variables in a Workflow

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 year, 2 months ago.

  • Author
    Posts
  • #98590

    Participant
    Topics: 5
    Replies: 0
    Points: 13
    Rank: Member

    I'm trying to use environment variables in a workflow:

    workflow foobar
    {
        [CmdletBinding()]
        Param
        (
            [string]$Path = "$env:HOMEDRIVE\Test\Files"
        )
    }
    

    Running this workflow returns the error:

    "In a windows PowerShell workflow, parameter defaults may only be simple value types (such as integers) and strings. In addition, the type of the default value must match the type of the parameter."

    If I change the parameter to:

    C:\Test\Files

    That is a solution. But is there a way to use environment variables (in the param block), in a workflow?

    Homedrive might not always be C: in my environment.

  • #98593

    Participant
    Topics: 18
    Replies: 245
    Points: 114
    Helping Hand
    Rank: Participant

    Not sure what you are trying to achieve but you could use an inline script block inside your workflow like below :-

    $HomeDrive = "$env:HOMEDRIVE"
    
    workflow foobar
    {
        [CmdletBinding()]
        Param
        (
            [string]$HomeDrivePath="c:\temp"
        )
        inlinescript {
    
      foreach ($Drive in $using:HomeDrivePath) {
    
      Test-Path -Path $Drive
    
      }
      }
    }
    foobar -HomeDrivePath $HomeDrive
    

The topic ‘Environment Variables in a Workflow’ is closed to new replies.