Objective

This purpose of this document is to mention the steps to cleanup site collection administrator from site collection administrator group using PowerShell script. The reusable script for this job has also attached with this document.

This script offers

Business Case

S. No.Business Case
1For several reasons (Some application troubleshooting, Deployment,Migration, Testing etc.) SharePoint Support team requires an access toPROD site collections. Most of the time we forgot to revoke access ofthese users from PROD sites which may result into access matrixviolation for Customer. However, there are some test site collectionwhich we use for testing purpose for offshore development team, we wantteam to have an access to only those test Site collection. This scriptwill allow administrator to do this job just by configuring the XMLfile.

Targeted Audience

Offerings

Technical Details

Below are the technical details for this PowerShell script,

  1. Pre-requisites

    • Login to server with Farm administrator account and copy the folder and paste the same to the location where you want to keep it.

    • Open the folder and Configure XML file as per your requirement.
      1. <?xml version="1.0" encoding="utf-8" ?>
      2. <Configuration Environment="DEV" Version="1.0.0.0">
      3. <GlobalWebApplications>
      4. <GlobalWebApplication url="http://myWebApplication" SitesToExclude="False">
      5. <SiteCollections>
      6. <SiteCollection relativeURL="sites/TeamSite"></SiteCollection>
      7. <SiteCollection relativeURL="Sites/ProjectSite"></SiteCollection>
      8. </SiteCollections>
      9. <UsersToCleanup>
      10. <User UserLogin="i:0#.w|Domain\LoginId"></User>
      11. <User UserLogin="i:0#.w|Domain\LoginId"></User>
      12. </UsersToCleanup>
      13. </GlobalWebApplication>
      14. </GlobalWebApplications>
      15. </Configuration>

    As shown in above image;

    • Enter Web Application URL.
    • Define SitesToExculde Tag to True/False

      • True

        If we configure this attribute value to True; which means we want to exclude the below configured site collections in this process. So that the cleanup activity will not work on this Site Collections.

      • False

        If we configure this attribute value to False; which means we want to include the below configured site collections in this process. So that the cleanup activity will work on all other Site Collections but not on those which are configured in XML.

    • Configure a set of Site collection relative URL which you want to exclude form this cleanup activity.
    • Configure a list of users which you want to cleanup from Site Collection Administrator group.

      Note:
      Name the file as Configuration.xml only.

  2. Execution

    Prerequisite:

    Login to SharePoint Server as Farm Administrator and copy the required files (PowerShell script and configuration XML).Configure the XML file as per your requirement.

    Run:

    • Run the PowerShell Script as “Run as Administrator“.

    • Browse the folder path where you have kept this PowerShell script file and execute a command as shown in below image.

      command

PowerShell Script

  1. ##########################################################################################################################
  2. ######## V 1.0
  3. ######## PowerShell Script to Cleanup not required admins form Site Collection other than those defined in Config file
  4. ##########################################################################################################################
  5. #check to see if the PowerShell Snapin is added
  6. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
  7. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  8. }
  9. ## SharePoint DLL
  10. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
  11. $global:currentPhysicalPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
  12. [xml]$xmlinput = (Get-Content "$global:currentPhysicalPath\Configuration.xml")
  13. function Global_CleanUpAdminsFromSiteCollections([xml]$xmlinput)
  14. {
  15. foreach($configWebApp in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication)
  16. {
  17. $webApp = Get-SPWebApplication $configWebApp.url -ErrorAction silentlycontinue
  18. $Choice = $configWebApp.SitesToExclude
  19. if($webApp -eq $null)
  20. {
  21. Write-host Web Application at url : $configWebApp.url does not Exists.. -foregroundcolor Red
  22. }
  23. else
  24. {
  25. $AllSiteCollections = $webApp | Get-SPSite -Limit ALL
  26. if($choice.ToString().ToLower() -eq "true")
  27. {
  28. $SiteCollectionsToExclude = $AllSiteCollections
  29. foreach($siteColl in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.SiteCollections.SiteCollection)
  30. {
  31. $SiteCollectionsToExclude = $SiteCollectionsToExclude | Where-Object {$_.url -ne $($webApp.url + $siteColl.relativeUrl) }
  32. }
  33. foreach($UserID in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.UsersToCleanup.User)
  34. {
  35. foreach($SiteCollection in $SiteCollectionsToExclude)
  36. {
  37. # Remove Site Collection Admin function call
  38. RemoveSiteCollAdmin $UserID.UserLogin $SiteCollection.url
  39. }
  40. }
  41. }
  42. else
  43. {
  44. $SiteCollectionsToInclude = @()
  45. if($choice.ToString().ToLower() -eq "false")
  46. {
  47. foreach($siteColl in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.SiteCollections.SiteCollection)
  48. {
  49. foreach($site in $AllSiteCollections)
  50. {
  51. if($site.url -eq $($webApp.url + $siteColl.relativeUrl))
  52. {
  53. $SiteCollectionsToInclude = $SiteCollectionsToInclude + $($webApp.url + $siteColl.relativeUrl);
  54. }
  55. }
  56. }
  57. foreach($UserID in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.UsersToCleanup.User)
  58. {
  59. foreach($SiteCollection in $SiteCollectionsToInclude)
  60. {
  61. # Remove Site Collection Admin function call
  62. RemoveSiteCollAdmin $UserID.UserLogin $SiteCollection
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. #EndRegion
  71. function RemoveSiteCollAdmin([string]$AdminID, [string]$SiteCollectionURL)
  72. {
  73. #Get the site collection object
  74. $site = Get-SPSite $SiteCollectionURL
  75. if ($site -ne $null)
  76. {
  77. Write-host -ForegroundColor Magenta "Site" $siteObject.Url "exists!"
  78. #Get the Admin to remove from Site collection Administrator Group
  79. $Account = $site.RootWeb.SiteAdministrators | Where-Object {$_.UserLogin -eq $AdminID}
  80. #if User account found
  81. if($Account)
  82. {
  83. $Account.IsSiteAdmin = $false
  84. $Account.Update()
  85. Write-Host -ForegroundColor Green "$($AdminID) has been removed from Site Collection $($SiteCollectionURL) Administrator Group!"
  86. }
  87. else
  88. {
  89. Write-Host -ForegroundColor DarkMagenta "$($AdminID) Not found in Site Collection Administrator Group!"
  90. }
  91. }
  92. else
  93. {
  94. Write-Host -ForeGroundColor Red "- Make sure you have typed the URl Correctly.Site at this" $SiteCollectionURL "does not exist."
  95. }
  96. }
  97. #Region function call
  98. #start-transcript -path .\CleanUpAdminsFromSiteCollections_Output.txt
  99. Global_CleanUpAdminsFromSiteCollections $xmlinput
  100. #stop-transcript
  101. #EndRegion