Asked by:
Remove access to 200 site collections

Question
-
Hi All,
Looking for expert opinions on this script I have written. The intention is to deny access to sites identified as not being used, in preparation for a migration from SharePoint 2010 to SharePoint Online. Have I missed anything?
#Custom script to remove all user access to a SharePoint site
#Create Data table
$SPSitePermissions = New-Object System.Data.DataTable “SPSitePermissions”
$SPSitePermissions.Columns.Add("Row_ID",[String]) | Out-Null
$SPSitePermissions.PrimaryKey = $SPSitePermissions.Columns["Row_ID"]
$SPSitePermissions.Columns.Add("Site_ID",[String]) | Out-Null
$SPSitePermissions.Columns.Add("Web_ServerRelativeURL",[String]) | Out-Null
$SPSitePermissions.Columns.Add("Web_GroupName",[String]) | Out-Null
$SPSitePermissions.Columns.Add("Web_BoundDefinition",[String]) | Out-Null
#Create output file
$filename = (get-date -format "dd_MMM_yyyy_HH_mm_ss") + "_SPWebPermissions"
$columns = "Row ID", "Site ID", "Server Relative URL","Group Name","Bound Definition"
$columns -join ";" >> c:\SPAudit\$filename.csv
$rowCount = 0
$siteList = Import-csv -Path C:\SPAudit\SiteList.csv
$ErrorActionPreference = 'SilentlyContinue'
Add-PSSnapin Microsoft.SharePoint.PowerShell >$null
$ErrorActionPreference = 'Continue'
#New SharePoint assignment store
Start-SPAssignment –Global
foreach($address in $siteList){
$target = "http://mysite" + $address.Address
$spsite = get-spsite $target
$spweb = get-spweb $target
#Create the No Access role definition
$spRoleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition;
$spRoleDefinition.Name = "No Access";
$spRoleDefinition.Description = "No Access";
$spRoleDefinition.BasePermissions = "EmptyMask";
$spweb.RoleDefinitions.Add($spRoleDefinition);
$spweb.update()
#Find the No Access role definition and store for later use
foreach($roleDefinition in $spweb.RoleDefinitions){
if($roleDefinition.Name -eq "No Access"){
$noAccessRole = $roleDefinition
$noAccessRole.Name
}
}
$spweb.dispose()
foreach($spweb in $spsite.allwebs){
#Check if the subweb has unique permissions, if it does, remove all permissions and replace with an empty mask.
if($spweb.hasuniqueroleassignments -eq "True"){
#Process each SharePoint permission group
foreach($roleAssignment in $spweb.RoleAssignments){
#Skip the system account group if present
if($roleAssignment.Member.Name -eq "System Account"){
}else{
#Add the No Access role to the groups defined permissions and commit
write-host -foregroundcolor green "Adding No Access permission to Group Name: "$roleAssignment.Member.Name "..."
$roleAssignment.RoleDefinitionBindings.Add($noAccessRole);
$roleAssignment.Update();
}
}
#Process each SharePoint permission group again
foreach($roleAssignment in $spweb.RoleAssignments){
#Skip the system account group if present
if($roleAssignment.Member.Name -eq "System Account"){
}else{
#Process each role assigned to this group and remove it if it is not either 'Limited Access' or 'No Access'
foreach($roleDefinition in $roleAssignment.RoleDefinitionBindings){
if($roleDefinition.Name -eq "Limited Access"){
write-host -foregroundcolor blue "Skipping Limited access permission assignment"
}elseif($roleDefinition.Name -eq "No Access"){
}else{
write-host -foregroundcolor red "Removing Permission Name: "$roleDefinition.Name "From Group: "$roleAssignment.Member.Name
$rowCount++
#Log current permission settings
$Row = $SPSitePermissions.NewRow()
$Row.Row_ID = $rowCount
$Row.Site_ID = $spsite.ID.ToString()
$Row.Web_ServerRelativeURL = $spweb.serverrelativeurl
$Row.Web_GroupName = $roleAssignment.Member.Name
$Row.Web_BoundDefinition = $roleDefinition.Name
$SPSitePermissions.Rows.Add($Row)
$RowNo = $SPSitePermissions.Rows.Find($rowCount)
$output = $RowNo.Row_ID, $RowNo.Site_ID, $RowNo.Web_ServerRelativeURL, $RowNo.Web_GroupName, $RowNo.Web_BoundDefinition
#output
$output -join ";" >> c:\SPAudit\$filename.csv
#Remove the role definition
$roleAssignment.RoleDefinitionBindings.Remove($roleDefinition);
$roleAssignment.Update();
}
}
}
}
}
$spweb.Dispose();
}
$spsite.Dispose();
}
Stop-SPAssignment –Global
Tuesday, July 21, 2020 5:54 PM
All replies
-
I am a bit concerned about this line:
$spweb.hasuniqueroleassignments -eq "True"
should it actually be:
$spweb.hasuniqueroleassignments -eq $TrueTuesday, July 21, 2020 6:02 PM -
Also, I have skipped removing the limited access permissions, because I am not really sure what they are there for... Are they required system permissions?
Tuesday, July 21, 2020 6:05 PM -
Hi, Alister,
I am not sure why you plan to remove the permission of all users which will cause you great time to rebuild the permission system. Do you know in SharePoint 2010 you can set the site to read-only mode? That shall be enough for you to do the migration to SharePoint Online
Set-SPSite -Identity "<siteURl>" -LockState "ReadOnly"
Reference: https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/set-spsite?view=sharepoint-ps
This “SharePoint 2010 - General Discussions and Questions” Forum will be migrating to a new home on Microsoft Q&A, please refer to this sticky post for more details.
Best Regards
Jerry
"SharePoint" forums will be migrating to a new home on Microsoft Q&A !
We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !- Edited by Jerry Xu4869Microsoft contingent staff Wednesday, July 22, 2020 9:37 AM
- Proposed as answer by Michael Han6 Thursday, July 23, 2020 7:50 AM
Wednesday, July 22, 2020 2:17 AM -
Hi, Alister,
Have you tried the cmdlet? Is there any further question on your task?
Best Regards
Jerry
"SharePoint" forums will be migrating to a new home on Microsoft Q&A !
We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !
Tuesday, July 28, 2020 6:20 AM -
Hi,
This forum will be locked down, if you still have issues about this question, you can continue to post in here before 8/10/2020. Or, you can create a new question on Microsoft Q&A, we will continue to support you on the new home.
Best Regards
Jerry
"SharePoint" forums will be migrating to a new home on Microsoft Q&A !
We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !
Monday, August 3, 2020 7:26 AM -
Hi, Alister,
Is there any update about your task?
Best Regards
Jerry
"SharePoint" forums will be migrating to a new home on Microsoft Q&A !
We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !
Friday, August 7, 2020 1:20 AM