Get persisted objects through PowerShell

Occasional Contributor

Dear colleagues,

I cannot reach persisted object through PowerShell.

Through similar C# code I can reach the persisted object.

The reason seems to be in the type parameter of the SPFarm.GetObject method.

In PowerShell is used as type: [SolutionWithPersistedObjects.Folder.PersistedObjectClass]

In C# is used as type: SolutionWithPersistedObjects.Folder.PersistedObjectClass.Settings.GetType()


PowerShell code:

$persistedObjectName = "Persisted Object Name"

$dllPath = "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\SolutionWithPersistedObjects\v4.0_1.0.0.0__ID...\SolutionWithPersistedObjects.dll"

$assembly = [System.Reflection.Assembly]::LoadFrom($dllPath)

if ($assembly -ne $null)
{
Write-Host "Assembly exists."

# Get the farm
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

if ($farm -ne $null)
{
Write-Host "Farm exists."

$persistedObject = $farm.GetObject($persistedObjectName, $farm.Id, [SolutionWithPersistedObjects.Folder.PersistedObjectClass])

if ($persistedObject -ne $null)
{
# Do some actions with the $persistedObject
}
else
{
# This if statement still ends here. PowerShell can not find the $persistedObject, even when this persisted object exists.
# Similar code is working in C#. In C# is the Type [SolutionWithPersistedObjects.Folder.PersistedObjectClass] replaced with SolutionWithPersistedObjects.Folder.PersistedObjectClass.Settings.GetType()
}
}
}


Can you help me please?

Thank you very much.

Ladislav Stupak

0 Replies