# # Name: WFA command for Recurring Execution of a workflow # Version: 1.1.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 param ( [parameter(Mandatory=$false, HelpMessage="Minutes Interval")] [int]$minsInterval, [parameter(Mandatory=$false, HelpMessage="Hours Interval. Provide value 1 for Hourly execution.")] [int]$hoursInterval, [parameter(Mandatory=$false, HelpMessage="Days Interval. Provide value 1 for daily execution.")] [int]$daysInterval, [parameter(Mandatory=$false, HelpMessage="Weeks Interval. Provide value 1 for weekly execution.")] [int]$weeksInterval, [parameter(Mandatory=$false, HelpMessage="Months Interval. Provide value 1 for montly execution.")] [int]$monthsInterval, [parameter(Mandatory=$false, HelpMessage="Years Interval. Provide value 1 for yearly execution.")] [int]$yearsInterval, [parameter(Mandatory=$false, HelpMessage="Years Interval. Provide value 1 for yearly execution.")] [Datetime]$expiryDate ) if (!$minsInterval -and !$hoursInterval -and !$daysInterval -and !$weeksInterval -and !$monthsInterval -and !$yearsInterval) { Get-WfaLogger -Warn -Message "No parameter selected for recurring execution. Returning Now!." return } #--- Check that only 1 scheduling parameter is provided. if ($PSBoundParameters.Count -eq 2) { if(!$expiryDate) { Get-WfaLogger -Warn -Message "Please provide only1 parameter for recurring schedule . Returning Now!" return } } if($PSBoundParameters.Count -ge 3) { Get-WfaLogger -Warn -Message "Please provide only 1 parameter for recurring schedule other than Expiry Date. Returning Now!" return } #---Check for PoSH version. It should be >=3.0 if ($PSVersionTable.PSVersion.Major -le '2') { 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" Get-WfaLogger -Info -Message $workflowName Get-WfaLogger -Info -Message $jobId #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] $httpsPort = (Get-ItemProperty $REGISTRY |select -ExpandProperty Options|where {$_ -match "-Dhttps.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" $keys = $data.job.jobStatus.userInputValues.userInputEntry.key $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) #### Get next execution date and time if($minsInterval) { $newDate = (get-date).AddMinutes($minsInterval) } if($hoursInterval) { $newDate = (get-date).AddHours($hoursInterval) } if($daysInterval) { $newDate = (get-date).AddDays($daysInterval) } if($weeksInterval) { $totalDays = $weeksInterval * 7 $newDate = (get-date).AddDays($totalDays) } if($monthsInterval) { $newDate = (get-date).AddMonths($monthsInterval) } if($yearsInterval) { $newDate = (get-date).AddYears($yearsInterval) } if($expiryDate) { $expr= $expiryDate.GetDateTimeFormats("d")[3] if( $newDate.GetDateTimeFormats("d")[3] -ge $expr ) { #$expr= $expiryDate.GetDateTimeFormats("d")[3] Get-WfaLogger -Info -Message "Recurring Execution has reached the exipry date $expr. Stopping now!!" return } } $newWfaDate = $newDate.GetDateTimeFormats("g")[0] Get-WfaLogger -Info -Message $newWfaDate #### $ele3 = $xmlDoc.CreateElement("executionDateAndTime", $xmlDoc.DocumentElement.NamespaceURI) $xmlSubText = $xmlDoc.CreateTextNode($newWfaDate) $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 ####### ----- Now invoke the workflow with delayed execution $Url3= "https://localhost:" + $httpsPort +"/rest/workflows/"+ $uuid + "/jobs" Get-WfaLogger -Info -Message $Url3 $res=Invoke-RestMethod -Method post -Uri $Url3 -Body $xmlDoc.OuterXml -Credential $myWfaCreds -ContentType "application/xml" Get-WfaLogger -Info -Message "Shchduled for next execution at $newWfaDate"