3

Please help me about adding new AD users, AD groups with Permission to sharepoint site. I can do it for lists and document libararys but adding to site seems to be different. This is what i do for lists:

Function Rights ($user, $ShortListName, $permission){

$userName = $spweb.EnsureUser($User)
$spFolder = $spweb.lists[$ShortListName]
if($spweb.SiteUsers[$userName])
    {
        $assignment=new-object  
Microsoft.SharePoint.SPRoleAssignment($spweb.SiteUsers[$userName])
        $assignment.RoleDefinitionBindings.Add($spweb.RoleDefinitions[$permission])
        $spfolder.BreakRoleInheritance($false, $false)
        $spfolder.RoleAssignments.add($assignment);
        Write-Host "Permission provided for group ", $userName
    $spfolder.update()
    }
 else{
      Write-Host "group ""$userName"" was not found in this web!"
    }
}

If i simply change the spfolder spweb, it won't work

2 Answers 2

2

If you want to add permission to the SPWeb, try this:

$account = $web.EnsureUser($userName)
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permission]
$assignment.RoleDefinitionBindings.Add($role)
$web.RoleAssignments.Add($assignment)
$web.dispose()
0

It's not clear to me what you are asking for. The title of your question is regarding adding an "AD user to SharePoint site" but your script is setting specific permissions on a folder and granting a user/group those permissions. Are you just trying to add a user or AD group to a SharePoint group like the owners/members/visitors groups? If that is the case, use this:

Set-SPUser -Identity 'domain\UserName' -Web 'http://sharepointdomain.com/sites/web' -Group 'Site Members'

1
  • 1
    No. I wanted to add AD users and AD groups with permission to sp site. My code is for adding to lists.
    – Lokids
    Aug 27, 2013 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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