Copy Users and Groups using SharePoint 2010 Power-shell script


In this case i am maintaining values as ";" seperated values in this script. If we want to maintain data please use list items. It is best approach for workflows also.
http://SP2010:8080/Powershellaction.asmx by using this SharePoint 2010 service we able to find the "RunScript" method for power-shell script.

save this script as "TestScript.ps1" and run this file in SharePoint power-shell cmd prompt. 

>&"c:\TestScript.ps1"



TestScript.ps1:

web = Get-SPWeb "http://SP2010:8080/sites/12345Test2"
for ($i = $web.RoleAssignments.Count -1; $i -ge 0; $i--)
    $web.RoleAssignments.Remove($i) 
}  
if($web.SiteGroups["Engineers"] -eq $null) 
{
          $web.SiteGroups.Add("Engineers", $web.Site.Owner, $web.Site.Owner, "Use this group to grant people Contribute permissions to the $web site")
}
$EngineersGroup = $web.SiteGroups["Engineers"]

$EngineersGroupAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($EngineersGroup) 

$EngineersGroupRoleDefinition = $web.Site.RootWeb.RoleDefinitions["Contribute"] 

$EngineersGroupAssignment.RoleDefinitionBindings.Add($EngineersGroupRoleDefinition) 

$web.RoleAssignments.Add($EngineersGroupAssignment) 

$EngineersGroup.OnlyAllowMembersViewMembership = $false 
$EngineersGroup.Update()

$EngineersColl = "prasad, narasimha; siva, narayana" 
$EngineersArray = $EngineersColl.Split(";")

foreach($Engineer in $EngineersArray) 
{  
    $user1 = $web.Site.RootWeb.EnsureUser("$Engineer")  
    $EngineersGroup.AddUser($user1) 
}

$EngineersGroup.Update()

$web.Update() 
$web.Dispose()


this script will create two users under the Engineers group.

Comments

  1. A new specialized inside the much wider coaching area, clash coaching allows men and women, family members and work environment teams build far better talk, far better selections, and much healthier operate and residence romantic relationships Männergruppen

    ReplyDelete

Post a Comment

Popular posts from this blog

Sending email using Powershell script

Convert List Collection to DataTable using C#

Difference Between Site Template and Site Definition