Introduction

This article is related to a PowerShell script I recently published on the Technet Gallery : you can find it here : SharePoint 2007/2010/2013 : Export WSP infos to csv file and SharePoint list.

This PowerShell script gathers data associated with your SharePoint farm solutions (WSP) and export them to a csv file and a SharePoint list.

It has been tested on SharePoint 2007, SharePoint 2010 and SharePoint 2013.

The exported data are the following :

  • DisplayName,
  • Deployed,
  • ContainsCasPolicy,
  • ContainsGlobalAssembly,
  • ContainsWebApplicationResource,
  • DeployedServers,
  • DeployedWebApplications,
  • DeploymentState,
  • LastOperationDetails,
  • Status.
I will not copy the full script content here (216 lines), but just explain the major steps :
  • Browse the farm solutions (WSP),
  • Build the structure containing the data,
  • Create a SharePoint list,
  • Configure the list,
  • Export the data to the csv file,
  • Export the data to the SharePoint list,
  • Launch a browser to view the list.

Step 1 : Browse farm solutions (WSP)

 Get the farm (I used reflection to remain compatible with SharePoint 2007), and browse solutions using its "Solutions" property.

# Get farm
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm=[Microsoft.SharePoint.Administration.SPFarm]::Local

# Get solutions
foreach ($solution in $farm.Solutions)
{
    # Fill the data
    ...

Step 2 : Build the structure containing the data

Create an object and add all members, corresponding to all WSP properties to export.

# Build structure
$itemStructure = New-Object psobject 
$itemStructure | Add-Member -MemberType NoteProperty -Name "DisplayName" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "Deployed" -value ""
$itemStructure | Add-Member -MemberType NoteProperty -Name "ContainsCasPolicy" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "ContainsGlobalAssembly" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "ContainsWebApplicationResource" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "DeployedServers" -value ""
$itemStructure | Add-Member -MemberType NoteProperty -Name "DeployedWebApplications" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "DeploymentState" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "LastOperationDetails" -value "" 
$itemStructure | Add-Member -MemberType NoteProperty -Name "Status" -value ""

Step 3 : Create a SharePoint list

A dedicated function will create the list :
  • Gets the SPWeb corresponding to the site URL parameter,
  • Gets the list template (Generic List),
  • Try to get the list; if found, the list is deleted, then recreated using the "Generic List" list template.
function CreateSharePointlist
{
    # Get SPWeb
    try
    {$currentWeb = (New-Object Microsoft.SharePoint.SPSite($siteUrl)).OpenWeb()}
    catch
    {
        Write-Warning "The site '$siteUrl' cannot be found; the data will only be exported to the csv file."
        return
    }

    # Get list template (Generic template) 
    $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
    
    # Try to get list
    $global:exportList = $currentWeb.Lists[$listName]
 
    # Delete list if necessary
    if($global:exportList -ne $null)
    {$global:exportList.Delete()}

    # Create list
    $listId = $currentWeb.Lists.Add($listName,$listDescription,$listTemplate)
  
    # Get list
    $global:exportList = $currentWeb.Lists[$listName]

Step 4 : Configure the list

A dedicated function will create the columns added to the list.

The function presented at the previous step :
  • Adds all columns to the list,
  • Gets the default view,
  • Adds the columns corresponding to the data to the view and removes the "Attachments" column,
  • Updates the view.
function AddColumn($fieldType, $fieldLabel, $fieldRequired)
{
    # Add column
    $SPFieldType = [Microsoft.SharePoint.SPFieldType]::$fieldType 
    $exportList.Fields.Add($fieldLabel,$SPFieldType,$fieldRequired)
}

function CreateSharePointlist
{
    ...
    
    # Add columns
    $dump = AddColumn -fieldType "Boolean" -fieldLabel "Deployed" -fieldRequired $false
    $dump = AddColumn -fieldType "Boolean" -fieldLabel "ContainsCasPolicy" -fieldRequired $false
    $dump = AddColumn -fieldType "Boolean" -fieldLabel "ContainsGlobalAssembly" -fieldRequired $false
    $dump = AddColumn -fieldType "Boolean" -fieldLabel "ContainsWebApplicationResource" -fieldRequired $false
    $dump = AddColumn -fieldType "Note" -fieldLabel "DeployedServers" -fieldRequired $false
    $dump = AddColumn -fieldType "Note" -fieldLabel "DeployedWebApplications" -fieldRequired $false
    $dump = AddColumn -fieldType "Text" -fieldLabel "DeploymentState" -fieldRequired $false
    $dump = AddColumn -fieldType "Note" -fieldLabel "LastOperationDetails" -fieldRequired $false
    $dump = AddColumn -fieldType "Text" -fieldLabel "Status" -fieldRequired $false

    # Update list
    $global:exportList.Update()

    # Get default view
    $view = $global:exportList.Views[0]
    
    # Add / delete columns from the view
    try{$view.ViewFields.delete("Attachments")} catch{}
    $view.ViewFields.add("Deployed")
    $view.ViewFields.add("ContainsCasPolicy")
    $view.ViewFields.add("ContainsGlobalAssembly")
    $view.ViewFields.add("ContainsWebApplicationResource")
    $view.ViewFields.add("DeployedServers")
    $view.ViewFields.add("DeployedWebApplications")
    $view.ViewFields.add("DeploymentState")
    $view.ViewFields.add("LastOperationDetails")
    $view.ViewFields.add("Status")
    
    # Update default view
    $view.Update()

Step 5 : Export the data to the csv file

For each solution, a variable named "solutionsList" is filled with a structure item; at the end, it is exported to the csv file.

# Local variables
$solutionsList = $null
$solutionsList = @()

# Get solutions
foreach ($solution in $farm.Solutions)
{
    $solutionInfos = $itemStructure | Select-Object *; 
    $solutionInfos.DisplayName = $solution.DisplayName;
    $solutionInfos.Deployed = $solution.Deployed;
    $solutionInfos.ContainsCasPolicy = $solution.ContainsCasPolicy;
    ...
    $solutionsList += $solutionInfos;
}

# Export
$solutionsList | Where-Object {$_} | Export-Csv -Delimiter "$delimiter" -Path "$exportPath\$fileName.csv" -notype;

Step 6 : Export the data to the SharePoint list

In the same part of the code seen in the previous step (I removed the code associated to the csv file), create a new item (SPItem) for each solution found, and fill its properties.
Update the item at the end.

# Get solutions
foreach ($solution in $farm.Solutions)
{
    ...
    if ($exportToSharePoint)
    {
        # Create SPItem
        $newItem = $global:exportList.Items.Add()
    }

    if ($exportToSharePoint)
    {
        $titleField = $global:exportList.Fields | where {$_.internalname -eq "LinkTitle"}
        $newItem[$titleField] = $solution.DisplayName
    }
    
    if ($exportToSharePoint) {$newItem["Deployed"] = $solution.Deployed}
    if ($exportToSharePoint) {$newItem["ContainsCasPolicy"] = $solution.ContainsCasPolicy}
    $serverIndex = 0
    foreach ($server in $solution.DeployedServers)
    {
       $solutionInfos.DeployedServers += $solution.DeployedServers[$serverIndex].Name +"`n";
       $serverIndex++;
    }
    ...

    if ($exportToSharePoint)
    {
        # Update SPItem
        $newItem.Update()
    }
}

Step 7 : Launch a browser to view the list

At the end of the script, a browser is launched to display the list content.

if ($exportToSharePoint)
{
    write-host "The data have been copied in the SharePoint list." -foreground "green"
    
    $ie = New-Object -ComObject InternetExplorer.Application
    $ie.Navigate("$siteUrl/Lists/$listName/AllItems.aspx")
    try{$ie.Visible = $true} catch{}
}

Results

1. Script launched with all parameters
 

2. SharePoint list content


References


Other languages

This article is also available in the following languages :