Powershell and accessing Audiences
-
10-12-2019 - |
Question
I'm trying to build a fully functioning powershell script that will merge user accounts, loop through all the site collections and delete the 2 user accounts, then execute audience compilation. I've got parts 1 and 2 working after a bit of work, but I'm hung up on hooking into audiences. I've seen this post by Per, and it always bombs out on line 3 with:
New-Object : Exception calling ".ctor" with "1" argument(s): "Object reference not set to an instance of an object." At line:3 char:18 + $upm = New-Object <<<< Microsoft.Office.Server.UserProfiles.UserProfileManager($context); + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
If I do a $context
, it gives me a GUID of all zeroes. I'm developing this on my local windows 7 machine, full admin access to everything.
$site = Get-SPSite "http://localhost"
$context = Get-SPServiceContext $site;
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);
$audmgr = New-Object Microsoft.Office.Server.Audience.AudienceManager($context)
$aud = $audmgr.GetAudience("Irvine")
Solution
Bleh, I wasn't listed in the permissions of the service application even though I was listed as an administrator.
Image and fix courtesy of get-spscripts
OTHER TIPS
Someone over on the MSDN forums had the same issue. They fixed it by changing the way the context was instantiated. Basically do this:
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
$context = [Microsoft.Office.Server.ServerContext]::GetContext($mysite)
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)