SharePoint – PowerShell Script to Remove Users from Site Collection

Site collections still have users that are either disabled or deleted from Active Directory. SharePoint does not have anything out of the box that will clean up these. The reasons to clean these users up is to avoid getting them as a result in the people picker when they should not be showing up. I do have two scripts that will clean these users up. This will be something you want to test out since this is a delete function (leave $RemoveUsers = $false).

WARNING: This will remove users from site collections. This will remove their alerts, personal views, unique permissions, etc. The only way to revert is to restore the site collection or content database from backup.

Just a bit of information about your site…

This script does require some modification to tailor it for your environment. The service account is needed even if it’s the local domain. This was designed this way, so you can use this script for sites that have users from different domains. As stated above, $RemoveUsers is the switch to make this to script to remove the users that were not found in Active Directory. By default, this is set to $false. After everything is set, run “GetUsers”.

CSV is created.

The CSV will be created if any users are in the site collections that are no longer in Active Directory or are disabled. Review this before feeding it through the second script. There might be some accounts you may have false positives or just want to remove a few entries. This script is more straight forward since we are taking that CSV and removing the user from the URL specified in the CSV. I would recommend backing up the content database(s) that could be affected by this script. DeletedADUsersFromsites1.1.ps1 can be downloaded here
The second script can be downloaded here.

Remove-SPTrustedIdentityTokenIssuer : The trusted login provider is in use and cannot be deleted

 

I have a post about the Migration from Windows to ADFS which utilizes the creation of the -UseDefaultConfiguration switch. A few days ago, I created a Trusted Identity Provider with -UseDefaultConfiguration. I did not need it in my test farm anymore, so it was time to delete it.

This failed with this exception:

PS C:\windows\system32> Remove-SPTrustedIdentityTokenIssuer foo

Remove-SPTrustedIdentityTokenIssuer : The trusted login provider is in use and cannot be deleted.

At line:1 char:1

+ Remove-SPTrustedIdentityTokenIssuer foo

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidData: (Microsoft.Share…dentityProvider:SPCmdletRemoveSPIdentityProvider) [Remove-SPTrustedIdentityTok

enIssuer], InvalidOperationException

+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletRemoveSPIdentityProvider

 

Only Two Checks

There are only two reasons this exception is tossed when trying to remove the Trusted Identity Token Issuer.

  1. Use in the web application authentication providers
  2. Claim Provider with the same name as your Trusted Identity Token Issuer

The first is the obvious choice to check since the Trusted Identity Token Issuer cannot be deleted if used by a web application. If you have numerous web applications with many zones to check, this can take a long time. The PowerShell script below numerates all the web applications and their zones to display the authentications that are enabled in those zones.

 

#Use this to enumerate all authentication providers in use for every zone for every web application

#author: adamsor

 

$log = “C:\logs\authproviders.txt”

$wa = Get-SPWebApplication

foreach($webapp in $wa)

{

$aams = $webapp.alternateurls

foreach($aam in $aams)

{

Write-host $webapp.url $aam.zone -ForegroundColor DarkGreen

$url = $webapp.Url

$zone = $aam.Zone

“$url $zone” | out-file $log -Append -noclobber

Get-SPAuthenticationProvider -WebApplication $webapp -Zone $aam.zone

Get-SPAuthenticationProvider -WebApplication $webapp -Zone $aam.zone | Out-File $log -Append -noclobber

}

 

}

 

Example of the output:


I checked the authentication providers. I have ADFSAdamsor in use and you can see the web application URL and zone at the top. Any that would come up we would just remove the that zone from using the Trusted Identity Token Issuer.

 

Claim Provider

The second check for Remove-SPTrustedIdentityTokenIssuer is Claim Providers that have the same name as the Trusted Identity Token Issuer.

Get-SPClaimProvider will display all the claim providers installed on the farm.

As the screenshot above shows, I have the claim provider that was created with -UseDefaultConfiguration. I was able to remove the claim provider then removed the Trusted Identity Token Issuer.

Remove-SPClaimProvider <NAME>

Remove-SPTrustedIdentityTokenIssuer <NAME>

Unable to start User Profile Service Application – Starting

The User Profile Synchronization service for SharePoint 2010 or 2013 can fail to start for numerous reasons. This post is for when the User Profile Service Application(UPA) is stuck on Starting. This prevents anybody from creating connections or syncing users.


This will prevent the User Profile Service Application from functioning properly. ULS shows the topology.svc tossing this “The requested application could not be found” with EventID evr4.

We can try to start the by running the Provision method to get the service application Started.

$upa = Get-SPServiceApplication <GUID of Service Application>

$upa.Provision()

This did fail and in the ULS logs we found this entry.

05/26/2018 11:13:46.90    PowerShell_ISE.exe (0x4AF4)    0x48F4    SharePoint Portal Server    User Profiles    ojxm    High    SSP administration site owner is ‘i:0#.w|oldaccount\deleted’.    018afcc8-f800-0001-5e35-00e1678bd301

SSP administration is a blast from the past. The reference is back to 2007 but service applications replaced the SSP in SharePoint 2010. This entry is stating we’re looking for owner of the Central Admin. This value can be exposed by using PowerShell as well.

$site = Get-SPSite http://centralAdmin:1234

$site.Owner

 

Or the UI with Site Settings and Site Collection Administrators

Or the Change site collection administrators in Central Admin

 

Find that account there and replace it with a valid account then run the Provision() PowerShell.

 

 

SAML roles and SharePoint 2016/2013 with OAuth

Roles (security groups) with SAML/ADFS will not work with OAuth without some more configuration and patching. OAuth affects 2013 Workflows, Office Web Apps, Provider Hosted Apps, Cross Farm Publishing/Consuming scenarios, Hybrid, etc. There are a few steps and requirements that are needed for this to work. KB 3203164 has some great information on this topic on setting the GroupClaimType.

NOTE: SharePoint 2013 must at (or later) March 2016 CU (15.0.4805.1000). SharePoint 2016 introduces the GroupClaimType with the September 2017 (16.0.4588.1000, Feature Pack 2) but we suggest deploying January 2018 PU (16.0.4639.1001) due to the regression causing Windows Claims Oauth augmentation to fail.

User Profiles are Key

User profiles are utilized when augmenting the role claims. The groups and users must be imported and synced within the User Profile Service application.

Map your Identifier claim

Run this PowerShell script to find the identifier claim:

$tips = Get-SPTrustedIdentityTokenIssuer

foreach($tip in $tips)

{

$name = $tip.Name

$claims = $tip.ClaimTypeInformation

foreach($claim in $claims)

{

If($claim.IsIdentityClaim -eq $true)

{

Write-Host -ForegroundColor DarkGreen “The Identity Claim is listed below for $name“

$claim } }}

Example of the script results when Email was the Identifier claim:

In the User Profile Service Application à Manage User Properties, edit the properties of the Claim User Identifier.

SharePoint 2016’s Active Directory Import automatically maps Claim User Identifier to saMAccountName. We would need to change that to correct identifier property in Active Directory. Any change in mappings will require a full sync.

Adding a new mapping for the identifier claim:

Group Membership in User Profiles

There are two ways to get this information – SQL or PowerShell. The PowerShell one will show nested groups unlike the SQL query.

Run this against your Profile Database and modify the domain\username to fit the user profile that we are searching for. This information would be in the UPA when searching for a User Profile.

SQL – SharePoint 2016

Select upa.UserProfile_Full.NTName, upa.MemberGroup.DisplayName as ‘Group Name’, upa.MemberGroup.SourceReference

From upa.UserProfile_Full with(nolock) JOIN upa.UserMemberships on upa.userprofile_full.RecordID = upa.UserMemberships.RecordID

Join upa.MemberGroup on upa.Usermemberships.MemberGroupId = upa.Membergroup.ID

Where NTName = ‘domain\username’

SQL – SharePoint 2013

Select UserProfile_Full.NTName, MemberGroup.DisplayName as ‘Group Name’, MemberGroup.SourceReference

From UserProfile_Full with(nolock) JOIN UserMemberships on userprofile_full.RecordID = UserMemberships.RecordID

Join MemberGroup on Usermemberships.MemberGroupId = Membergroup.ID

Where NTName = ‘domain\username’

Bdoe has 5 group memberships in the UserProfile service application.


PowerShell

$site = Get-SPSite “http://adamsor-2013upa”

$userprofile = “i:05.t|adfs|user@contoso.com”

$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);

$pm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

$up = $pm.GetUserProfile($userprofile)

[xml]$xml = ($up.Memberships.GetDataSet()).getxml()

$xml.SelectNodes(“//Title”)

Output:

GroupClaimType

This property shown when running Get-SPTrustedIdentityTokenIssuer

As KB 3203164 states, we only have two options, the Role or GroupSID for claims.

PowerShell:

#Create a variable containing the SPTrustedIdentityTokenIssuer object
$issuer = Get-SPTrustedIdentityTokenIssuer

#Set the GroupClaimType property to the Role claim type, do not run for GroupSID claim type
$issuer.GroupClaimType = [Microsoft.IdentityModel.Claims.ClaimTypes]::Role

#Set the GroupClaimType property to the GroupSID claim type, do not run for Role claim type
$issuer.GroupClaimType = [Microsoft.IdentityModel.Claims.ClaimTypes]::GroupSid

#Update the SPTrustedIdenityTokenIssuer object to apply the change
$issuer.Update()

There is another way….

Custom claim providers can be developed to augment claims. This can be used instead of completing the steps above. The link below will get you started on that path of development.

https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-create-a-claims-provider-in-sharepoint

Migrate Users (Move-SPUser) when moving from Windows to SAML/FBA

When adding another authentication provider to a web application like ADFS(SAML) or FBA(LDAP), SharePoint sees these users and roles(groups) as entirely different users and groups than Windows counterparts. The backend Active Directory might be the same along with usernames and passwords but to SharePoint these are different users.

Different Users, Different Permissions

I’ve deployed ADFS to a web application. I have added both my ADFS and Windows accounts to the site collection. The UserInfo table shows as two different users.


One is a site collection administrator but the other one is not.

What’s the difference?

The users will experience different…

  • User Profiles
  • My Sites
  • Permissions(users will report random access denied)
  • Alerts
  • Personal Views

People picker will also show two different accounts. This will further confuse end users on which account is correct.


The Best Practice

Migrate the farm to utilize one type of authentication. Mixed authentication can be done but only for a subset of users. If you’re utilizing ADFS, I suggest checking out my previous post on how to migrate to ADFS from Windows claims.

If you’re wanting to keep Windows Authentication but need something like SAML for working away from the office, I would suggest checking out Web Application Proxy (WAP). This will not require SharePoint to migrate any of the users. The SAML authentication happens at WAP then translate the token to Kerberos for SharePoint.