Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Last active March 23, 2017 11:33
Show Gist options
  • Save pawelpabich/08844f957e3fc47e11dff47e3a0259c4 to your computer and use it in GitHub Desktop.
Save pawelpabich/08844f957e3fc47e11dff47e3a0259c4 to your computer and use it in GitHub Desktop.
Gets events related to variable sets
param($OctopusServerUrl, $OctopusServerApiKey, $OutputFile, $DateFrom, $DateTo)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest;
$Server = @{ OctopusServerUrl = $OctopusServerUrl; OctopusServerApiKey = $OctopusServerApiKey}
function Get-Data($Path)
{
$Path = $Path.Replace("api/", "")
$headers = @{'X-Octopus-ApiKey'= $OctopusServerApiKey}
$data = Invoke-RestMethod -Method Get -Headers $headers -Uri "$OctopusServerUrl/api/$Path"
return $data
}
function Get-VariableSetRelatedEvents
{
$batchUrl = "api/events?eventCategories=Modified&from=$DateFrom&to=$DateTo"
[System.Collections.ArrayList]$eventsToReturn = @()
do
{
Write-Host "Retrieving a batch of events from $batchUrl"
$batch = Get-Data -Path $batchUrl -Server $Server
[array]$events = $batch.Items | Where-Object {$_.RelatedDocumentIds[0].StartsWith("variableset-") };
if ($events -ne $null) {
$eventsToReturn.AddRange($events);
}
$property = Get-Member -InputObject $batch.Links -MemberType Properties | Where-Object {$_.Name -eq 'Page.Next'}
if ($property -eq $null)
{
$eventsCount = $eventsToReturn.Count;
Write-Host "No more events to process"
Write-Host "Total of $eventsCount events related to variable sets found"
return $eventsToReturn | Sort-Object Occurred ;
}
$batchUrl = $batch.Links.'Page.Next'
$batchUrl += "&eventCategories=Modified&from=$DateFrom&to=$DateTo";
} while($true)
}
$events = Get-VariableSetRelatedEvents
$index = 0;
$output = "<pre>";
foreach($event in $events)
{
$occured = $event.Occurred;
$output += "Occurred: $occured <br>";
$output += $event.Details.Replace("&para;", "");
$output += "<hr>"
}
$output += "</pre>";
$output | Out-File $OutputFile
Write-Host "Results written to $OutputFile"
.\Get-Events.ps1 -OctopusServerUrl "http://octopus.mydomain.com" -OctopusServerApiKey "API-XXXXXXXXXXXXX" -OutputFile "C:\Temp\Diffs\index.html" -DateFrom "2017-03-20" -DateTo "2017-03-24"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment