Run workflow from powershell using webservices

by Occasional Contributor on ‎2013-04-03 11:23 AM - edited on ‎2014-09-25 03:51 PM by allison Former NetApp Employee

<#

.SYNOPSIS

   Calls a workflow from WFA

   Input is detected automatically

 

.VERSION

   2013-02-04  

 

.

#>

##

# DATE: 04/02/2013

# AUTHOR: Mirko Van Colen

##

 

# variables

$username = "Username"

$password = "Password"

$wfaserver = "127.0.0.1"

$uri = "http://$wfaserver/wfa-ws/WorkflowService_rpc?wsdl"

 

$parameters = @()

 

#$VerbosePreference = "continue"

 

# create credentials

Write-Verbose "Creating credentials"

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

 

# fire up the webservice

Write-Verbose "Connect to webservice $uri"

$wfa = New-WebServiceProxy -Uri $uri -Credential $cred

 

# get list of workflows

Write-Verbose "Get the workflows"

$workflows = $wfa.getAllWorkflows()

 

# list them up

Write-Output "Pick a workflow"

Write-Output "==============="

 

$workflowIds = @() # build a valid workflowid array

 

foreach($workflow in $workflows){

    $workflowIds += $workflow.id

    Write-Output "$($workflow.id) : $($workflow.name)"

}

 

# prompt for workflow id with error checking

do{

    $workflowId = read-host "Enter a valid Workflow-id you want to start"

}

while($workflowIds -notcontains $workflowId)

 

# get chosen workflow

Write-Verbose "Get workflow $workflowid"

$workflow = $workflows | where{$_.id -eq $workflowId}

 

# get user input

Write-Verbose "Extract userput fields"

$userinputFields = $workflow | select -ExpandProperty userinput

foreach($field in $userinputFields){

    $input = ""

    $validationString = ""

    Do{

        Write-Output "-----------------------------------"

        # description

        if($field.description){

            Write-Output "Description : $($field.description)"

        }

        # validation presentation

        if($field.value){

                $validationString = $field.value

                # visualize Enum with "|"

                if($field.type -eq "Enum"){

                    $validationString = "[" + ($field.value -join "|") + "]"

                }

                Write-Output "Validation : $validationString"

        }

        # get default value if any

        if($field.defaultValue){

            $defaultValue = " [$($field.defaultValue)]"

        }else{

            $defaultValue = ""

        }

        # read the input

        $input = read-host "$($field.name)$defaultValue"

 

        # set the default value

        if((-not $input) -and $field.defaultValue){

            Write-Verbose "No input given : defaulting the value to $($field.defaultValue)"

            $input = $field.defaultValue

        }

 

        # validate the input

 

        # default = true

        $validation = $true

 

        # only validation if value is given

        if($field.value){

 

            # number - scan the range

            if($field.type -eq "Number"){

                $range = $validationString -split "-"

                $input = [Int32]$input

                $lowest = [Int32]$range[0].Trim()

                $highest = [Int32]$range[1].Trim()

                $validation = (($input -ge $lowest) -and ($input -le $highest))

                Write-Verbose "Validated number $input as $validation between $lowest and $highest"

            }

 

            # enumeration - check the array

            if($field.type -eq "Enum"){

                $validation = $field.value -contains $input

                Write-Verbose "Validated enumeration $input as $validation in array {$($field.value -join ",")}"

            }

 

            # string - check the regex

            if($field.type -eq "String"){

                # we change the regex a bit (^ & $) to get it working

                $validation = $input -match "^$validationString$"

                Write-Verbose "Validated string $input as $validation against regex /$validationString/"

            }

        }

 

    }while(-not $validation)

    Write-Verbose "Adding parameters : $($field.name)=$input"

    $parameters += "$($field.name)=$input"

}

 

 

# lauch the workflow

Write-Verbose "Launching the workflow $($workflow.id) with parameters $parameters"

$jobid = $wfa.executeWorkflow($workflow.id,$parameters)

 

# monitor job status

 

do{

    $status = $wfa.getJobStatus($jobid)

    Write-Output "Workflow status : $($status.jobStatus1)..."

    Write-Verbose "Sleeping 2 seconds"

    Start-Sleep 2

}

while($status.jobStatus1 -ne "COMPLETED")

Write-Verbose "Job completed"

 

write-output "returning parameters through the job status"

foreach($param in $status.returnParameter){

    write-output "$($param.name) = $($param.value)"

}

 

write-output "returning parameters through webservice"

 

foreach($param in $workflow.returnParameters){

    $returnedParameter = $wfa.getReturnParameters($jobid,$param.name)

    write-output "$($returnedParameter[0].name) = $($returnedParameter[0].value)"

}

 

$stop = Read-Host 'job finished... press enter to stop'

Warning!

This NetApp Community is public and open website that is indexed by search engines such as Google. Participation in the NetApp Community is voluntary. All content posted on the NetApp Community is publicly viewable and available. This includes the rich text editor which is not encrypted for https.

In accordance to our Code of Conduct and Community Terms of Use DO NOT post or attach the following:

  • Software files (compressed or uncompressed)
  • Files that require an End User License Agreement (EULA)
  • Confidential information
  • Personal data you do not want publicly available
  • Another’s personally identifiable information
  • Copyrighted materials without the permission of the copyright owner

Files and content that do not abide by the Community Terms of Use or Code of Conduct will be removed. Continued non-compliance may result in NetApp Community account restrictions or termination.