Replace a SharePoint group with another

744 Views Asked by At

I have a SharePoint sites with a numbers of subsite's. On some of the sites and subsites are there an AD group on called "a-team".

The group "a-team" must no longer have access. A new group called "c-team" has been created. My problem is to replace "a-team" with "c-team" on all Sites/subsites, where "a-team" is. Sites/subsites where there is no "a-team" shut not get "c-team" added.

Right now the code add "c-team" to all sites/subsites witch is wrong. It shut only add the group if the group "a-team" is on the site/subsite. Subsites with out "a-team" shut not get "c-team" added.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = "http://MyTest.demosite.net/sites/EAL"
$permissionLevel = "Design"
$domainGroup = "c:0+.w|s-1-5-21-3475484702-42583850-3077926241-3108" # a-team
$domainGroupAdd = "c:0+.w|s-1-5-21-3475484702-42583850-3077926241-3110" # c-team
$spSite = new-object Microsoft.SharePoint.SPSite($siteUrl)
$firstTime = "true";
$myRole;

foreach ($web in $spSite.AllWebs){
## Finding role ##
$roles = $Web.RoleDefinitions;                
$roleAssignments = $web.RoleAssignments;
if ($firstTime -eq "true")
{
    for($k = 0; $k -lt $roles.Count; $k++){
        Write-Output $web.Url
        $role = $roles[$k]         
        if( $role.Name -eq $permissionLevel)
        {                
            $myRole = $role; # Role fundet                             
        }
    }
    $firstTime = "false";
}    

 ## Adding group "c-team" to site ##
if($roleAssignment.Member.SystemUserKey -eq  $domainGroup){
    Write-Host -ForegroundColor White -BackgroundColor Green "Found the right type " $role.Name;
    $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($domainGroupAdd, "", "", "");
    $assignment.RoleDefinitionBindings.Add($myRole);
    $roleassignments = $web.RoleAssignments;
    $roleassignments.Add($assignment);
    Write-Output $web.Url;
}

$web.Dispose();
}
$spSite.Dispose();

Example

Before:

http://MyTest.demosite.net/sites/EAL" - Site permissions: "a-team", admin
http://MyTest.demosite.net/sites/EAL/Test1" - Site permissions: "a-team", admin
http://MyTest.demosite.net/sites/EAL/Test2" - Site permissions: admin
http://MyTest.demosite.net/sites/EAL/Test3" - Site permissions: "a-team", admin
http://MyTest.demosite.net/sites/EAL/Test4" - Site permissions: admin

After:
http://MyTest.demosite.net/sites/EAL" - Site permissions: "c-team", admin
http://MyTest.demosite.net/sites/EAL/Test1" - Site permissions: "c-team", admin
http://MyTest.demosite.net/sites/EAL/Test2" - Site permissions: admin
http://MyTest.demosite.net/sites/EAL/Test3" - Site permissions: "c-team", admin
http://MyTest.demosite.net/sites/EAL/Test4" - Site permissions: admin
1

There are 1 best solutions below

0
On

I found the solution. Replace group in sharepoint:

First add the new one, then remove the old one.

$siteUrl = "http://MyDemosite.demosite.net/sites/EAL" 
$permissionLevel = "Design" 
$oldGroupName    = "eal\a-team" 
$newGroup = "c:0+.w|s-1-5-21-3475484702-42583850-3077926241-3109" 

$spSite = new-object Microsoft.SharePoint.SPSite($siteUrl) 

$firstTime = "true";
$myRole;

foreach ($web in $spSite.allwebs)
{    
    $roles = $web.RoleDefinitions; 

    if ($firstTime -eq "true")
    {
        for($k = 0; $k -lt $roles.Count; $k++)
        {
            Write-Output $web.Url
            $role = $roles[$k]         
            if( $role.Name -eq $permissionLevel)
            {                
                $myRole = $role;                              
            }
        }
        $firstTime = "false";
    }   

    foreach ($name in $web.RoleAssignments.member.displayname) 
    {
        if($name -eq $oldGroupName)
        { 
            Write-Host -ForegroundColor White -BackgroundColor Green "Found the right type " $role.Name
            $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($newGroup, "", "", "");
            $assignment.RoleDefinitionBindings.Add($myRole);
            $roleassignments = $web.RoleAssignments;
            $roleassignments.Add($assignment);
            Write-Output $web.Url; 
        }
    }
    $web.Dispose();
}
$spSite.Dispose();  

And for removing group/user on all webs:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = "http://mydemosite.demosite.net/sites/eal" 
$RemoveGroup = "eal\b-team"
$spSite = new-object Microsoft.SharePoint.SPSite($siteUrl) 

foreach ($web in $spSite.allwebs)
{    
    ## Remove user/group
    foreach ($user in $web.SiteUsers) 
    {
        if ($user.DisplayName -eq $RemoveGroup)
        {
            $web.siteusers.Remove($user) 
            Write-Host -ForegroundColor White -BackgroundColor Green "Found the right user "
        }
    }
    $web.Dispose();
}
$spSite.Dispose();