Last active
September 10, 2021 14:57
-
-
Save marcinotorowski/ef5eebf10f67c3a10e6a4bd755f3c852 to your computer and use it in GitHub Desktop.
A PowerShell script which shows how to trigger Data Hub tasks
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
$baseUrl = "<dataHubUrl>"; # for example http://localhost:81 | |
$taskGuid = "<taskGuid>"; # for example 6ca5c4e2-5095-4422-3b5e-08d96c734da2 - the ID of the task to start (can be copied from respective URL) | |
$username = "<username>"; # for example MyUser | |
$password = "<password>"; # for example Pa$$word | |
function TriggerTask | |
{ | |
param( | |
$DataHubUrl, | |
$AuthToken, | |
$TaskId | |
) | |
Write-Progress -Activity "Triggerring the task"; | |
$headers = @{ | |
"Content-Type" = "application/json"; | |
"Accept" = "application/json"; | |
"Accept-Encoding" = "gzip, deflate, br"; | |
"Authorization" = "Bearer " + $AuthToken; | |
} | |
$url = "{0}/v1/Tasks/trigger/{1:B}" -f $DataHubUrl, $TaskId; | |
$request = Invoke-WebRequest -Headers $headers -Method Get -Uri $url -ContentType 'application/json' | |
if ($request.StatusCode -ne 200) | |
{ | |
throw "Task could not be triggered. Error code {0}" -f $request.StatusCode; | |
} | |
$content = ConvertFrom-Json $request.Content | |
return @{ | |
"name" = $content.task.name; | |
"tableName" = $content.task.tableName; | |
} | |
} | |
function GetFirstTenantId | |
{ | |
param( | |
$DataHubUrl, | |
$UserName, | |
$Password | |
) | |
Write-Progress -Activity "Getting the first tenant ID..."; | |
$url = "{0}/v1/Authenticate/getTenants" -f $DataHubUrl | |
$payload = @{ | |
"username" = $UserName; | |
"password" = $Password; | |
} | |
$headers = @{ | |
"Content-Type" = "application/json-patch+json"; | |
"Accept" = "application/json"; | |
"Accept-Encoding" = "gzip, deflate, br"; | |
} | |
$request = Invoke-WebRequest -Headers $headers -Method Post -Body (ConvertTo-Json $payload) -Uri $url -ContentType 'application/json-patch+json' | |
if ($request.StatusCode -ne 200) | |
{ | |
throw "Identification of tenants was not possible. Error code {0}" -f $request.StatusCode; | |
} | |
$content = ConvertFrom-Json $request.Content; | |
if ($content.Length -eq 0) | |
{ | |
throw "No tenant found."; | |
} | |
$myTenant = $content[0].id # This will get the first tenant from the list. | |
return $myTenant | |
} | |
function GetAuthToken | |
{ | |
param( | |
$DataHubUrl, | |
$UserName, | |
$Password, | |
$TenantId | |
) | |
Write-Progress -Activity "Getting authorization token..."; | |
$url = "{0}/v1/Authenticate/request" -f $DataHubUrl; | |
$payload = @{ | |
"username" = $UserName; | |
"password" = $Password; | |
"tenantId" = $TenantId; | |
"fingerprint" = [System.Guid]::NewGuid().ToString("B"); | |
"rememberMe" = $false; | |
} | |
$headers = @{ | |
"Content-Type" = "application/json-patch+json"; | |
"Accept" = "application/json"; | |
"Accept-Encoding" = "gzip, deflate, br"; | |
} | |
$request = Invoke-WebRequest -Headers $headers -Method Post -Body (ConvertTo-Json $payload) -Uri $url -ContentType 'application/json-patch+json' | |
if ($request.StatusCode -ne 200) | |
{ | |
throw "Authentication was not possible. Error code {0}" -f $request.StatusCode; | |
} | |
$content = ConvertFrom-Json $request.Content; | |
$accessToken = $content.access_token; | |
return $accessToken; | |
} | |
# Step 1 | |
# Get list of tenants. You can ignore this step if you already know your tenant ID. | |
# In this case, jump directly to step 2. | |
$tenantId = GetFirstTenantId -DataHubUrl $baseUrl -UserName $username -Password $password; | |
# Step 2 | |
# Authenticate and get the authentication token (bearer) for the rest of the calls. | |
# The token is short-living so you should not cache it. | |
$authToken = GetAuthToken -DataHubUrl $baseUrl -UserName $username -Password $password -TenantId $tenantId; | |
# Step 3 | |
# Having the token, perform the operation. | |
$taskData = TriggerTask -DataHubUrl $baseUrl -AuthToken $AuthToken -TaskId $taskGuid; | |
"Task '{0}' writing to data set '{1}' has been successfully triggered." -f $taskData.name, $taskData.tableName; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment