Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Total Powershell newbie here, but I have managed to write a couple scripts so far that have been working.

We are using 2010 Enterprise edition, and I have been tasked with creating a script that will add a new Document Library to every site collection....No problem, got that one created. But the kicker on this was that they want the document library to be hidden from all users except for Site Collection Administrators.

I have searched for 2 days straight (Google isnt returning any blue links anymore, just purple haha) and have not been able to find the answer.

Could anybody help me out with this? What do I add to this script to make the new Libraries hidden from everyone except site collection admins?

this is what I have to create the libraries:

$sites = get-spsite -webapplication http://portal -limit all
foreach($site in $sites)
{
$spWeb = Get-SPWeb $site.url
$listTemplate = [Microsoft.sharepoint.SPListTemplateType]::DocumentLibrary
$spWeb.Lists.Add("Audits","Location for storage of Audit Files",$listTemplate)
}
share|improve this question

3 Answers 3

$spSites = Get-Spweb -Limit All
foreach ($SPsite in $spSites)
{
   $webUrl= $SPsite.url
   $listTemplate = [Microsoft.sharepoint.SPListTemplateType]::DocumentLibrary
   $spWeb.Lists.Add("Audits","Location for storage of Audit Files",$listTemplate)
   $spList="Audits"
   $spWeb = ([Microsoft.SharePoint.PowerShell.SPWebPipeBind]$webUrl).Read()
   $spList =  $spWeb.Lists[$spList]
   $spList.BreakRoleInheritance("true");
   $spweb.AllowUnsafeupdates = $true
   [Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments = $spList.RoleAssignments
  for([int] $a = $spRoleAssignments.Count-1; $a -ge 0; $a--)
         {

            $spRoleAssignments.Remove($a);

         }
    $spweb.AllowUnsafeupdates = $false
    $spWeb.Dispose()
}
share|improve this answer

Wanted to thank the folks who wrote replies for this question. I had decided to do my tasks in separate scripts, for the sake of making things easier.

I ended up using this as my final script to break the permissions on the document libraries. I have tested it out in my dev environment extensively, and all seems to be working well. The script calls out every site collection, looks for a document library called "Audit Logs" (which is created in a previous script), and then breaks the permissions from the parent folder, which in turn leaves only the Site Collection Administrators with access to the library. When regular users browse the site, the folder doesnt even show up in their view. All is right in the world.

Again, thanks for the help guys.

$site = Get-SPSite | Get-SPWeb -limit all | ForEach-Object {
$listName = "Audit Logs"
$list = $_.Lists[$listName]
$list.BreakRoleInheritance($False)
$list.Update()
}
share|improve this answer

You need to break inheritance to the list as soon as your create it. Because site collection administrators can see everything in the site collection, you shouldn't have to add there roles to the library. Try something like this:

$sites = get-spsite -webapplication http://portal -limit all
foreach($site in $sites)
{
$spWeb = Get-SPWeb $site.url
$listTemplate = [Microsoft.sharepoint.SPListTemplateType]::DocumentLibrary
$spWeb.Lists.Add("Audits","Location for storage of Audit Files",$listTemplate)
$docLib = $SpSite.Lists["Audits"];
$docLib.BreakRoleInheritance("false");
    while ($docLib.RoleAssignments.Count > 0)  
    { 
      $docLib.RoleAssignments.Remove(0); 
    }
}
share|improve this answer
    
Mike, thanks so much for getting back to me. I didnt realize it until it was too late, but I posted code that did not work. I just updated the code with the correct script that adds the libraries to each site. –  Kevin Jan 22 '13 at 19:52
    
I updated my code to match your working code. –  Mike Jan 22 '13 at 19:54
    
should the line $docLib.BreakRoleInheritance(false) have quotes in it? It turns all the text below it black if it is just in parentheses. –  Kevin Jan 22 '13 at 20:21
    
1 or 2 errors: Cannot index into a null array. At C:\Users\kmfassanella-admin\Desktop\myscripts\CreateNewDocLibraryWithBreakingPer‌​missions1.ps1:12 char:25 + $docLib = $SpSite.Lists[ <<<< "Audits Logs"]; + CategoryInfo : InvalidOperation: (Audits Logs:String) [], RuntimeException + FullyQualifiedErrorId : NullArray –  Kevin Jan 22 '13 at 20:25
    
You cannot call a method on a null-valued expression. At C:\Users\kmfassanella-admin\Desktop\myscripts\CreateNewDocLibraryWithBreakingPer‌​missions1.ps1:13 char:29 + $docLib.BreakRoleInheritance <<<< ("false") + CategoryInfo : InvalidOperation: (BreakRoleInheritance:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull –  Kevin Jan 22 '13 at 20:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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