在powershell脚本中需要帮助。

时间:2012-10-04 15:19:27

标签: powershell sharepoint-2010

我有一个名为Test的sharepoint 2010 toplevel网站。在测试中,有三个子站点,分别名称为test1,test2,test3。在顶级站点(测试)中,有三个自定义组名称:test1group,test2group和test3group。

使用powershell脚本我想将组和权限导出到各自的子网站。例如,如果我们要在test1子网站中导出组和权限 那么只有test1group应该继承而不是test2group和test3group ..而且在执行test2子网站的组导出时类似,那么只应该继承test2group ...而不是test1group和test2group ....等等(对于test3子网站)..

使用以下脚本我试图执行此操作:

function AddGroupToSite($url, $groupName, $permLevel)
{
$web = Get-SPWeb $url
#Break permissions inheritance and copy the groups from parent site into this site     (recommended)

 $web.BreakRoleInheritance($true)
 $web.Update()
#Creating a new group:
$web.SiteGroups.Add($groupName, $web.Site.Owner, $web.Site.Owner, "New Group from   powershell 4")
 $newGroup = $web.SiteGroups[$groupName]

#Create role assignment:
$newGroupAssign = New-Object Microsoft.SharePoint.SPRoleAssignment($newGroup)

#Assign a specific role
#The possible enumeration values are: None, Guest, Reader, Contributor, WebDesigner, Administrator
$newGroupAssign.RoleDefinitionBindings.Add($web.RoleDefinitions.GetByType($permLevel))
$web.RoleAssignments.Add($newGroupAssign)

#Update web
$web.Update()
$web.Dispose()
}

但是每次它都是来自顶级站点的所有组(这是默认行为)....我们可以自定义powershell脚本,以便我们可以实现上述功能。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

当您中断继承时,它会创建最初继承的内容的副本。

我没有方便的Sharepoint命令,但您应该可以执行类似

的操作
function Remove-SPGroup ($url, $groupName)
{
  $web = Get-SPWeb $url
  $web.SiteGroups.Remove($GroupName)
  $web.Update()
  $web.dispose()
}

删除最初继承的组。

因此,您可以将其添加到现有脚本中,并使用类似

的内容
function AddGroupToSite($url, $groupName, $permLevel)
{
$web = Get-SPWeb $url
#Break permissions inheritance and copy the groups from parent site into this site     (recommended)

 $web.BreakRoleInheritance($true)
 $web.Update()
#Creating a new group:
$web.SiteGroups.Add($groupName, $web.Site.Owner, $web.Site.Owner, "New Group from   powershell 4")
 $newGroup = $web.SiteGroups[$groupName]

 foreach ($ExistingGroup in $web.SiteGroups)
 {
   if ($ExistingGroup.name -notlike $groupname)
   {
     $web.SiteGroups.Remove($ExistingGroup.name)
    }
  }

#Create role assignment:
$newGroupAssign = New-Object Microsoft.SharePoint.SPRoleAssignment($newGroup)

#Assign a specific role
#The possible enumeration values are: None, Guest, Reader, Contributor, WebDesigner, Administrator
$newGroupAssign.RoleDefinitionBindings.Add($web.RoleDefinitions.GetByType($permLevel))
$web.RoleAssignments.Add($newGroupAssign)

#Update web
$web.Update()
$web.Dispose()
}