# # Name: WFA command to get the XML 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) 2015 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_) + ".xml" #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 [xml]$xmlDoc = New-Object system.Xml.XmlDocument $xmlDoc.LoadXml('') $ele1 = $xmlDoc.CreateElement("userInputValues", $xmlDoc.DocumentElement.NamespaceURI) $index = 0 foreach ( $key in $keys ) { $key = $key.Split("$")[1] $ele= $xmlDoc.CreateElement("userInputEntry", $xmlDoc.DocumentElement.NamespaceURI) $attr1 = $xmlDoc.CreateAttribute("key") $attr1.Value = $key $attr2 = $xmlDoc.CreateAttribute("value") $attr2.Value = $($values[$index]) $ele.Attributes.Append($attr1) $ele.Attributes.Append($attr2) $ele1.AppendChild($ele) $index++ } $xmlDoc.LastChild.AppendChild($ele1) if($data.job.jobStatus.scheduleType -eq "Delayed") { $executionDateAndTime = $data.job.jobStatus.plannedExecutionTime $ele3 = $xmlDoc.CreateElement("executionDateAndTime", $xmlDoc.DocumentElement.NamespaceURI) $xmlSubText = $xmlDoc.CreateTextNode($executionDateAndTime) $ele3.AppendChild($xmlSubText) $xmlDoc.LastChild.AppendChild($ele3) } if ($data.job.jobStatus.comment) { $comment = $data.job.jobStatus.comment $ele2 = $xmlDoc.CreateElement("comments", $xmlDoc.DocumentElement.NamespaceURI) $xmlSubText = $xmlDoc.CreateTextNode($comment) $ele2.AppendChild($xmlSubText) $xmlDoc.LastChild.AppendChild($ele2) } Get-WfaLogger -Info -Message $xmlDoc.OuterXml Get-WfaLogger -Info -Message "Saving the file $xmlFileName" $xmlDoc.Save($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" }