Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / PowerShell
Article

Create HTML Report of Facebook URLs using PowerShell

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Oct 2012CPOL5 min read 17.5K   78   4  
In this article we will explore a scenario of adding a custom User Profile property and generating a report based on it.

Introduction

In this article we will explore a scenario of adding a custom User Profile property and generating a report based on it. The challenge here is that the report generation should be done with PowerShell as it is easier to create, execute and manage for an Administrator.

Scenario

For each of the user in the company requires a property to capture their Facebook URL. This property can be edited through the My Profile page by the respective user.

Image 1

An HTML report has to be generated for the all users with the URL value assigned. The report should be in PowerShell so that Administrators can modify it. The report should look like:

Image 2

Solution

As each user needs to be assigned with a custom property we have to use the User Profile Service Application.

The implementation steps are the following:

  1. Create a User Profile property

  2. Update users with Facebook URL

  3. Create PowerShell script

  4. Generate report

User Profile Property

As the first step we need to create a new User Profile Property. SharePoint 2010 provides User Profile Properties like:

  • Account Name

  • Email

  • Web site

  • Hire date etc.

But these properties may not be enough for your Organizational Needs!

We can create Custom User Profile Property to address the needs. In this particular scenario we need to add a property of type string to store Facebook URL.

Note: The User Profile Service Application is the service application responsible for storing the User Profiles, Synchronization, My Site Configuration etc.

In order to create custom user profile property, open Central Administration > Manage service applications > User Profile Service Application > Manage.

Image 3

In the appearing page you can see People, Synchronization, Organization, My Site Settings groups. From the People group select Manage User Properties.

Manage User Properties

This page displays the existing User Profile Properties. You can see the properties listed as shown below:

Image 4

Using this screen we can Add / Edit / Delete properties. Click the New Property link from the top for adding the Facebook URL property.

In the appearing page enter the details as shown below:

Image 5

Please note that the Name represents programmatic purpose and Display Name represents formatted display purpose. Enter the Type as string and Length as 255.

We are expecting the following values for the Facebook URL property:

  • http://www.facebook.com/UserA
  • http://www.facebook.com/UserA

After setting the Property values click the OK button to save changes. Now our new property should be get listed in the Properties page > Custom Properties section.

Manage User Profiles

Now we need to set the value for this property. The value can be set through:

  • My Site > My Profile page of each user

  • Central Administration by Administrator

We can quickly set the profile properties through Central Administration > Manage service applications > User Profile Service Application > Manage.

Image 6

In the appearing page click the Manage User Profiles link as highlighted above. You will get the following page:

Image 7

Enter the user name in the text box and click Find button. For the results retrieved, choose the Edit My Profile context menu to set the Facebook URL property.

Image 8

Save the changes and repeat this for a couple of Profiles.

Now we are ready with enough Profiles assigned with Facebook URL property.

Create PowerShell Script

We can start with creating the PowerShell script. For writing the script we can use the editor included with Windows.

Image 9

The script should contain the following activities:

  1. Enumerate all the User Profiles

  2. Write the Account Name, Facebook URL to screen

  3. Write the Account Name, Facebook URL to HTML file

Following is the script which achieves the same:

C#
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles") 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$RootSite = "http://localhost";
$site = new-object Microsoft.SharePoint.SPSite($RootSite); 
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site); 
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()

$array = @()

write-output "Account Name, Facebook Url"

foreach($profile in $AllProfiles) 
{ 
$FacebookUrl = $profile["FacebookUrl"].value
$AccountName = $profile["AccountName"].value

$IsEmpty = [string]::IsNullOrEmpty($FacebookUrl)

if ($IsEmpty -ne "False")
{
# Create object and store in array
$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -name "Account Name" -value "$AccountName"
$obj | Add-Member -type NoteProperty -name "Facebook Url" -value "$FacebookUrl"
$array += $obj

# Output to Screen
write-output "$AccountName, $FacebookUrl"
}
}

# Output to HTML, sorted
$array | Select-Object | Sort-Object -Descending -Property "FacebookUrl" | 
         ConvertTo-Html -title "Active Users" | Set-Content .\FacebookUsers.html

$site.Dispose()

The code is explained below:

Image 10

The first three lines represent:

  • Adding namespaces

The next five lines represent the creation of the SPSite, UserProfileManager instances:

  • new-object keyword represents new operator in C#

  • $ symbol is used to create variables

  • GetEnumerator() is invoked on ProfileManager instance

The foreach loops over the $AllProfiles variable and inside it:

  • Profiles with Facebook URL empty are discarded

  • New object is created with properties Account Name, Facebook URL

  • Values are assigned to the new object

  • Object is added to the Array

  • Writes the row to SCREEN (Writing to HTML follows the foreach loop)

Please note that the following statement adds object to the array:

  • $array += $obj

The write-output statement:

  • Writes the text to the Output screen (ISE Editor)

Note: Under the hood, the script is interpreted and .Net Reflection is used to invoke the methods & properties. For example, new-object maps to new in C# and invokes Activator.CreateInstance method of Reflection.

Operators

The comparison operators available in PowerShell are given below:

Operator

Description

-eq

Equal to

-lt

Less than

-gt

Greater than

-ge

Greater than or Equal to

-le

Less than or equal to

-ne

Not equal to

-like

Like (* for wildcard)

-contains

Contains (used for collections)

More operators can be found in the References section.

Comments in PowerShell

# is used to represent a single line comment

Arrays in PowerShell

The array is created using @ symbol

$arrayVariable = @()

Objects in PowerShell

A class is created and the properties are assigned using the code below:

$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -name "Account Name" -value "$AccountName"
$obj | Add-Member -type NoteProperty -name "Facebook Url" -value "$FacebookUrl"

The –name tag specifies the property name of object. The –value tag specifies the property value of object.

Formatted HTML Output in PowerShell

Creating an HTML file includes adding HTML tags like <table>, <tr>, <td> etc. PowerShell Cmdlet named ConvertTo-HTML helps us in performing this job.

Executing Code

You can execute the code in ISE Editor and view the result on screen.

Image 11

The HTML file generated will be in the folder highlighted above and varies from machine to machine. On opening the file in Internet Explorer you can view the following results.

Image 12

This concludes our article on HTML report generation using PowerShell.

References

http://tinyurl.com/2bc72ce

Summary

In this article we have explored a scenario of creating custom User Profile property and HTML report generation using PowerShell. To summarize, the following are the aspects we have explored:

  • User Profile Service Application

  • Creating a custom User Profile property

  • PowerShell cComparison operators

  • Using PowerShell for HTML report generation

The source code attachment contains the script we have discussed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
-- There are no messages in this forum --