如果SPGroups不存在使用Power Shell脚本,则在所有子站点中创建多个SPGroup

时间:2014-06-26 19:50:52

标签: sharepoint powershell-v2.0 spgroup

我需要在SubSites中创建2个SPGroup,并将多个AD组成员添加到该组中。

例如,

  1. 我的网站
  2. 下有子网站x,y,z
  3. 我有A组,B组
  4. A组成员为Member1,Member2,Member3
  5. B组成员包括Member1,Member2,Member3,Member4,Member5
  6. 我需要在子站点中创建组,如果它们不存在。
  7. 请让我知道更好的方法。

    我正在尝试使用Power shell。

    $SPWeb = $Web.Read()
    if ($SPWeb.SiteGroups[$GroupName] -ne $null){
       throw "Group $GroupName already exists!"   
    }
    if ($SPWeb.Site.WebApplication.UseClaimsAuthentication){
       $op = New-SPClaimsPrincipal $OwnerName -IdentityType WindowsSamAccountName
       $mp = New-SPClaimsPrincipal $MemberName -IdentityType WindowsSamAccountName
       $owner = $SPWeb | Get-SPUser $op
       $member = $SPWeb | Get-SPUser $mp
    }
    else {
       $owner = $SPWeb | Get-SPUser $OwnerName
       $member = $SPWeb | Get-SPUser $MemberName
    }
    $SPWeb.SiteGroups.Add($GroupName, $owner, $member, $Description)
    $SPGroup = $SPWeb.SiteGroups[$GroupName]
    $SPWeb.RoleAssignments.Add($SPGroup)
    $SPWeb.Dispose()
    

1 个答案:

答案 0 :(得分:0)

#Create an XML with all the details for the new Groups and menbers for the groups

<?xml version="1.0"?>
<Groups>
    <Group name="GroupA" description="Test GroupA" PermissionLevel="Contribute">
        <Users>
            <User>Domain\Member1</User>
            <User>Domain\Member2</User>
            <User>Domain\Member3</User>
        </Users>
     </Group>
<Group name="GroupB" description="Test GroupB" PermissionLevel="Read">
    <Users>
        <User>Domain\Member1</User>
        <User>Domain\Member2</User>
        <User>Domain\Member3</User>
        <User>Domain\Member4</User>
        <User>Domain\Member5</User>
    </Users>
</Group>

#Now create a script file with the below script

#Get Site and Web objects
$site = get-spsite https://portal/site/Test/ 

#Get all the sub sites under the web 
foreach($web in $site.allwebs) 
{ 
#Get XML file containing groups and associated users
$groupsXML = [xml] (Get-Content ("C:\Temp\Groups.XML"))

#Walk through each group node defined in the XML file
$groupsXML.Groups.Group | ForEach-Object {

    [string]$permissionLevel = $_.PermissionLevel

    #Check to see if SharePoint group already exists in the site collection
    if ($web.SiteGroups[$_.name] -eq $null)
    {
        #If the SharePoint group doesn't exist already - create it from the name and description values at the node
        $web.SiteGroups.Add($_.name, $web.CurrentUser, $null, $_.description)
        $newGroup = $web.SiteGroups[$_.name]
    }

    #Get SharePoint group from the site collection
    $group = $web.SiteGroups[$_.name]

    #Add the users defined in the XML to the SharePoint group
    $_.Users.User | ForEach-Object {
        $group.AddUser($_, "", "", "")      
    }

    $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)  
    $roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]  
    $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)

    $web.RoleAssignments.Add($roleAssignment)

    Write-Host "Group Name: "$group" Created at site: " $web.url
 }

 #Dispose of Web and Site objects
 $web.Dispose()
 $site.Dispose()   
 }