Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!

How to create an external program task in Qlik Sense with a Powershell Script

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
Bastien_Laugiero

How to create an external program task in Qlik Sense with a Powershell Script

Last Update:

Mar 13, 2023 3:45:05 AM

Updated By:

Sonja_Bauernfeind

Created date:

Mar 10, 2020 3:47:23 AM

External program tasks in Qlik Sense are simply another task type. With external program tasks, you can trigger external processes, such as scripts or .exe files. Task chaining is supported and you can combine reload tasks with external program tasks.

The functionality is exposed in the Qlik Sense Management Console beginning with the May 2021 release. See Creating and editing external program tasks.

Previous versions allow you to create external tasks in a command shell process behind the scenes; which in turns means you can run more or less anything you can run on the command prompt. Once you define your command and create a new task, you can both trigger and chain these types of tasks in the QMC.

In this example, we will be using a Powershell Script which means that powershell.exe needs to be in your PATH variable (By default C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe). Again, you can run any command that you can run in the windows command prompt, so these examples are only for demo purposes.

Before we get started, we need to understand that IF you are calling scripts in your command, the script needs to be accessible by the user running the Qlik services. In this example, there’s a folder on the root C: drive called ‘externalTasksExample’, in this folder, there is a script.ps1 file which simply creates a new file in the same folder.

The file looks like:

Start-Transcript C:\externalTasksExample\transcript.log
$date = Get-Date
Write-Host $date
Stop-Transcript

You can run the file from the command prompt by doing the following:

C:\externalTasksExample>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File script.ps1

When the command runs successfully, you should have a new file created in the externalTasksExample folder called “transcript.log”.

Now we will delete this transcript.log and try to run that same PowerShell file using an external program task in Qlik Sense. The first thing we need to do is actually create the task.

The endpoint is:

POST /qrs/externalprogramtask

With the body being:

{
  "path": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  "parameters": "C:\\externalTasksExample\\Script.ps1",
  "name": "Task Name",
  "taskType": 1,
  "enabled": true,
  "taskSessionTimeout": 1440,
  "maxRetries": 0,
  "privileges": null,
  "schemaPath": "ExternalProgramTask"
}

Here is a sample script:

 

$hdrs = @{}
$hdrs.Add("X-Qlik-xrfkey","12345678qwertyui")
$hdrs.Add("X-Qlik-User","UserDirectory=DOMAIN;UserId=Administrator")
$hdrs.Add("content-type","application/json; charset=UTF-8")
$body = '{
  "path": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  "parameters": "C:\\externalTasksExample\\Script.ps1",
  "name": "Task Name",
  "taskType": 1,
  "enabled": true,
  "taskSessionTimeout": 1440,
  "maxRetries": 0,
  "privileges": null,
  "schemaPath": "ExternalProgramTask"
}'

$cert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where {$_.Subject -like '*QlikClient*'}
$url = "https://qlikserver1.domain.local:4242/qrs/externalprogramtask?xrfkey=12345678qwertyui"
Invoke-RestMethod -Uri $url -Method Post -Headers $hdrs -Body $body -Certificate $cert

 

This generates a response like:

 

id                 : 1353eb7c-44c7-422b-9e73-c2667011a1b0
createdDate        : 2022-06-13T19:35:40.382Z
modifiedDate       : 2022-06-13T19:35:40.382Z
modifiedByUserName : DOMAIN\administrator
customProperties   : {}
path               : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
parameters         : C:\externalTasksExample\Script.ps1
qlikUser           : 
operational        : @{id=5ececc2a-dc6c-4769-b424-c325faddf74c; lastExecutionResult=; nextExecution=1753-01-01T00:00:00.000Z; privileges=}
name               : Task Name
taskType           : 1
enabled            : True
taskSessionTimeout : 1440
maxRetries         : 0
tags               : {}
privileges         : 
schemaPath         : ExternalProgramTask

 

Once you post this into Qlik, you should be able to go to the QMC and see the following task in the task list.

You should be able to select the task, click 'Start', and the same file should appear in 'C:\externalTasksExample'. You are now running an external task. Remember you don't have to use powershell.exe, you can trigger anything you want as long as it can be run from the command line.

 

Related Content:

How to configure Postman (desktop app) to connect to Qlik Sense 
 

Labels (2)
Comments
maks2481
Partner - Contributor II
Partner - Contributor II

Hi

You write:

"Once you post this into Qlik, you should be able to go to the QMC and see the following task in the task list."

How to post this in Qlik ?

Thanks.

 

Sonja_Bauernfeind
Digital Support
Digital Support
matKa
Contributor III
Contributor III

Hi @Sonja_Bauernfeind ,

In latest versions it's possible to edit external tasks in QMC. I did just like instructions above but directly in QMC. The task fails though right after running it. I can't download any logs all I get is:

 
Changing task state from Started to FinishedFail
 
ExitCode: 1
 
How can I troubleshoot this?
Sonja_Bauernfeind
Digital Support
Digital Support

Hello @matKa 

I would recommend posting about this issue in the appropriate forum: Qlik Sense Deployment & Management.

All the best,
Sonja 

hanna_choi
Partner - Creator
Partner - Creator

@Bastien_Laugiero 

Hello

I have completed the exercise until you run the script.ps1 powershell that creates transcript.log.

But I got lost from "The endpoint is:" explanation.
How do I change Script.ps1?
What value should I enter for Parameter in the QMC setting?

I need your help.

Thanks.

Sonja_Bauernfeind
Digital Support
Digital Support

Hello @hanna_choi !

We're reviewing the article to see if we can give you a more detailed example.

All the best,
Sonja 

Sonja_Bauernfeind
Digital Support
Digital Support

Hello @hanna_choi 

We have added sample scripts.

If you require more assistance, I would recommend posting about your query in our active Integration forum (where you can make use of our userbase and our support engineers).

All the best,
Sonja 

jchacko_rxsense
Contributor III
Contributor III

@Bastien_Laugiero 
I was able to create an external task using the provided powershell script.

Is it possible to run another QMC task (not an external task) after my external task executes successfully?

Sonja_Bauernfeind
Digital Support
Digital Support

Hello @jchacko_rxsense 

Is this what you are looking for? Examples: Trigger a Qlik Sense task externally 

If you need more assistance with implementing and customising the code,  please post in our active Integration forum (where you can use our userbase and support engineers).

All the best,
Sonja 

GeoFev
Contributor II
Contributor II

Hi,

Most of search on the web to create an external program task to execute a powershell script bring to this page. So just to be more clear for those who do not use postman and want to execute a powershell script: In the QMC, create a new task with this configuration :

Path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Parameters: C:\externalTasksExample\Script.ps1

Do not use -command or -file parameter  as usual with PowerShell.exe.

Thanks @Bastien_Laugiero  !

Version history
Last update:
‎2023-03-13 03:45 AM
Updated by: