Randomly Update User Profiles from PowerShell – What the heck?
July 1, 2011 2 Comments
Whilst demonstrating SharePoint and in particular the communities capabilities it provides, it can sometimes be hard for users to grasp the concepts involved when they are presented with an empty profile store and nothing on a users activity feed.
To help the demonstration to become a richer experience it is usually necessary to load dummy data into the environment. However, when it comes to data in the Newsfeed (Activity Feed), which sorts activities from the most recent to the oldest, it’s important to have up to date demo data at all times.
So how is this achieved without manually creating new data just before each demonstration? PowerShell!
The script below shows you how you can update user profile data at random which can be used to simulate multiple users on the system updating their status. The same technique can be used to update other profile properties (which would consequently create and activity) or to tag content.
The script uses an XML file to determine which user profiles can be updated. In a separate post I’ll show you how this XML file can be used to provision profiles, but for the time being we are simply interested in the list of users. Example XML users file:
You’ll see that each user is simply identified by their account name.
Along side this list of users, you’ll also need a list of possible status values, here’s my list I’ve saved into another XML file called quotes.xml:
Now here comes the PowerShell. Firstly the script connects to the my site host and retrieves the UserProfileManager:
## Load the SharePoint Snapin so the script can be executed from PowerShell editor
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue## Create a random number
$random = New-Object system.random#Set up my site host url
$mySiteUrl = "http://my.sharepoint.com"#Get site objects and connect to User Profile Manager service
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
Next, the script opens the user XML file and picks a user at random:
# pick an account at random
$InputFile = "C:\Documents\SharePoint\Demo\users.xml"
[xml]$xml = (Get-Content $InputFile)
$userCount = $xml.Users.User.Count
$pickUser = $random.next(0,$userCount)
$count = 0
$victim = "";
foreach ($user in $xml.Users.User)
{
if ($count -eq $pickUser)
{
$victim = "bc01\" + $user.Name
break;
}
else
{
$count++
}
}
Next, we use the same pattern to pick a quote at random:
# pick a quote at random
$InputFile = "C:\Documents\SharePoint\Demo\quotes.xml"
[xml]$xml = (Get-Content $InputFile)
$quoteCount = $xml.Quotes.Quote.Count
$pickQuote = $random.next(0,$quoteCount)
$count = 0
$saysthis = "";
foreach ($quote in $xml.Quotes.Quote)
{
if ($count -eq $pickQuote)
{
$saysthis = $quote.Text
break;
}
else
{
$count++
}
}
Now we have a random user and a random quote we update the user profile status with the quote:
#Get user profile and change the value
$up = $profileManager.GetUserProfile($victim)
write-host "$victim says " -nonewline; write-host "$saysthis" -foregroundcolor Blue
$up["SPS-StatusNotes"].Value = $saysthis
$up.Commit()$site.Dispose()
If you stitch the script snippet pieces together you’ll have a script that results is seemingly genuine status updates that appear to come from numerous users:
I’ve appended this script to my SharePoint start-up and shut-down scripts so that every time I start or stop my SharePoint I will accumulate status updates. Happy My Site demoing!
I hope this helps…