Tuesday, January 28, 2014

Publish a page from PowerShell

Today I fiddled around with PowerShell and found you can pretty easily publish a page in SDL Tridion. This can come in handy for administrative tasks where you typically want to automate in scripting, to avoid to setting up a .NET console project with a lot of code to maintain.

The Tridion PowerShell Modules project comes in handy to get you a CoreService client. It's just five steps to install this module on your machine.
  1. Get the .psm1, .psd1 and DLL files from the project.
    You can also copy the CoreService DLL from the %TRIDION_HOME%bin\client\CoreService folder on your own Content Manager server if you do not like to download the DLLs from the project.
  2. Create a directory
    C:\Users\your_username\Documents\WindowsPowerShell\Modules\Tridion-CoreService
  3. Copy the .psm1, .psd1 and .dll files in that directory
  4. Restart any open PowerShell consoles or run Import-Module Tridion-CoreService in the open PowerShell console.
Once you can use the PowerShell module, run a simple script to publish the page.
# Set up a Tridion Core Service client for the Content Manager server cms.server.com.
# The 2013-SP1 label is for the CM server version, other valid entries are 2011-SP1 and 2013.
Import-Module Tridion-CoreService
Set-TridionCoreServiceSettings cms.server.com 2013-SP1
$client = Get-TridionCoreServiceClient
# Set a publish instruction with resolve instruction and render instruction. These are all standard
# instructions, nothing special here but you can use this to create a special publinshing action.
$publishIntructionData = New-Object Tridion.ContentManager.CoreService.Client.PublishInstructionData
$publishIntructionData.RenderInstruction = New-Object Tridion.ContentManager.CoreService.Client.RenderInstructionData
$publishIntructionData.ResolveInstruction = New-Object Tridion.ContentManager.CoreService.Client.ResolveInstructionData
# Set readOptions, again very standard.
$readOptions = New-Object Tridion.ContentManager.CoreService.Client.ReadOptions
# Publish the page tcm:69-6164-64 to target tcm:0-1-65537 with publish priority normal.
$client.Publish("tcm:69-6164-64", $publishIntructionData, "tcm:0-1-65537",
[Tridion.ContentManager.CoreService.Client.PublishPriority]::Normal, $readOptions)


Also see http://tridion.stackexchange.com/questions/4331/how-can-i-publish-a-page-by-using-powershell

As an example administrative task, take this question. For unclear reasons we need to publish a page every hour or so. This is not a Tridion best practice, but easy to automate. Just have a Windows Scheduled Task which runs powershell -file "C:\publish-a-page.ps1". That will do the trick effectively.

For future optimization: you could extend the Tridion PowerShell Modules to help you to perform this simple publishing action without setting up the PublishInstruction and ReadOptions and all.

No comments:

Post a Comment