# # Name: WFA command to get the JSON body which can be used for executing this workflow via REST apis. # Version: 1.0.0 # From: NetApp Inc. # Min WFA version needed: WFA2.2 # Min Powershell : 3.0 # Copyright (C) 2018 NetApp, Inc. All rights reserved. # # Email: sinhaa@netapp.com #---Check for PoSH version. It should be >=3.0 $poshCheck = $true if ($PSVersionTable.PSVersion.Major -le '2') { $poshCheck = $false throw("Minimun PowerShell version required is 3.0") } #------ Get the current workflow UUI and the Current Job ID $myWfaCreds = Get-WfaCredentials -Host "localhost" if (!$myWfaCreds) { throw("No credentials added for localhost") } $workflowName = Get-WfaRestParameter "workflowName" $jobId = Get-WfaRestParameter "jobId" if ( $workflowName -eq "DUMMY_WORKFLOW_NAME_FOR_TEST") { Get-WfaLogger -Warn -Message "This command should only be executed in a workflow. Returning now." return } New-Item -ItemType Directory -Path C:\temp -ErrorAction SilentlyContinue $xmlFileName = "C:\temp\" + $workflowName + $(Get-Date -Format __dd_MM_yyyy_hh_mm_ss_) + ".json" #Get the WFA http port $REGISTRY = "HKLM:\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\NA_WFA_SRV\Parameters\Java" $httpPort = (Get-ItemProperty $REGISTRY |select -ExpandProperty Options|where {$_ -match "-Dhttp.port"}).split("=")[1] [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $Url= 'http://localhost:' + $httpPort + '/rest/workflows?name=' + $WorkflowName #Get-WfaLogger -Info -Message $Url #Get UUID of the workflow $uuid=(Invoke-RestMethod -Method get -Uri $url -Credential $myWfaCreds).collection.workflow.uuid #Get-WfaLogger -Info -Message $uuid # Now get the current Job' details. You need user inputs. $Url2="http://localhost:" + $httpPort +"/rest/workflows/"+ $uuid + "/jobs/" + $jobId #Get-WfaLogger -Info -Message $Url2 [xml]$data=Invoke-RestMethod -Method get -Uri $Url2 -Credential $myWfaCreds -ContentType "application/xml" [Array]$keys = $data.job.jobStatus.userInputValues.userInputEntry.key [Array]$values = $data.job.jobStatus.userInputValues.userInputEntry.value $body = [ordered]@{} ### Build User Input Values $UserInputArray = @() $index = 0 foreach ( $key in $keys ) { $inputHash = @{} $key = $key.Split("$")[1] $inputHash["key"] = $key $inputHash["value"] = $($values[$index]) $UserInputArray +=$inputHash $index++ } $body['userInputValues'] = $UserInputArray ##Execution Date and Time if($data.job.jobStatus.scheduleType -eq "Delayed") { $executionDateAndTime = $data.job.jobStatus.plannedExecutionTime $body['executionDateAndTime'] = $executionDateAndTime } if ($data.job.jobStatus.comment) { $comment = $data.job.jobStatus.comment $body['comments'] = $comment } $jsonBody = $body |ConvertTo-Json Get-WfaLogger -Info -Message $jsonBody Get-WfaLogger -Info -Message "Saving the file $xmlFileName" $jsonBody | Out-File $xmlFileName throw("All done. The Workflow will fail now.") trap { if(!$poshCheck) { throw "Minimun PowerShell version required is 3.0" } Get-WFALogger -Info -message "Clearing the Reservation on this job for the Failed Command and the command which haven't been executed." #Prepare the new URL to clean reservation $Url3="http://localhost:" + $httpPort +"/rest/workflows/"+ $uuid + "/jobs/" + $jobId +"/reservation" #Invoke REST api. Invoke-RestMethod -Method Delete -Uri $Url3 -Credential $myWfaCreds Get-WfaLogger -Info -Messag "Reservation cleaned successfully" }