Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell iterate through SharePoint subsites and add group permission based on list column.

$
0
0

I have a web app that houses all of our project sites. I want to be able to add permissions for a group where the "Project Department" column of the "Project Health" list equals a user defined department (Read-Host). Due to the nature of the web app this script is a necessity, any help is appreciated.

$deptName = Read-Host -Prompt "Department Name:"
$groupName2 = Read-Host -Prompt "Group Name:"

function AddGroupToSite ($web, $groupName, $permLevel)
{
    $account = $web.SiteGroups[$groupName]
    $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
    $role = $web.RoleDefinitions[$permLevel]
    $assignment.RoleDefinitionBindings.Add($role);
    $web.RoleAssignments.Add($assignment)
}
function Add-GroupPerm() {
    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    foreach ($spService in $farm.Services) {
        if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
            continue;
        }

        foreach ($webApp in $spService.WebApplications) {
            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }
            foreach ($site in $webApp.Sites) {
                foreach ($web in $site.AllWebs) {
                    foreach ($list in $web.Lists | where {$list.title -eq "Project Health"}){
                        foreach ($field in $list.fields | where {$field["Project Department"] -eq $deptName}){
                            AddGroupToSite -web $web -groupName $groupName2 -permLevel "Contribute"
                       }
                    }
                    $web.Dispose();
                }
                $site.Dispose()
            }
        }
    }
}


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>