TaskAgent & Windows PowerShell - Part 3
November 28th, 2008This is Part 3 of the TaskAgent & PowerShell series. Here you can find Part 1 and Part 2 posts. Check the Downloads section at the end of the post for the available source files and PDF version of the full post.
Scenario 3 – Web Dashboard powered by TaskAgent and Windows PowerShell
The third task which I wanted to automate involves the periodic update of an online dashboard which displays a summary of different types of business data – . This time I will use some T-SQL scripting from within the PowerShell script to pull the necessary data from the database. In addition, to make the script usable across different databases, I will be passing the database connection information into it via command-line parameters. Here’s how this is done:
1. We will start by creating the “frame” for our online dashboard. I will use a simple HTML file - dashboard.html -which has just a heading text and a table with two rows and two columns – this will give us the four cells where the dashboard data will be presented. Each dashboard cell will be a separate web page represented by an IFRAME element. Here is the content of the dashboard.html file:
In addition to the dashboard HTML file, create four files for the different frames – frame1.html, frame2.html, frame3.html and frame4.html – the files can be simply blank or contain some default text to be displayed while there is no summary data. These files will later be automatically replaced by the TaskAgent with ones containing our live data. Here is how the directory containing the dashboard HTML files should look:
Here is how the empty dashboard should look while there is no data to display:
2. Create the PowerShell scripts which will do all the processing. We will use a separate script to update each dashboard cell. This will provide the flexibility of updating the different parts of the dashboard at different intervals (using different TaskAgent jobs) or using data from different data sources. The demo will use a single job to process all four tasks, but it is very easy to separate the tasks into different jobs with different schedules. You can use the ready files available from the download section – demo_webdashboard_frame1.ps1, demo_webdashboard_frame2.ps1, demo_webdashboard_frame3.ps1, demo_webdashboard_frame4.ps1
I will briefly explain what the purpose is of the different sections of the first script file. The scripts for the other frames are similar – only the SQL query and the frame HTML file which they update are different.
The first section of the script handles the input parameters. The scripts expects two parameters – the SQL server name (specified by /S:<server_name>) and the database name (specified by /D:<database_name>). It checks if the required parameters are provided and extracts their values. If any of the required parameters is not provided, the script throws an exception with the appropriate message.
The second part of the script prepares a SQL command object and a SQL connection object and executes a SQL query to get the summary data. The section marked with blue is the SQL query which is different in each of the frame scripts. The database connection is created using the information specified by the command-line parameters. It uses integrated security (Windows authentication) instead of requiring username and password.
The last part of the script formats the retrieved SQL data in an HTML table and writes the results to an HTML file. The section marked with blue is the HTML file name which is different for each of the frame scripts.
3. In the TaskAgent console, create a job with a time schedule (similar to the way it is done for the prior two scenarios)
4. Create a task for the job created above and select “Run External Program” as the task type.
5. On the “Task Settings” – “General” tab, in the “Program or File to Run” field, enter the PowerShell executable “powershell.exe” and in the “Command-line Parameters” field, enter the full path to the PowerShell script file. The difference here is that in addition to the script file, we provide as part of the command-line parameters the database connection information which should be used by the PowerShell script. In this particular case, we are providing only the SQL server name and the database – the PowerShell will use Windows authentication to connect to the database. This requires that the Windows credentials which will be used to execute the task have a corresponding SQL login on the specified SQL server. I chose to go this route to avoid entering in plain text the SQL server username and password. I recommend that you use this approach as well because it does not expose any secure information.
Note that the script input parameters are surrounded by single-quotes.
6. On the “Security” tab provide the necessary Windows account credentials (which also have a corresponding SQL login account) if the TaskAgent service account does not provide them.
7. On the “Execution Result” tab configure what the TaskAgent should do after the external program has been started.
8. Repeat the steps 4 through 7 three more times – the only difference each time will be the PowerShell script file which you specify in the Command-line parameters field. The end result should be a job with four tasks – each task executing one of the four PowerShell scripts which we created in Step 2.
Do not forget to configure the job task workflow – use the “Workflow” tab to specify how the tasks should be executed. The end result should be similar to this:
Save the job and give it a test run. Depending on your TaskAgent configuration it may take a minute or two to complete the execution of all four tasks. At the end, the updated web dashboard will look similar to this.
Depending on the job schedule, the TaskAgent will periodically update the dashboard with the latest information from your database. You can even configure the different tasks to pull information from different databases – this way the dashboard will be presenting on a single page summary information from multiple data sources. By modifying the SQL queries you can present different summaries. You can even add new “cells” to the dashboard to expand the presented information. The current style of the dashboard is not perfect and requires the attention of a web designer but I hope that at least its business value is clear.
Downloads
PDF version of the full post is available here: TaskAgent & Windows PowerShell
The demo source files are available for download here: Sample PowerShell scripts and other source files from the demos









