开发者

User powershell to get a list of all folders in /downloads/pivots

开发者 https://www.devze.com 2022-12-08 09:05 出处:网络
So I have a boat load of pivot tables that I upload everyday to folders like: Pivot0001 Pivot0002 Pivot0003

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;
    $p开发者_如何转开发ath = "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;


Something like this should work:

$domain = "YOURDOMAIN"
$path = "C:\Your\Folder\Path"

$dirs = Get-ChildItem $path | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Directory }
foreach ($dir in $dirs)
{
    $acl = Get-Acl $dir.FullName
    $user = $domain + "\" + $dir.Name
    $permission = $user, "FullControl", "Allow"
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($rule)
    $acl | Set-Acl $dir.FullName
}

The above works for a normal local file system path, but SharePoint has a different folder security model. I found a blog post by Robert Gruen that explains how to programmatically set permissions. He gives this C# code sample:

// get a reference to the folder (this assumes path points to a valid folder)
SPFolder folder = SharePointConfiguration.Site.GetFolder(path);

// get a reference to the Sharepoint group collection
SPGroupCollection spc = SharePointConfiguration.Site.SiteGroups;

// get a reference to the group who’s permissions you want to modify for the folder above
SPGroup group = spc[groupName];

// create a role assignment from the group reference
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);

// break role inheritance for folders/files because they will be having permissions separate from their parent file/folder
folder.Item.BreakRoleInheritance(true);

// update the role assignments for the group by adding the permissionSet "TestPermissionLevel" which is a custom
// permissionset I created manually... you can easily use any of the built-in permission sets
roleAssignment.RoleDefinitionBindings.Add(SharePointConfiguration.Site.RoleDefinitions["Test Permission Level"]);

// apply the new roleassignment to the folder.  You can do this at the listitem level if desired (i.e. this could be SPfile.Item.... instead of SPFolder.Item)
folder.Item.RoleAssignments.Add(roleAssignment);

I'm sure with a bit of translation, this could be adapted to PowerShell.


Getting a list of folders isn't exactly straightforward, but easily understandable:

Get-ChildItem \downloads\pivots | Where-Object { $_.PSIsContainer }

You can then pipe that further into the ForEach-Object cmdlet where you can set the permissions:

Get-ChildItem \downloads\pivots |
Where-Object { $_.PSIsContainer } |
ForEach-Object {
    $_.SetAccessControl( ... )
}


OK, figured out what I was missing, here is my code:

The key is to provide the complete URL path to the Folder to check for.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -ne $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

function go
{
    $Source = "http://portal.jedi.local"
    $SDL = "Test Document"
    $Site = Get-SPSite $Source
    $Web = Get-SPWeb $Site.URL
    $Lists = $Web.Lists[$SDL]
    $DocFolder = "Add New Folder Tomorrow"
    $FolderPath = [String]::Format("{0}/{1}/{2}",$Site.URL, $SDL, $DocFolder)
    [Hashtable]$docSetProperties = @{}
    $Folders = $Web.GetFolder($FolderPath)        
    $ContentType = $Lists.ContentTypes["infoCloud Set"]
    if(!$Folders.Exists)
    {
        Write-Host "true"
        $docSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create ($Lists.RootFolder, $DocFolder, $ContentType.Id, $docSetProperties) 
    }
    else
    {
        Write-Host "false"
    }
}
go 
0

精彩评论

暂无评论...
验证码 换一张
取 消