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