Quantcast
Channel: InterWorks
Viewing all articles
Browse latest Browse all 2698

Tableau Email Subscription Documentation

$
0
0

Files: 

SubscriptionScript.ps1 -> Actual PowerShell script.
SubscriptionConfig.xml -> This is the config file that should drive all the settings for the script (filename can be changed).

First a look at the Config.xml file:

SubscriptionConfig.xml

How It Works:

1. The first step of the script is to pull down the view you specify in your config file to the file you specify. We do this with a few tabcmd statements:

tabcmd login -s $server -u $username -p $password
tabcmd get "$view_url.pdf" -f "$saved_file"

2. The second step is creating our email using .NET objects:

#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
$attach = new-object Net.Mail.Attachment("$saved_file")#Creating SMTP server object $smtp = new-object Net.Mail.SmtpClient($smtp_server)
$smtp.Credentials = New-Object System.Net.NetworkCredential($mail_username, $mail_password);
if ($config_xml.selectsinglenode('/root/mail/enable_ssl')."#text"–eq 1)
{$smtp.enablessl = $true}

#Email structure
$msg.From = "$message_from"
>foreach ($node in $message_to)
{$msg.To.Add($node."#text")}
$msg.subject = "$message_subject"
$msg.body = "$message_body"
$msg.Attachments.add($attach)
#Sending email
>$smtp.Send($msg)

I’ve omitted parts that are relevant for logging or cleanup, but these are the guts of the script.

Setting Up Your Subscription:

  1. Set up your config file with the appropriate settings
  2. Place your PowerShell script and config file somewhere that is available to the computer that will be running the subscription
  3. We are going to use Windows Task Manager to manage our schedules. Launch the task manager by going pulling up the run prompt (win+r) and entering: “Taskschd.msc”. You should see a screen like the one below pop up:

Task Scheduler

Next we are going to select the‘Create Task’ button on the right hand side, as shown above, and will be prompted with the dialogue below:

Create Task

Fill out the relevant fields. You can decide if you want to run it as yourself or use a service account to execute this.

We then fill out the 'Triggers' tab to set the schedule that we want our subscription to run on:

Triggers

Now we fill out the ‘Actions’ tab and tell our schedule to execute our PowerShell script with the right parameters.

Actions tab

Here's how we achieve this:

  1. Select a ‘New’ action
  2. Tell it that we want to run PowerShell
  3. Pass in the file we want to run as a parameter, and then separated by a space we pass it the parameter the script needs to find the config file. 

We can go through the ‘Conditions’ and ‘Settings’ tabs to make sure that everything is as we’d expect it, but most times the default values in these tabs will work fine. 

After this, we hit ‘OK’ and your subscription should be up and running.

AttachmentSize
SubscriptionScript.ps16.45 KB
config.xml1.73 KB
list.csv127 bytes

Viewing all articles
Browse latest Browse all 2698

Trending Articles