VCF Automation Blog

from Stefan Schnell

PowerShell programming language is type-agnostic, this means that it automatically decides at runtime which data type a variable used. But PowerShell offers the possibility to use type-constrained variables, to determine which data type can be assigned to a variable. This raised the question whether cast notation can also be used in the context of VCF Automation. This will be considered in this post.

Using Cast Notation in PowerShell Runtime Environment


Cast notation can be used seamlessly. The standard PowerShell handler function of VCF Automation looks like this with cast notation:

function handler([Object]$context, [Object]$inputs) {
    [OutputType([System.Collections.Hashtable])]

    [System.String]$inputsString = $inputs | ConvertTo-Json -Compress
    Write-Host "Inputs were $($inputsString)"

    [System.Collections.Hashtable]$outputs = @{
        status = "done"
    }

    return $outputs
}

vcf automation example usage of powershell cast notation
A slightly updated standard PowerShell handler could look like this:

function handler {
    [OutputType([System.Collections.Hashtable])]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [Object]$context,
        [Parameter(Mandatory = $false, Position = 1)]
        [Object]$inputs
    )

    [System.Collections.Hashtable]$result = @{}
    [System.Collections.Hashtable]$outputs = @{}

    try {

        [System.String]$vcoUrl = $context.vcoUrl
        [System.String]$bearerToken = $context.getToken()

        [System.String]$inputsString = $inputs | ConvertTo-Json -Compress
        Write-Host "Inputs were $($inputsString)"



        $outputs = @{
            status = "done"
            error = $null
            result = $result
        }

    } catch {

        $outputs = @{
            status = "incomplete"
            error = $_
            result = $null
        }

    }

    return $outputs

}

vcf automation example usage of powershell cast notation

Conclusion

Cast notation can be used in the PowerShell Runtime Environment of VCF Automation without any problems.