How to use WaitForCompletion in Octopus.Client library (powershell)

Hi Octopus team,

I am using Octopus.client library to automate sending email when certain projects are being deployed.

according this this link, there is a function repository.Tasks.WaitForCompletion(task) to wait for the tasks to be completed.

However I am trying this in powershell
$taskId = $deploymentCreation.TaskId
$task = $repository.Tasks.Get($taskId)
$repository.Tasks.WaitForCompletion($task)

but getting an exception error:
Index was outside the bounds of the array.
At line:1 char:1

  • $repository.Tasks.WaitForCompletion($task)
  •   + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
      + FullyQualifiedErrorId : System.IndexOutOfRangeException
    
    

Can you show me how to call this function properly, thanks.

Hi Jian,

I’ve just tested the below PowerShell script successfully on a local instance:

Add-Type -Path <PathTo>\Octopus.Client.dll

$apikey = 'API-xxx' # Get this from your profile
$octopusURI = 'http://localhost' # Your Octopus Server address

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$environment = $repository.Environments.FindByName("Dev")
$project = $repository.Projects.FindByName("Test")
$process = $repository.DeploymentProcesses.Get($project.DeploymentProcessId)
$channel = $repository.Channels.FindByName($project,"Default") #Provide a valid channel
$template = $repository.DeploymentProcesses.GetTemplate($process,$channel)

$release = new-object Octopus.Client.Model.ReleaseResource
$release.Version = $template.NextVersionIncrement
$release.ProjectId = $project.Id

$newRelease = $repository.Releases.Create($release, $true)
$environmentId = $environment.Id

$deployment = new-object Octopus.Client.Model.DeploymentResource
$deployment.ReleaseId = $newRelease.Id
$deployment.ProjectId = $newRelease.ProjectId
$deployment.EnvironmentId = $environmentId

$created = $repository.Deployments.Create($deployment)
$task = $repository.Tasks.Get($created.TaskId)
$repository.Tasks.WaitForCompletion($task)
$completed = $repository.Tasks.Get($task.Id)
if(-not $task.State -eq [Octopus.Client.Model.TaskState]::Success) {
	Write-Host "One or more projects did not complete successfully"
} else {
    Write-Host "Release deployed successfully!"
}

I hope that helps.

Thank you and best regards,
Henrik

Hi Henrik,

Thanks for getting back to me. What I did is pretty much same as yours, but still getting the error, below is my full scripts, hope you can find anything that I missed.

Add-Type -Path ‘C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll’
$OctoExeDir = ‘C:\Program Files\OctopusTools.4.31.1’

$apikey = ‘API-key’ # Get this from your profile
$octopusURI = ‘http://myoctopus/octopus/’ # Your server address

cd $OctoExeDir
$result = .\Octo.exe create-release --project 'Test Email 2'
–server $octopusURI --apiKey $apiKey
–channel ‘Default’ `
–ignoreexisting | Out-String

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$environment = $repository.Environments.FindByName(‘DEV’)
$project = $repository.Projects.FindByName(“Test Email 2”)

$releases = $repository.Projects.GetReleases($project)
$latestRelease = $releases.Items | Select-Object -first 1

$deployment = new-object Octopus.Client.Model.DeploymentResource
$deployment.ReleaseId = $latestRelease.Id
$deployment.ProjectId = $project.ProjectId
$deployment.EnvironmentId = $environment.Id

$created = $repository.Deployments.Create($deployment)
$task = $repository.Tasks.Get($created.TaskId)
$repository.Tasks.WaitForCompletion($task)
$completed = $repository.Tasks.Get($task.Id)

Error
Index was outside the bounds of the array.
At line:31 char:1

  • $repository.Tasks.WaitForCompletion($task)
  •   + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
      + FullyQualifiedErrorId : System.IndexOutOfRangeException

Another update,

I used your exact same code to run in my environment, the same error also occurred, could it be our Octopus.Client library different?

Thanks

Hi Jian,

What version of Octopus.Client are you using? I was using the latest version.

Thank you and best regards,
Henrik

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.