PowerShell Automation Example

This PowerShell example demonstrates how to automate the following operations:

  • Submit the following scripts to BarTender Cloud by using the Actions API:

    • PrintBTWAction: Prints a BarTender document to PDF.

    • WriteMessageToLogAction: Writes a message to the message log.

    • DeleteFileAction: Deletes the PDF file from the server.

  • Download the PDF file contents to the local disk by using the Librarian API.

Note: In the following example, you must replace "____PASTE_YOUR_ACCESS_TOKEN_HERE____" with your access token. For more information about your access token, refer to Authentication.

Copy
param (
  [string]$btw_file = "Librarian://main/MyDocuments/Simple1_QR.btw",
  [string]$local_pdf_folder = "d:\",
  [string]$access_token = "____PASTE_YOUR_ACCESS_TOKEN_HERE____"
)

$btc_server_location = "https://am1.bartendercloud.com" # Replace am1. with eu1. or ap1., depending on your region.

$print_script_template = "
- PrintBTWAction:
    Document: {0}
    Printer: PDF
    QueueAndContinue: false
    VerifyPrintJobIsComplete: true
    MakeUniqueOutputFile: true
    SaveAfterPrint: false    
    ReturnPrintData: true
    ReturnPrintSummary: true
    PrintToFileFolder: Librarian://main/MyDocuments/pdf/
    PrintToFileFileName: Output.pdf
    PrintToFileNameVariable: PrintToFileName

- WriteMessageToLogAction:
    Level: Info
    Message: '%PrintToFileName%'
"

$delete_pdf_script_template = "
- DeleteFileAction:
    File: {0}
"

$script = ($print_script_template -f $btw_file)
Write-Host $script

$content_type = "text/vnd.yaml"
$submit_script_url = "{0}/api/actions?Wait=30s&MessageCount=200&MessageSeverity=Info" -f $btc_server_location
$headers = @{ "Content-Type"=$content_type; "Authorization"=("Bearer {0}" -f $access_token) }

$response = Invoke-WebRequest -Uri $submit_script_url `
                              -Method POST `
                              -Headers $headers `
                              -Body $script
$response

$status = ($response.Content | ConvertFrom-Json).Status
Write-Host ("The script status is {0}." -f $status)

if ($status -eq "RanToCompletion")
{
   # Get the output PDF file path by querying the %PrintToFileName% variable value.
   $status_url = ($response.Content | ConvertFrom-Json).StatusUrl
   $get_pdf_path_url = "{0}/variables/PrintToFileName" -f $status_url
   Write-Host ("Getting output pdf file path from {0}." -f $get_pdf_path_url)

   $headers = @{ "Authorization"=("Bearer {0}" -f $access_token) }
   $response = Invoke-WebRequest -Uri $get_pdf_path_url `
                                 -Method GET `
                                 -Headers $headers
   $pdfpath = $response.Content
   Write-Host ("Output pdf file in the server is '{0}'." -f $pdfpath) -ForegroundColor Yellow -BackgroundColor Blue

   # Download the output PDF file by using the Librarian API.
   $get_pdf_content_url = "{0}/api/librarian/files/path/{1}/content" -f $btc_server_location, [System.Web.HttpUtility]::UrlEncode($pdfpath)

   $output_pdf_filename = $btw_file.Substring($btw_file.LastIndexOf("/") + 1).Replace(".btw", ".pdf")
   $output_pdf_path = ("{0}\{1}" -f $local_pdf_folder, $output_pdf_filename)

   Write-Host ("Downloading pdf from {0}" -f $get_pdf_content_url)

   $response = Invoke-WebRequest -Uri $get_pdf_content_url `
                                 -Method GET `
                                 -Headers $headers `
                                 -OutFile $output_pdf_path

   Write-Host ("Output pdf file is downloaded to {0}" -f $output_pdf_path) -ForegroundColor Yellow -BackgroundColor Blue

   # Delete the PDF file from the server, because it is no longer needed.
   $script = ($delete_pdf_script_template -f $pdfpath)
   Write-Host $script
   $headers = @{ "Content-Type"=$content_type; "Authorization"=("Bearer {0}" -f $access_token) }

   $response = Invoke-WebRequest -Uri $submit_script_url `
                                 -Method POST `
                                 -Headers $headers `
                                 -Body $script
   $response

   Write-Host ("Output pdf file '{0}' in the server has been deleted." -f $pdfpath) -ForegroundColor Yellow -BackgroundColor Blue
}