As you’ve found the
FindAll() method on the client doesnt allow for easily filtering on dates on the server side. That’s because the underlying API call (
/api/deployments) doesnt support it.
To get an efficient list of yesterdays deployments, I’d use the Events API (
/api/events) endpoint instead as it’s one of the only endpoints that allows you to filter on date on the server side, much more efficient than pulling all data back and then filtering on the client side.
With it, you can filter on different types of deployment event. For example: “DeploymentQueued”, “DeploymentStarted”, “DeploymentSucceeded”. Which you choose is up to your own requirements.
Using DeploymentQueued as an example, here is what a script for yesterdays queued deployments might look like for the
# Load octopus.client assembly
Add-Type -Path "C:\Octo\Octopus.Client.dll"
$octopusURL = "https://your.octopus.app"
$octopusAPIKey = "API-YOURKEY"
$spaceName = "Default"
$Today = Get-Date
$StartOfYesterday = $Today.AddDays(-1).Date
$EndOfYesterday = $Today.Date.AddMilliseconds(-1)
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURL, $octopusAPIKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$client = New-Object Octopus.Client.OctopusClient $endpoint
# Get space
$space = $repository.Spaces.FindByName($spaceName)
$repositoryForSpace = $client.ForSpace($space)
# Required Parameters
$from = $StartOfYesterday.ToString("yyyy-MM-dd HH:mm:ss")
$to = $EndOfYesterday.ToString("yyyy-MM-dd HH:mm:ss")
$eventGroups = "Deployment"
$eventCategories = "DeploymentQueued"
# Other Parameters
$skip = 0
$take = $null
$regarding = $null
$regardingAny = $null
$includeInternalEvents = $True
$user = $null
$users = $null
$projects = $null
$environments = $null
$tenants = $null
$tags = $null
$fromAutoId = $null
$toAutoId = $null
$documentTypes = $null
$eventAgents = $null
$projectGroups = $null
# Get Deployment events queued yesterday
$deployments = $repositoryForSpace.Events.List($skip,$take, $from, $to, $regarding, $regardingAny, $includeInternalEvents, $user, $users, $projetcs, $environments, $eventGroups, $eventCategories, $tenants, $tags, $fromAutoId, $toAutoId, $documentTypes, $eventAgents, $projectGroups)
Write-Host "Found $($deployments.Items.Count) deployments queued between $($from) and $($to)"
Note: If you add multiple event categories the Items count will need to be filtered to identify unique deployments
I hope that helps!