Create HTML Report on Facebook Urls Using PowerShell

In this article we can explore addition of 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 for an Administrator to create, execute and manage.

Scenario

Each 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.

Scenario-using-PowerShell.jpg

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

active-users.jpg

Solution

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

The implementation procedure is as in the following:

  1. Create a User Profile Property
  2. Update Users with Facebook URL
  3. Create PowerShell Script
  4. Generate Report

User Profile Property

In 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 a 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.

To create a custom user profile property, open Central Administration > Manage service applications > User Profile Service Application > Manage.

User-Profile-Service-Application-Manage.jpg

In the page that appears you will 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 will see the properties listed as shown below:

Manage-User-Properties.jpg

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 page that appears enter the details as shown below:

Facebook-Url-property.jpg

Please note that the Name is for programmatic purposes and Display Name is for formatted display purposes. Enter the Type as a 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 the changes. Now our new property should be 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.

Manage-User-Profiles.jpg

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

Manage-User-Profiles-link.jpg

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

Edit-My-Profile-context-menu.jpg

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

Now we are ready with enough Profiles assigned with the 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.

Create-PowerShell-Script.jpg

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

The following is the script that does that:

[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 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:

code-is-explained.jpg

The first 3 lines represent:

  • Adding namespaces

The next 5 line represents creation of 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 the ISE Editor and view the result on the screen.

Executing-Code.jpg

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.

active-users.jpg

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 & HTML Report generation using PowerShell. To summarize with, following are the aspects we have explored:

  • User Profile Service Application

  • Creating custom User Profile Property

  • PowerShell Comparison Operators

  • Using PowerShell for HTML Report Generation

The source code attachment contains the script we have discussed.