Here is the scenario. I only need to run this once as business team just decided their contents are confidential. User Group is "Marketing", will use OOTB "Contribute" and all of marketing items are using "2011 Marketing" content type. The library name is "2011Sales". I need to take out current permissions (also remove inheritance) and only add "Marketing" group with "Contribute" right.
Updated Working Code:
$webUrl = "http://inside.national.com/Sales"
$web = Get-SPWeb $webUrl
$list = $web.Lists["2011Sales"]
$ct = "2011 Marketing"
$spgroup = "Custom SP Group"
$rd="Contribute"
foreach ($item in $list.items) {
If ($item.ContentType.Name -eq $ct)
{
$item.ResetRoleInheritance() # Not sure if this line is needed
$item.BreakRoleInheritance($false) # I tried $true but did not work, so leave it at $false
$item.SystemUpdate() # update without changing the modification date
$group = $web.AllUsers[$spgroup]
$roledef = $web.RoleDefinitions[$rd]
$roleass = New-Object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$spgroup])
$roleass.RoleDefinitionBindings.Add($roledef)
$item.RoleAssignments.Add($roleass)
$item.SystemUpdate() # update without changing the modified date
Write-Host $item.Name " permission applied"
}
}
$web.dispose()