Octopus client & Powershell - 30 results max lists

Hello,
Every time I try to extract data (releases list for instance) from Octopus using the dll and Powershell, I get only 30 results (the max in the UI - website).
I read here, this is something to do with the “paginate” instruction (exists in C#), but what if we still want to use Powershell?
Is there a workaround?
Thank you for your time.
Best Regards.
L.

Hi Lucio, thanks for reaching out.

The Octopus Client library has a number of methods that start with the prefix GetAll that return all the items in a collection. Behind the scenes these methods handle pagination for you, so you get the complete list without having to deal with multiple requests.

For example, the code below will get all the releases for a project.

[Reflection.Assembly]::LoadFrom("octopus.client.4.41.2\lib\net45\Octopus.Client.dll")
$endpoint = new-object Octopus.Client.OctopusServerEndpoint("https://master.octopushq.com", "API-APIKEYGOESHERE")
$repository = new-object Octopus.Client.OctopusRepository($endpoint)

$project = $repository.Projects.FindByName("Your project name");
$releases = $repository.Projects.GetAllReleases($project);
# $releases is the complete list

Regards
Matt C

Hello Matthew,
thank you for reaching me out.
That’s exactly what I am saying (see below attached).

I have used the instruction $repository.Projects.GetChannels($project);
And the items I can assign to a variable are only 30 (even if the total result is 88).
There isn’t any getallchannels method.
Any idea?

Thank you for your time.
Best regards.
L.

Hi Lucio,

Where the clients library doesn’t have a GetAll method you can replicate the same functionality in Powershell with a few lines of code.

If you look at the code in https://github.com/OctopusDeploy/OctopusClients/blob/56fe5a427451237463a5057dd401855869e7ba0d/source/Octopus.Client/Repositories/ProjectRepository.cs#L35 you can see that the GetAllReleases call is just return Client.ListAll<ReleaseResource>(project.Link("Releases"));.

This equates to the following PowerShell.

[Reflection.Assembly]::LoadFrom("Octopus.Client.dll")
$endpoint = new-object Octopus.Client.OctopusServerEndpoint("https://master.octopushq.com", "API-APIKEYGOESHERE")
$repository = new-object Octopus.Client.OctopusRepository($endpoint)
$project = $repository.Projects.FindByName("Project Name");
$method = [Octopus.Client.IOctopusClient].GetMethod("ListAll")
$closedMethod = $method.MakeGenericMethod([Octopus.Client.Model.ReleaseResource])
$releases = $closedMethod.Invoke($repository.Client, @($project.Link("Releases"), $null))

Replace Octopus.Client.Model.ReleaseResource with Octopus.Client.Model.ChannelResource and $project.Link("Releases") with $project.Link("Channels") for the channels query.

[Reflection.Assembly]::LoadFrom("Octopus.Client.dll")
$endpoint = new-object Octopus.Client.OctopusServerEndpoint("https://master.octopushq.com", "API-APIKEYGOESHERE")
$repository = new-object Octopus.Client.OctopusRepository($endpoint)
$project = $repository.Projects.FindByName("Project Name");
$method = [Octopus.Client.IOctopusClient].GetMethod("ListAll")
$closedMethod = $method.MakeGenericMethod([Octopus.Client.Model.ChannelResource])
$channels = $closedMethod.Invoke($repository.Client, @($project.Link("Channels"), $null))
$channels

http://www.leeholmes.com/blog/2007/06/19/invoking-generic-methods-on-non-generic-classes-in-powershell/ has details on how to call generic C# methods in PowerShell.

I’ve also created a PR at https://github.com/OctopusDeploy/OctopusClients/pull/320 to add the GetAllChannels method to make this easier in future, so keep an eye on that to grab the new Clients library when it is merged.

Regards
Matt C

Hello Matt,
this is exactly what I was looking for.
It’s working perfectly.
I will take a look when the PR will be merged into.
Thank you for your time.
Regards.
L.

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