Updates the Step Templates used on Deployment Processes to the latest versions
   Step templates can be updated from the library on Octopus, but that doesnt mean that the Deployment processes using that template will start using the latest version right away. Normally, the user would have to update the step template on each deployment process manually. This script takes care of that.
   Update-StepTemplatesOnDeploymentProcesses -ActionTemplateID "ActionTemplates-3" -OctopusURI "http://localhost" -APIKey "API-RLMWLZBPMX5DRPLCRNZETFS4HA"
   Update-StepTemplatesOnDeploymentProcesses -AllActionTemplates -OctopusURI "" -APIKey "API-TSET42BPMX5DRPLCRNZETFS4HA"
   Github project:
   QA and Cmdlet request:

Function Update-OctopusStepTemplates
        # Action Template ID. Use when you only want to update the deployment processes that only use this Action Template.
        [Parameter(Mandatory=$true,ParameterSetName= "SingleActionTemplate")]

        # If used, all the action templates will be updated on all the deployment processes.
        #[Parameter(Mandatory=$true, ParameterSetName= "MultipleActionTemplates")]

        # Octopus instance URL
        [string]$OctopusURI = $env:OctopusURI,

        # Octopus API Key. How to create an API Key =
        [string]$APIKey = $env:OctopusApiKey        


        Add-Type -Path "$PSScriptRoot\..\bin\Newtonsoft.Json.dll"
        Add-Type -Path "$PSScriptRoot\..\bin\Octopus.Client.dll"
        Add-Type -Path "$PSScriptRoot\..\bin\Octopus.Platform.dll"

        #Connection Data
        $headers = @{"X-Octopus-ApiKey"="$($apikey)";}

        #Create endpoint connection
        $endpoint = new-object Octopus.Client.OctopusServerEndpoint "$($OctopusURI)","$($apikey)"
        $repository = new-object Octopus.Client.OctopusRepository $endpoint

        $template = Invoke-WebRequest -Uri $OctopusURI/api/actiontemplates/$ActiontemplateID -Method Get -Headers $headers | select -ExpandProperty content| ConvertFrom-Json
        $usage = Invoke-WebRequest -Uri $OctopusURI/api/actiontemplates/$ActiontemplateID/usage -Method Get -Headers $headers | select -ExpandProperty content | ConvertFrom-Json

        #Getting all the DeploymentProcesses that need to be updated
        $deploymentprocesstoupdate = $usage | ? {$_.version -ne $template.Version}

        write-host "Template: $($" -ForegroundColor Magenta

        If($deploymentprocesstoupdate -eq $null){

            Write-host "`t--All deployment processes up to date" -ForegroundColor Green



            Foreach($d in $deploymentprocesstoupdate){

                Write-host "`t--Updating project: $($d.projectname)" -ForegroundColor Yellow

                #Getting DeploymentProcess obj
                $process = $repository.DeploymentProcesses.Get($d.DeploymentProcessId)

                #Finding the step that uses the step template
                $step = $process.Steps | ?{$ -eq $template.Id}


                    foreach($prop in ${

                        $$prop = $template.Properties.$prop
                        #Updating the Template.Version property to the latest
                        $'Octopus.Action.Template.version' = $template.Version

                            Write-host "`t--Project updated: $($d.projectname)" -ForegroundColor Green


                catch [System.InvalidOperationException]{
                    #Catching and error caused by modifying the same collection evaluated on the foreach
                    #Feel free to add a comment proposing a cleaner fix

                    Write-Error "Error updating Process template for Octopus project: $($d.projectname)"
                    write-error $_.Exception.message