Files
qseow-scripts/getAllEvents.ps1
2023-04-13 13:10:28 -04:00

51 lines
2.2 KiB
PowerShell

#!/usr/bin/pwsh
### Set your hostname, apiKey and change paths as desired.
$hostname = "cg8fuxcfavt2dog.us.qlikcloud.com"
$apiKey = "eyJhbGciOiJFUzM4NCIsImtpZCI6IjJlY2M2NWJmLTA4NzUtNDE3Mi05YzE4LTg2ZmZhNmI5Yzk5OSIsInR5cCI6IkpXVCJ9.eyJzdWJUeXBlIjoidXNlciIsInRlbmFudElkIjoiRlVPVl9vZkQ0YmRsejZBNk5PWkQ3UU9JYmF3MjFWSDciLCJqdGkiOiIyZWNjNjViZi0wODc1LTQxNzItOWMxOC04NmZmYTZiOWM5OTkiLCJhdWQiOiJxbGlrLmFwaSIsImlzcyI6InFsaWsuYXBpL2FwaS1rZXlzIiwic3ViIjoiNjIxNjU3MzJmZDIwYjVhMmYzZmVlNWY4In0.XKYFTzBiZ-huSZGVFNBqIlTkkUVw0jdGncD_pNIohzAJ5WNRHFx0lD17H68IPhC4Z1oOuuxfeaAHkqf-8fsjRAIEEGmTnX-b9NDmHk-0BtZEVqYa03LVcyRQ_Eq4bAx1"
$json = "$HOME\events-list.json"
$txt = "$HOME\events-list.txt"
### Don't change these variables
# $URL = "https://$hostname/api/v1/reloads?limit=100"
$URL = "https://$hostname/api/v1/audits"
$token = $apiKey | ConvertTo-SecureString -AsPlainText -Force
### Invoke the first REST request to the reloads endpoint
$reloads = (Invoke-RestMethod -Uri $URL -Authentication Bearer -Token $token -ContentType 'application/json')
### Check if files exist, create if not, carry-on if they already do.
if (-not(Test-Path -Path $txt -PathType Leaf)) {
try {
$null = New-Item -ItemType File -Path $txt -Force -ErrorAction Stop
Write-Host "The file [$txt] has been created."
}
catch {
throw $_.Exception.Message
}
}
else {
Write-Host "Cannot create [$txt] because a file with that name already exists."
}
if (-not(Test-Path -Path $json -PathType Leaf)) {
try {
$null = New-Item -ItemType File -Path $json -Force -ErrorAction Stop
Write-Host "The file [$json] has been created."
}
catch {
throw $_.Exception.Message
}
}
else {
Write-Host "Cannot create [$json] because a file with that name already exists."
}
### While there is a value for the "next" cursor, indicating more output, run again
while ($reloads.links.next) {
$reloads | ConvertTo-JSON -Depth 4 >> $json
$reloads.data | Sort-Object -Property startTime -Descending | Format-Table -Property appId,name,type,status,startTime,endTime |Tee-Object -Append -FilePath $txt
$URL = $reloads.links.next.href
$reloads = (Invoke-RestMethod -Uri $URL -Authentication Bearer -Token $token -ContentType 'application/json')
}