So I have a boat load of pivot tables that I upload everyday to folders like: Pivot0001 Pivot0002 Pivot0003
and so on.
I also have user groups called Pivot0001 and so on with users that need to access that folder in it. What I now need to do is set the permissions on each folder (I have 400 or so of them). I know I need to do a loop and set permissions. What I dont know how to do is get a list of all the folders and then set permissions to that folder.
EDIT I forgot to say this is for SharePoint...sorry about that
Here is the final code that worked (not really clean but it works)
[Void][System.Diagnostics.Stopwatch] $sw;
$sw = New-Object System.Diagnostics.StopWatch;
$sw.Stop();
$sw.Start();
clear
$path = "\\path\to\webdav\"
$dirs = Get-ChildItem $path -Recurse | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Directory }
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://sharepoint");
$OpenWeb = $SpSite.OpenWeb("/Downloads");
[int]$i = 0;
foreach ($dir in $dirs) {
$i++
Write-Host "Setting $dir to $dir" -F Green;
$path = "http://sharepoint/Downloads/" + $dir;
$TheNewGroup = $OpenWeb.GetFolder($path);
[Microsoft.SharePoint.SPFolder]$folder = $OpenWeb.GetFolder($path);
[Microsoft.SharePoint.SPGroupCollection]$spc = $OpenWeb.SiteGroups;
[Microsoft.SharePoint.SPGroup]$group = $spc[$dir];
[Microsoft.SharePoint.SProleAssignment]`
$roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment([Microsoft.SharePoint.SPPrincipal]$group);
$OpenWeb.GetFolder($path).Item.BreakRoleInheritance("true");
$roleAssignment.RoleDefinitionBindings.Add($OpenWeb.RoleDefinitions["Read"]);
$OpenWeb.GetFolder($path).Item.RoleAssignments.Add($roleAssignment);
}
Write-Host "found $i Folders" -F Green
$SPSite.Dispose();
$sw.Stop();
$howlong = $sw.Elapsed.ToString();
write-host "Took: " $howlong -f Green;