7

I would like to query the broker database from a PowerShell script, but don't know where to start...

from the PS prompt* I manage to load the Tridion.ContentDelivery.Interop.dll

*This is done from a working and configured HttpUpload directory with the right DLLs, JARs and XMLs.

add-type -path .\tridion.contentdelivery.interop.dll
$q = new-object tridion.contentdelivery.dynamiccontent.query

and this results in errors... enter image description here

So how to query content from the broker database with PS?

2013-10-2 Update: Some additional information after comments

In Java one would use something like:

import com.tridion.broker.querying.Query;
import com.tridion.broker.querying.criteria.Criteria;
import com.tridion.broker.querying.criteria.content.SchemaTitleCriteria;

Criteria criteria = new SchemaTitleCriteria(MY_SCHEMA);
Query query = new Query();
query.setCriteria(criteria);
try {
    String[] result = query.executeQuery();
} catch (StorageException e) { ... }

In PowerShell one would use something like this to call Oracle functionality:

add-type -path .\Oracle.DataAccess.dll
$conn = new-object oracle.dataaccess.client.oracleconnection("Data Source=...; User Id=...; Password=...");
$conn.open()

Now my challenge is not to do something else and write C# code or OData webservice but to find out:

  • How to load the correct .NET CD DLL in PowerShell
  • How to instantiate the right objects

like one would do in either Java or C#.

  • 2
    This will be tricky, as the Tridion CD core runs inside a JVM hosted by IIS... might be easier to add an odata webservice on top of your database – Nuno Linhares Sep 30 '13 at 13:00
  • The direction you mention is then: my_query.aspx >> IIS / .NET >> JVM >> cd_core.jar. and the route I try to do is: my_query.ps1 >> .NET >> JVM >> cd_core.jar. My aim is to solve it via PS and .NET. – user348 Sep 30 '13 at 14:44
  • When calling the CD API from .NET, you should have a config and lib folder as subfolders of the current folder of your application. These folders should contain the cd-config files and jar-files, respectively. I'm not saying this will work from PS though.. – Quirijn Sep 30 '13 at 15:09
  • It's probably easier to use the OData webservice or write a custom exe in C# which you then call from PowerShell. – Rob Stevenson-Leggett Sep 30 '13 at 15:36
  • 1
    Post the error(s) too since this might be configuration, license, etc problem rather than just syntax. – Alvin Reyes Sep 30 '13 at 16:08
7

It's actually possible, but requires doing some hackwork. Use at own risk!

Write-Host 'Hello World! Lets hack a Tridion Jvm!'

# You'll need the assemblies listed below, plus xmogrt.dll
# in your executing directory

add-type -path '.\netrtsn.dll'
add-type -path '.\Tridion.ContentDelivery.Configuration.dll'
add-type -path '.\tridion.contentdelivery.dll'
add-type -path '.\tridion.contentdelivery.interop.dll'

# Let JuggerNet know we need the config and lib stuff
# This will only work if a) you're in TRIDION_HOME, 
# b) your config and lib dirs are below this executing dir.

[Tridion.ContentDelivery.Web.Jvm.ConfigurationHook]::Init()

# Corny way of tricking the process in loading a VM. Types injected 
# through depedency injection are not loaded.
# You can see that if you remove the $loader = part in the line below

$loader = [Codemesh.JuggerNET.JvmLoader]::GetJvmLoader('true','true')

# lets get all tcm ids of publication id 5
# below two lines need to be 1 line (formatting reasons)
$publicationCriteria = 
new-object Tridion.ContentDelivery.DynamicContent.Query.PublicationCriteria(5)

# below two lines need to be 1 line (formatting reasons)
$query = 
new-object Tridion.ContentDelivery.DynamicContent.Query.Query($publicationCriteria)

$results = $query.ExecuteQuery()

$i=0
$cmf = new-object Tridion.ContentDelivery.Meta.ComponentMetaFactory(5)


do {
    Write-Host $results[$i]
    # Get the meta, just for fun
    Write-Host $cmf.getMeta($results[$i]).Title
    Write-Host $cmf.getMeta($results[$i]).Author
$i++
}
while ($i -lt $results.count)

Write-Host 'Cleaning up.'
# if $jvm is null the loader object will fail silently. 
# You should also be able to just close the process to unload the jvm.
$jvm = $loader.JvmIfCreated
$loader.UnloadCleanup($jvm)
  • Thanks! you'll need to run it from an x86 PowerShell since the DLLs are 32-bit, all lines work well, until the line $publicationCriteria = new-object Tridion.ContentDelivery.DynamicContent.Query.PublicationCriteria(5). It fails with the error message: New-Object : Exception calling ".ctor"... "The type initializer for 'Tridion.ContentDelivery.DynamicContent.Query.Criteria' thre an exception." ... + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException – user348 Oct 3 '13 at 11:46
  • 1
    It depends on whether your dlls are the 64 bit or the 32 bit ones. Powershell also comes in either variety, so match the one you load with the bit version of your Tridion DLLs. – Raimond Oct 3 '13 at 12:05

Your Answer

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