Run Tabcmd from Batch file and PowerShell Script
The same procedure written as DOS Batch (.bat) file and as a PowerShell script (.ps1).
Batch File (.bat) Version
Runs the Tableau commands via the Tabcmd utility to extract a Tableau sheet to a .csv file.
set csvfile=C:\DataSelf\Tableau_Extract\Replenish.csv
set tabcmd=C:\Tabcmd\2020.2\extras\Command_Line_Utility\tabcmd.exe
%tabcmd% login -s https://dataselfbi.com/#/site/<site-name> -u "user-name" --password "password"
%tabcmd% export "ReplenishItems/Sheet1" --csv -f "%csvfile%"
%tabcmd% logout
PowerShell (.ps1) Version
Runs exactly the same Tabcmd commands as the batch/.bat
file version above.
#-- File to receive the .csv output
$csvfile = "C:\DataSelf\Tableau_Extract\Replenish.csv"
$tabcmd = "C:\Tabcmd\2020.2\extras\Command_Line_Utility\tabcmd.exe"
Write-Host "** Login to Tableau Web Server"
& "$tabcmd" login -s https://dataselfbi.com/#/site/<sitename> -u "user-name" --password "password"
& "$tabcmd" export "ReplenishItems/Sheet1" --csv -f "$csvfile"
& "$tabcmd" logout
Save PowerShell files with the
.ps1
extension.To edit, find the script file in File Explorer, right-click and click
Edit
.
This usually will open the PowerShell editor PowerShell ISE.The file can also be edited with Notepad.
To run this script, find the file in File Explorer, right-click and click
Run with Powershell
.To run from a scheduler (Windows Task Scheduler or SQL Server Agent): Running PowerShell Script from Scheduler
PowerShell (.ps1) Version with Logging
This version creates a complete log file that saves all messages, errors or warnings from Windows and Tableau. This log file can be very helpful when diagnosing problems with processes scheduled to run
Uses Powershell’s Start-Transcript
command to capture every command, response and error message in a log file. A date & time stamp gives each log file a unique name. After running the tabcmd
commands and saving the log file, the script finishes by deleting prior log files more than 90 days old.
# Setup for Logging / Start-Transcript
$LogsPath = "$PSScriptRoot\Extract_Logs"
if (-not (Test-Path -LiteralPath $LogsPath)) {
write-host "Folder doesn't exist. Create New Folder: $LogsPath"
New-Item -Path $LogsPath -ItemType directory
}
$DateTimeStr = Get-Date -UFormat %Y-%m-%d@%H-%M
$Transcript_FileRef = "$LogsPath\Log_$DateTimeStr.txt"
Write-Host "Write Log entries to $Transcript_FileRef"
Start-Transcript -Path $Transcript_FileRef -Append # Start logging
#======================================================================
#-- File to receive the .csv output
$csvfile = "C:\DataSelf\Tableau_Extract\Replenish.csv"
$tabcmd = "C:\Tabcmd\2020.2\extras\Command_Line_Utility\tabcmd.exe"
Write-Host "** Login to Tableau Web Server"
& "$tabcmd" login -s https://dataselfbi.com/#/site/<sitename> -u "user-name" --password "password"
Write-Host ""
Write-Host "** Extract to $csvfile"
& "$tabcmd" export "ReplenishItems/Sheet1" --csv -f "$csvfile"
& "$tabcmd" logout
#===============================================================
Stop-Transcript
#--- Delete old log files after number of days set in the next line
$KeepDays = 90 # <=== OK to change / number of days to keep old Log files
$limit = (Get-Date).AddDays(-$KeepDays)
Write-Host " Delete files than $($limit)) in $LogsPath"
Get-ChildItem $LogsPath | Where-Object { $_.LastWriteTime -lt $limit } | Remove-Item -Force
Lines 2 - 6. Makes sure that the path to the folder that saves the log file exists. The file name for the log file includes a date and time stamp.
Line 2. $PSScriptRoot
in line 2 is a predefined variable that returns the folder path to the current script.
Lines 3 - 6. Creates the log file folder path if it does not exist.
Line 7. Formats the date & time stamp string used in the log file name.
Line 8. Combines $LogsPath
and the $DateTimeStr
variables into the final file path and file name and saves the whole name in the $Transcript_FileRef
variable.
Line 10. Starts the logging. All output from this point will be written to file named in the $Transcript_FileRef
variable.
Logging will stop when either the script ends or the Stop-Transcript
command is encountered.
Lines 13 - 24. The lines between the Start-Transcript
and the Stop-Transcript
commands are the main body and purpose of the script. In this example it’s the same tabcmd.exe commands first shown above.
Lines 28 - 32. Optional. These lines will delete old log files that are older than $KeepDays
days old.
Microsoft Recommendations PowerShell over Batch files
Windows has two command shells: The Command shell [batch/
.bat
files] and PowerShell.
For the most robust, up-to-date Windows automation, we recommend using PowerShell instead of Windows Commands … for Windows automation.
-- https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands
Related Pages:
Tabcmd.exe is a stand-alone application that works with Tableau Web Server.
https://www.dataself.com/docs/DataSelf_WebServer/PublishingDataSources/Tabcmdbatchfile.html Using the
refreshextacts
option. Installing Tabcmd.exe.