1

Attempting to use PowerShell to obtain some social metrics from our SharePoint farm. One metric I wanted to get was the average number of sites users are following. The following script runs without errors, but the GetFollowedCount for each user returns 0 sites, even if they are following many.

$site = Get-SPSite https://mysite.server.com
$servContext = Get-SPServiceContext($site)
$profileMgr = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($servContext)
$typeSite = [Microsoft.Office.Server.Social.SPSocialActorTypes]::Sites
$profiles = $profileMgr.GetEnumerator()
$count = 0
$siteFollows = 0
foreach ($person in $profiles)
{
    if($person.PersonalSite.Url)
    {
        $followMgr = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($person)
        $count++
        $thisSiteFollows = $followMgr.GetFollowedCount($typeSite)
        $siteFollows = $siteFollows+$thisSiteFollows
        Write-Host "Sites Followed by " $person.AccountName ": " $thisSiteFollows
    }
}
$averageSites = $siteFollows/$count
Write-Host "Average Sites Followed: " $averageSites

Need help figuring out what it is I'm missing. Thanks.

2
  • are your sure you getting the users in $profile?
    – Waqas Sarwar MVP
    Jun 19, 2014 at 19:26
  • Yes. It was a permissions issue (see my answer). Thanks for taking some time on this question.
    – Todd
    Jun 19, 2014 at 20:46

1 Answer 1

0

It's a permissions issue. If you run the script under the farm admin account, it works as expected. To fix the script, you have to run it within a [Microsoft.Sharepoint.SPSecurity]::RunWithElevatedPrivileges block:

[Microsoft.Sharepoint.SPSecurity]::RunWithElevatedPrivileges({
  #script goes here
})
2
  • Thanks for your answer, Todd. Just to clarify, you wrapped the entirety of the script in your question in an elevated block, or just a select portion of the script? Jun 25, 2014 at 3:16
  • Sorry for missing your question before. Yes, the entire script was inside the RunElevated block. I won't say that is strictly necessary, but it's what worked for me.
    – Todd
    Jul 22, 2014 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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