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.

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.

SharePoint 2013/2016 – Migrate from Windows claims to ADFS

Many farms are moving from Windows Authentication(NTLM or Kerberos) to SAML. This migration and change requires a lot of planning. This guide is only to give a frame work of migrating some of the simpler farms. This guide also assumes that the ADFS server is already setup and just needs to be configured.

The difference of this guide and other guides are two things. We will be using the -UseDefaultConfiguration when created the trusted identity token issuer. This will give us an OOB claims provider instead of having your farm fending for itself with the people picker/claim provider. The next difference is the user and group migrations. We will be using Convert-SPWebapplication instead of developing a script to use Move-SPUser. More details later in the post about both of these.

Requirements:

  1. The SharePoint farm has to be at June 2014 CU (or higher) for SharePoint 2013(15.0.4623.1001).
  2. The web applications must be Windows Claims. SAML is not supported on classic.
  3. ADFS must be backed with the same Active Directory used in Windows Claims.

Steps to be completed:

  1. Configure ADFS
  2. Creating Trusted Identity Provider
  3. Migrate Users and Groups

Config ADFS

As stated above we assume the ADFS server is setup and connected to AD and ready to configure the Relying Party Trusts.

  1. In AD FS, right click Relying Party Trusts and select Add Relying Party Trust.


  2. This will bring up Add Relying Party Trust Wizard. Click Start


  3. Click Enter data about the relying party manually, Next.


  4. Type in a display name and description(optional). Click Next.


  5. We want to select AD FS 1.0 and 1.1 profile since SharePoint does not support anything but SAML 1.1.


  6. We will need to enter in the web application with /_trust. This will need to be HTTPS.


  7. We will need to add in the realm for an identifier. I entered in urn:sp13:contoso. We will be using this on the SharePoint side.


  8. I’m not using Multi-factor so just clicking Next


  9. We’re permitting everybody through. Click Next


  10. Click Next


  11. Click Close and leave the box checked. We will be mapping claims.


  12. In the Issuance Transform Rules, click Add Rule…


  13. Leave the drop down value at Send LDAP Attributes as Claims. Click Next


  14. Give the claim rule a name. Select Active Directory from the dropdown.


  15. We will only be mapping the required fields here. The first one we will map is your identifier claim. This is the claim that a user needs to have in AD to sign in. The only choices Account-Name, Email or USER-PRINCIPAL-NAME. Adjust your guide as needed. I will be using Email. The only other Claim is for security groups.


LDAP Attribute Outgoing Claim Type
User-Principal-Name UPN
E-Mail-Address E-Mail Address
SAM-Account-Name Windows account name
Token-Groups as SIDs Group SID
  1. We will need the Token-sign certificate from ADFS. Click on Certificates, then click on the token-signing certificate, then View Certificate


  2. Click on Details tab and Copy to File…


  3. DER .CER is the file format we want. Click Next


  4. Give the file a name and location. Click Next


  5. Click Finish. Copy the certificate to the SharePoint server.


Creating Trusted Identity Provider

  1. On the SharePoint server, open PowerShell ISE. We will be loading the SharePoint module using Add-PSSnapin *sharepoint*


  2. This is to set the path of the token signing certificate we got from ADFS
$adfscertPath = “c:\adfstoken.cer” 
$adfscert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($adfsCertPath)

3. We want SharePoint to trust the certificate.

New-SPTrustedRootAuthority -Name “ADFS Token Signing Cert” -Certificate $adfscert

4. The script to create the trusted provider in SharePoint.

The -Realm is what we put in as an identifier for ADFS(urn:sp13:contoso)

The -IdentifierClaimIs can only be ACCOUNT-NAME, EMAIL, or USER-PRINCIPAL-NAME.


$ap = New-SPTrustedIdentityTokenIssuer -Name “ADFS” -Description “ADFS provider” -Realm “urn:sp13:contoso” -ImportTrustCertificate $adfscert -signInURL “https://adfs.contoso.com/adfs/ls” -UseDefaultConfiguration -IdentifierClaimIs EMAIL

 We can add in more claims after the trusted provider is created. Example is below of adding a role claim.

$ap = Get-SPTrustedIdentityTokenIssuer “ADFS”
$ap.ClaimTypes.Add(“http://schemas.microsoft.com/ws/2008/06/identity/claims/role”)
$map = New-SPClaimTypeMapping “http://schemas.microsoft.com/ws/2008/06/identity/claims/role” -IncomingClaimTypeDisplayName “Role” -SameAsIncoming
$ap.AddClaimTypeInformation($map)
$ap.Update()

5. Associate the new trusted provider to your web application(s). In central admin, select Application Management, then Manage web applications. Select your web application, then Authentication Providers. For each zone, select Windows Authentication and Trust Identity provider. Both are required for the migration.


Migrate Users and Groups

The migration of users and groups are very similar to the migration scripts with a difference of the groups. The groups will change from Windows claim + SID to ADFS + SID. Other migrations are passing the unqualified name through the SAML token. The claim provider that is provisioned will not show you the SID just like Windows does either.

The PowerShell we will be running is Convert-SPWebApplication.

$wa = get-spwebapplication https://contoso
Convert-SPWebApplication -Identity $wa -From CLAIMS-WINDOWS -To CLAIMS-TRUSTED-DEFAULT -TrustedProvider $ap -RetainPermissions 

This will need to be run on each web application. After the conversion is done, you can disable the Windows Authentication.

People picker will have two results for all users that have an Email address in Active Directory. The ADFS results is the one we would want to select.


Resources

The Convert-SPWebApplication command cannot convert from Windows claims to SAML in SharePoint Server 2013 – https://support.microsoft.com/en-us/help/3042604/the-convert-spwebapplication-command-cannot-convert-from-windows-claim

Configure SAML-based claims authentication with AD FS in SharePoint 2013 – https://technet.microsoft.com/en-us/library/hh305235.aspx