4

I have been working with a PowerShell script to publish things, and it has worked perfectly.

I am now trying to extend it to be able to unpublish items as well, but have not found the right command or the right set of parameters.

The error that I get is:

Cannot find an overload for "Publish" and the argument count: "5".

My code is:

$publishIntructionData = New-Object Tridion.ContentManager.CoreService.Client.PublishInstructionData
$publishIntructionData.RenderInstruction = New-Object Tridion.ContentManager.CoreService.Client.RenderInstructionData 
if ($jobPublishAction -eq "unpublish")
{
    $publishIntructionData = New-Object Tridion.ContentManager.CoreService.Client.UnPublishInstructionData
}
$publishIntructionData.ResolveInstruction = New-Object Tridion.ContentManager.CoreService.Client.ResolveInstructionData 
$publishIntructionData.ResolveInstruction.IncludeChildPublications = $allPublications
if ($jobPublushAction -eq "publish")
{
    $publishIntructionData.ResolveInstruction.IncludeWorkflow = $true
}
if ($jobPublishAction -eq "unpublish")
{
    # $publishIntructionData.ResolveInstruction.ResolvePurpose = New-Object Tridion.ContentManager.Publishing.ResolvePurpose.UnPublish
    # Unpublish = 1
    $publishIntructionData.ResolveInstruction.Purpose = 1
}

if ($scheduled -eq $true) {
    $publishIntructionData.StartAt = $scheduledDateTime
}

$result = $client.Publish($Id, $publishIntructionData, $publicationTarget, $priority, $readOptions)
  • You have a typo... $jobPublushAction.. – Dominic Cronin Apr 21 '17 at 7:15
  • My personal preference is not to use a "magic number" for something like ResolveInstruction.Purpose. You can either simply say "Publish", or if you are addicted to intellisense [Tridion.ContentManager.CoreService.Client.ResolvePurpose]::Publish - by the time you get to the ::, you will be able to see the valid options – Dominic Cronin Apr 21 '17 at 7:17
5

I had to create a new object. $publishInstructionData retained the object type of PublishInstructionData even though I re-initialized it.

The changes to the code include:

# Create a different object altogether
$unpublishIntructionData = New-Object Tridion.ContentManager.CoreService.Client.UnPublishInstructionData
$unpublishIntructionData.ResolveInstruction = New-Object Tridion.ContentManager.CoreService.Client.ResolveInstructionData 
$unpublishIntructionData.ResolveInstruction.IncludeWorkflow = $true
# Call a different method
$result = $client.UnPublish($Id, $unpublishIntructionData, $publicationTarget, $priority, $readOptions)
  • 3
    It's hard to believe that a variable retains it value if you re-initialize it. I guess the only real issue was that you should use a different method for un-publishing. – Rick Pannekoek Apr 20 '17 at 20:24
  • I think the type of the variable was retained - I guess because powershell is weirdly typed (stackoverflow.com/questions/115643/…) – Rob Stevenson-Leggett Apr 21 '17 at 12:29
5

If you'd excuse the shameless plug, I recommend that you start using the open-source Tridion PowerShell Modules that I maintain.

With the Tridion-CoreService module imported, you could just do this:

Unpublish-TridionItem -Id $Id -Target $publicationTarget -Priority $priority;

And similar for Publish-TridionItem. There is already a decent amount of features available, with more to come. If something is missing from the modules, I take pull requests :) Or you could just ask for it and I will try to add it as soon as possible.

If everybody used and contributed to that project, over time we'd build a comprehensive library of functionality that benefits everyone who needs/wants to work with SDL Web from within PowerShell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.