PowerShell: Getting Sandbox Solutions in a SharePoint Web Application


Introduction

Like all good SharePoint developers we have been using Sandbox Solutions where we can. Of course one of the problems with Sandbox solutions is that its difficult to find out what you have installed and where.
This is particularly important if you have a Sandbox solution that is used for branding and is installed into a number of site collections.
Therefore I built a script which will retrieve sandbox solutions in a web application.
You can filter which sandbox solutions by the name.
Please fill free to use and modify the script, though make sure you test it before using in Production.
If you do make some enhancement that you think is useful, please let me know as would love to see what additional functionality is useful.

How does the script works

The script is a PowerShell script which works in the following way:
  • The script is run by providing the Url for the Web Application
  • The script will enumerate through all the site collections in the web application and creates an array object which has two properties, SiteCollection and Solution
  • Once all site collections have been enumerated then the array will be outputted.
This way you can use the output for further processing steps. For example, to filter the results for one solution and upgrade the solutions in all the site collections returned.
The script is shown below:

param
(
[Parameter(Mandatory=$false, HelpMessage='Solution Name')]
[string]$SolutionName="",
[Parameter(Mandatory=$false, HelpMessage='Solution Name')]
[string]$WebApplicationUrl="")$memoryAssignment = Start-SPAssignment;
$numberOfSolutionsFound = 0;
$webApplication = Get-SPWebApplication -Identity $WebApplicationUrl -ErrorAction SilentlyContinue -AssignmentCollection $memoryAssignment;
if($webApplication -ne $null)
{
#enumerate through site collections in web application
$allSites = Get-SPSite -WebApplication $webApplication -Limit ALL -Confirm:$false -AssignmentCollection $memoryAssignment;
foreach($checkSite in $allSites)
{
#Write-Output "Checking Site " $checkSite.Url " for solution " $SolutionName;
if($SolutionName -eq "")
{
$checkSolutions = Get-SPUserSolution -Site $checkSite -AssignmentCollection $memoryAssignment;
foreach($solution in $checkSolutions)
{
$output = New-Object -TypeName "System.Object";
Add-Member -InputObject $output -MemberType NoteProperty -Name "Solution" -Value "";
Add-Member -InputObject $output -MemberType NoteProperty -Name "SiteCollection" -Value "";
$output.Solution = $solution;
$output.SiteCollection = $checkSite;
Write-Output -InputObject $output;
$numberOfSolutionsFound++
}
}
else
{
$checkSolution = Get-SPUserSolution -Identity $SolutionName -Site $checkSite -ErrorAction SilentlyContinue -AssignmentCollection $memoryAssignment;
if($checkSolution -ne $null)
{
$output = New-Object -TypeName "System.Object";
Add-Member -InputObject $output -MemberType NoteProperty -Name "Solution" -Value "";
Add-Member -InputObject $output -MemberType NoteProperty -Name "SiteCollection" -Value "";
$output.Solution = $checkSolution;
$output.SiteCollection = $checkSite;
Write-Output -InputObject $output;
$numberOfSolutionsFound++
}
}
}
}</p>
<p style="direction: ltr; margin-top: 0; margin-left: 0; width: 7.5875in;">Write-Output "Found $numberOfSolutionsFound Instances of Solution $SolutionName";
Stop-SPAssignment $memoryAssignment;</p>
<p style="direction: ltr; margin-top: 0; margin-left: 0; width: 7.5875in;">

 

How to execute the script

 To execute the script do the following:
  • Logon to SharePoint 2013 server
  • Run the SharePoint Management Shell
    • .\Get-SPUserSolutionInWebApplication -WebApplicationUrl https://sharepoint
    • There is an optional parameter -SolutionName which allows you to only return the solutions with a particular name

The script will output an array of Solution Objects with an associated Site Collection object.

We can assign a value to the output and then do some further processing using that value.

Here is an example of the output from the script without assigning the result to a variable.

getusersolutionsfromwebapp

To do something more useful then assign the output to a variable. You can then enumerate through the variable array.

For example, the following screenshot shows the output being assigned to a variable $solutions and then it is being enumerated using the following:

$solutions | %{Write-Host $_.Solution.Name “in” $_.SiteCollection.Url}

enumerate-usersolutionarray

Downloads

If you would like to try out the PowerShell script, then you can download the script here:

Get-SPUserSolutionInWebApplication.zip

Please fill free to use and modify the script, though make sure you test it before using in Production.

Also let me know if you find it useful and if you have made some cool changes let me know as always looking for ways to make things better!

PowerShell Script: Add user as Site Collection Admin to all sites in Web Application


 

Introduction

 

A few days ago I had a request from one of the SharePoint team. Could we give him Site Collection Admin rights to all site collections in a web application.

Now one method would be to use a Web Application User Policy (see below) and give them the full access permission.

WebApplicationPolicy

However, we didn’t want to take that approach.

So we looked at using a PowerShell script. The important point was that we did not want to assign the permission using the Owner or SecondaryContact properties of the SPSiteobject. Instead we just wanted to add the user as the site collection admin.

When using the user interface this is achieved by doing the following:-

  • Browse to the site collection
  • Click Site Settings
  • Click Site collection administrators (Under Users and Permissions)
    • Add the user to the list and click OK.
      After a bit of investigation using PowerShell I could see how this permissions was set.

 

Solution

So to the solution, how is a user configured as a Site Collection Admin?

Well it turns out that its based on the following property, SPUser.IsSiteAdmin. Site Collection Administrators have the IsSiteAdmin property set to true.

 

Once that information had been understood, then the script was relatively easy and the following script was created:-

param
(
	[Parameter(Mandatory=$true, HelpMessage='username in format DOMAIN\username')]
	[string]$Username = "",
	[Parameter(Mandatory=$true, HelpMessage='url for web application e.g. http://collab')]
	[string]$WebApplicationUrl = ""

)

Write-Host "Setting up user $Username as site collection admin on all sitecollections in Web Application $WebApplicationUrl" -ForegroundColor White;
$webApplication = Get-SPWebApplication $WebApplicationUrl;

if($webApplication -ne $null)
{

foreach($siteCollection in $webApplication.Sites){
    Write-Host "Setting up user $Username as site collection admin for $siteCollection" -ForegroundColor White;
    $userToBeMadeSiteCollectionAdmin = $siteCollection.RootWeb.EnsureUser($Username);
    if($userToBeMadeSiteCollectionAdmin.IsSiteAdmin -ne $true)
    {
        $userToBeMadeSiteCollectionAdmin.IsSiteAdmin = $true;
        $userToBeMadeSiteCollectionAdmin.Update();
        Write-Host "User is now site collection admin for $siteCollection" -ForegroundColor Green;
    }
    else
    {
        Write-Host "User is already site collection admin for $siteCollection" -ForegroundColor DarkYellow;
    }

    Write-Host "Current Site Collection Admins for site: " $siteCollection.Url " " $siteCollection.RootWeb.SiteAdministrators;
}
}
else
{
	Write-Host "Could not find Web Application $WebApplicationUrl" -ForegroundColor Red;
}

The PowerShell script accepts the following parameters:-

  • -UserName – the user to add as a site collection admin (DOMAIN\username)
  • -WebApplication – the URL to the Web Application that should be updated

The script tries to get resolve the Web Application. The script then runs through each site collection in the web application and ensures that the user can be found in the site collection.

If the user is not already a site collection admin then the property is updated and the user object is saved.

The script is not perfect and could have a bit more exception handling, for example the Get-SPWebApplication call does not check the return value.

 

Anyway the link to the PowerShell script is below (just rename the file extension from .txt to ps1):-

Set-UserAsSiteCollectionAdminOnWebApplication.txt