0

I have a powershell script I developed in order to create document libraries across multiple sites under a single site collection. I wish to now add the appropriate Site Administrators with appropriate permissions to the document library, and am having a little difficulty in finding the appropriate group name in order to add it. This is what I have thus far. It will create the document library and I managed to display any Sharepoint group that has a string match to what I am looking for, but this seems too prone to error. I also do not know how to actually add the group once I have properly grabbed it.

$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$listURL = "mydoclib"
$listTitle = "Document Library"
$listDescription = "Document Library"

$spWeb = Get-SPWeb "http://site.com/aboutme"
  foreach($subWeb in $spWeb)
  {
  if($subWeb -ne $null)
  {
    Write-Host $("Site: "+$subWeb.URL)

    $SiteRoles = $subWeb.RoleAssignments
    foreach($role in $SiteRoles)
    {
      write-Host $role.Member.Name -foregroundcolor "yellow"
      if ($role.Member.Name -like "*Admin*")
      {
        write-host $role.Member.Name -foregroundcolor "cyan"
      }
    }
    ####CREATE
    $subWeb.Lists.Add($ListUrl,$ListDescription,$listTemplate)
    $list = $subWeb.Lists[$listURL]
    $list.Title = $ListTitle
    $list.OnQuickLaunch = $True
    $list.Update()
    $subWeb.Dispose()
  }
  else
  {
    Write-Output $($subWeb + "does not exist")
  }
}

How would I go about properly grabbing any SharePoint group with the "TeamSite Administrator" permission level (a custom permission level) and apply it to the newly created Document Library?

1

You have to break the permission of the list, create a new role and assing it the to group.
Then assign the role to the list.

$list.BreakRoleInheritance($True)
$group = $web.SiteGroups[$GroupName]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.RoleDefinitions[$PermissionLevel];
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
$list.RoleAssignments.Add($roleAssignment)
$list.Update();

Have a look here Add group to Library permissions using powershell

  • Thank you for your reply - this works wonderfully. The solution provided assumes I have a group name already to work with. Would you happen to know how I can grab a group with specific permission levels on the specific site I am working with? Since I am iterating through sites, I do not have a method to grab specific group names prior to applying them. For example, I would like to grab the "About Me Administrators" group (if I am currently iterating that site) which has the "TeamSite Administrators" permission level and only apply that group to the newly created document library – Kolten Sep 11 '14 at 17:55
  • nevermind i found a way :) I mistakenly assumed I needed to ADD the group, but it gets created with inherited permissions anyways, so I actually needed to REMOVE the groups I'm NOT interested in. Using code found here I finalized my solution. Thanks! sharepointthinking.wordpress.com/2012/10/01/… – Kolten Sep 11 '14 at 21:37
0

You can get site collection admins from powershell as:

foreach($siteAdmin in $site.RootWeb.SiteAdministrators)

http://gallery.technet.microsoft.com/office/PowerShell-Script-for-aeba93d8

http://sharepointhangout.com/powershell-script-to-display-site-collection-administratorsprimary-secondary/

  • I am actually looking to grab site administrators for a specific site with a specific permission level - not the site collection admins. – Kolten Sep 11 '14 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.