Created
February 22, 2020 17:45
-
-
Save levi-turner/f9f25ad9981cf9e3303117265e7d7fd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-------------------------------------------------------------------------------------------------------------------------------- | |
# | |
# Script Name: qs-qrs-telemetry_task_fix.ps1 | |
# Description: A PowerShell script to fix tasks for Telemetry Dashboard project | |
# Dependency: None | |
# | |
# Version Date Author Change Notes | |
# 0.1 2020-20-22 Levi Turner Initial Version | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
# Build out headers for QRS API Calls | |
$hdrs = @{} | |
$hdrs.Add("X-Qlik-Xrfkey","examplexrfkey123") | |
$hdrs.Add("X-Qlik-User", "UserDirectory=INTERNAL; UserId=sa_api") | |
# Get the certificate | |
$cert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where {$_.Subject -like '*QlikClient*'} | |
# Check for the cert | |
if (!$cert) { | |
Write-Host "Client certificate not found" -ForegroundColor Red | |
Exit | |
} | |
# Construct the host to call | |
$Data = Get-Content C:\ProgramData\Qlik\Sense\Host.cfg | |
$FQDN = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($($Data))) | |
if (!$Data) { | |
Write-Host "Host.cfg file not found. Ensure script is run on a Qlik Sense node" -ForegroundColor Red | |
Exit | |
} else { | |
Write-Host "Attempting to connect to $FQDN" -ForegroundColor Green | |
} | |
# Handle TLS 1.2 exclusive environments | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' | |
# Test call /qrs/about to ensure connectivity | |
$about = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/about?xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if (!$about) { | |
Write-Host "Unable to contact QRS at $($FQDN)" -ForegroundColor Red | |
Exit | |
} else { | |
Write-Host "Connected to $FQDN" -ForegroundColor Green | |
} | |
# Check for whether the app exists | |
$app = 'Telemetry Dashboard' | |
$telemetryapp = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/app/full?filter=(name eq '$app')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if (!$telemetryapp) { | |
Write-Host "Telemetry App: [ ]" -ForegroundColor Red | |
Write-Host "Rename / Import app" -ForegroundColor Red | |
Exit | |
} | |
elseif ($telemetryapp.count -gt 1) { | |
Write-Host "Duplicate copies of apps named Telemetry Dashboard found" -ForegroundColor Red | |
Write-Host "Ensure only 1 app is named Telemetry Dashboard for task fix" -ForegroundColor Red | |
Exit | |
} | |
else { | |
Write-Host "Telemetry App: [x]" -ForegroundColor Green | |
} | |
$telemetryapp = $telemetryapp.id | |
# Check for App Metadata Fetch Task | |
$task1 = 'TelemetryDashboard-1-Generate-Metadata' | |
$fetchtask = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/task/full?filter=(name eq '$task1')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if (!$fetchtask) { | |
Write-Host "Metadata Fetch: [ ]" -ForegroundColor Red | |
Write-Host "Creating Metadata Fetch Task " -ForegroundColor Green | |
$sharepath = (Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/servicecluster/full?xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert).settings.sharedPersistenceProperties.rootFolder | |
$sharepath = $sharepath.Replace('\', '\\') | |
$parampath = $sharepath | |
$parampath += '\\TelemetryDashboard\\MetadataGenerater\\fetchMetadata.js' | |
$body = '{ | |
"path": "..\\ServiceDispatcher\\Node\\node.exe",' | |
$body += '"parameters": "' | |
$body += $parampath | |
$body += '"' | |
$body += ',' | |
$body += '"name": "TelemetryDashboard-1-Generate-Metadata", | |
"taskType": 1, | |
"enabled": true, | |
"taskSessionTimeout": 1440, | |
"maxRetries": 0, | |
"impactSecurityAccess": false, | |
"schemaPath": "ExternalProgramTask" | |
}' | |
$null = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/externalprogramtask?xrfkey=examplexrfkey123" -Method Post -Body $body -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
$fetchtask = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/task/full?filter=(name eq '$task1')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if ($fetchtask.count -eq '1') { | |
Write-Host "Metadata Fetch: [x]" -ForegroundColor Green | |
} | |
$trigger = 'telemetry-metadata-trigger' | |
$triggerevent = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/compositeevent/full?filter=(name eq '$trigger')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
$null = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/compositeevent/$($triggerevent.id)?xrfkey=examplexrfkey123" -Method Delete -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
} | |
elseif ($fetchtask.count -gt 1) { | |
Write-Host "Multiple tasks found named TelemetryDashboard-1-Generate-Metadata" -ForegroundColor Red | |
Write-Host "Ensure only 1 task is named TelemetryDashboard-1-Generate-Metadata" -ForegroundColor Red | |
Exit | |
} | |
else { | |
Write-Host "Metadata Fetch: [x]" -ForegroundColor Green | |
} | |
# Check for App Reload Task | |
$task2 = 'TelemetryDashboard-2-Reload-Dashboard' | |
$apptask = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/task/full?filter=(name eq '$task2')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if (!$apptask) { | |
Write-Host "App Reload Task: [ ]" -ForegroundColor Red | |
Write-Host "Creating Reload Task" -ForegroundColor Green | |
$reloadtaskbody = '{"task": { | |
"app": { | |
"id": "' | |
$reloadtaskbody += $telemetryapp | |
$reloadtaskbody += '",' | |
$reloadtaskbody += ' | |
"name": "Telemetry Dashboard" | |
}, | |
"enabled": true, | |
"isManuallyTriggered": false, | |
"maxRetries": 0, | |
"name": "TelemetryDashboard-2-Reload-Dashboard", | |
"taskSessionTimeout": 1440, | |
"taskType": 0 | |
} | |
}' | |
$null = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/reloadtask/create?xrfkey=examplexrfkey123" -Method Post -Body $reloadtaskbody -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
$apptask = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/task/full?filter=(name eq '$task2')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if ($apptask.count -eq '1') { | |
Write-Host "App Reload Task: [x]" -ForegroundColor Green | |
} | |
} | |
elseif ($apptask.count -gt 1) { | |
Write-Host "Multiple tasks found for app named Telemetry Dashboard" -ForegroundColor Red | |
Write-Host "Ensure only 1 task is named TelemetryDashboard-1-Generate-Metadata" -ForegroundColor Red | |
Exit | |
} | |
else { | |
Write-Host "App Reload Task: [x]" -ForegroundColor Green | |
} | |
# Check for the trigger | |
$trigger = 'telemetry-metadata-trigger' | |
$triggerevent = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/compositeevent/full?filter=(name eq '$trigger')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if (!$triggerevent) { | |
Write-Host "Tasks linked: [ ]" -ForegroundColor Red | |
Write-Host "Creating link" -ForegroundColor Green | |
$triggerbody = ' | |
{ | |
"compositeEvents":[ | |
{ | |
"timeConstraint":{ | |
"seconds":0, | |
"minutes":360, | |
"hours":0, | |
"days":0 | |
}, | |
"name":"telemetry-metadata-trigger", | |
"enabled":true, | |
"eventType":1, | |
"reloadTask":{ | |
"id":"' | |
$triggerbody += $apptask.id | |
$triggerbody +='" | |
}, | |
"compositeRules":[ | |
{ | |
"ruleState":1, | |
"externalProgramTask":{ | |
"id":"' | |
$triggerbody += $fetchtask.id | |
$triggerbody +='" | |
} | |
} | |
], | |
"privileges":[ | |
"read", | |
"update", | |
"create", | |
"delete" | |
] | |
} | |
] | |
}' | |
$null = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/reloadtask/update?xrfkey=examplexrfkey123" -Method Post -Body $triggerbody -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
$triggerevent = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/compositeevent/full?filter=(name eq '$trigger')&xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert | |
if ($triggerevent.count -eq '1') { | |
Write-Host "Tasks linked: [x]" -ForegroundColor Green | |
} | |
} | |
elseif ($triggerevent.count -gt 1) { | |
Write-Host "Multiple chains are found." -ForegroundColor Red | |
Write-Host "Trigger is named telemetry-metadata-trigger" -ForegroundColor Red | |
Write-Host "Ensure only 1 trigger is named telemetry-metadata-trigger" -ForegroundColor Red | |
Exit | |
} | |
else { | |
Write-Host "Tasks linked: [x]" -ForegroundColor Green | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment