Scripts/Add-BPAWorkflowVariable.ps1

function Add-BPAWorkflowVariable {
    <#
        .SYNOPSIS
            Adds a shared variable to a BPA workflow
 
        .DESCRIPTION
            Add-BPAWorkflowVariable can add shared variables to a workflow object.
 
        .PARAMETER InputObject
            The object to add the variable to.
 
        .PARAMETER Name
            The name of the variable.
 
        .PARAMETER InitialValue
            The initial value of the variable.
 
        .PARAMETER Description
            The description of the variable.
 
        .PARAMETER DataType
            The data type of the variable.
 
        .PARAMETER PassValueFromParent
            If specified, the variable will be configured to pass the value from the parent workflow to this workflow.
 
        .PARAMETER PassValueToParent
            If specified, the variable will be configured to pass the value from this workflow to the parent workflow.
 
        .INPUTS
            The following BPA object types can be modified by this function:
            Workflow
 
        .EXAMPLE
            # Add variable 'emailAddress' to workflow 'Some Workflow'
            Get-BPAWorkflow "Some Workflow" | Add-BPAWorkflowVariable -Name "emailAddress" -InitialValue "person@example.com" -Description "Email this user when the job fails"
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 06/27/2017
            Date Modified : 02/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [ValidateNotNull()]
        $InitialValue = "",

        [ValidateNotNull()]
        [string]$Description = "",

        [BPAWorkflowVarDataType]$VariableType = [BPAWorkflowVarDataType]::Variable,

        [ValidateNotNullOrEmpty()]
        [BPAWorkflowVarType]$DataType = [BPAWorkflowVarType]::Auto,

        [ValidateNotNullOrEmpty()]
        [switch]$PassValueFromParent = $false,

        [ValidateNotNullOrEmpty()]
        [switch]$PassValueToParent = $false
    )
    BEGIN {
        switch ($VariableType) {
            {($_ -in @("Array","Dataset"))} {
                # Ignore values that don't apply to arrays or datasets
                $InitialValue = ""
                $DataType = [BPAWorkflowVarType]::Auto
                $PassValueFromParent = $false
                $PassValueToParent = $false
            }
        }
    }

    PROCESS {
        foreach ($obj in $InputObject) {
            if ($obj.TypeName -eq "Workflow") {
                # Get the latest version of the object. Get-BPAWorkflow by ID also returns an object from workflows/<id>/get, which is in the correct format for updating.
                $updateObject = Get-BPAWorkflow -ID $obj.ID -BPAServer $obj.BPAServer
                $shouldUpdate = $false

                # Get the template object from the PoshBPA\ObjectTemplates folder, and configure the object
                $variable = Get-BPAObjectTemplate -Type "WorkflowVariable" -BPAServer $obj.BPAServer
                $variable.ID           = "{$((New-Guid).Guid)}"
                $variable.Name         = $Name
                $variable.ParentID     = $updateObject.ID
                $variable.DataType     = $VariableType.value__
                $variable.Description  = $Description
                $variable.InitalValue  = $InitialValue
                $variable.Parameter    = $PassValueFromParent.ToBool()
                $variable.Private      = $PassValueToParent.ToBool()
                $variable.VariableType = $DataType.value__

                if ($updateObject.Variables.Name -notcontains $Name) {
                    $updateObject.Variables += $variable
                    $shouldUpdate = $true
                }
                if ($shouldUpdate) {
                    Set-BPAWorkflow -Instance $updateObject
                } else {
                    Write-Verbose "$($obj.TypeName) '$($obj.Name)' already contains the specified values."
                }
            } else {
                Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
            }
        }
    }
}