用户powershell获取/ downloads / pivots中所有文件夹的列表

时间:2009-10-10 20:21:59

标签: sharepoint powershell

所以我每天都会上传到像以下文件夹这样的文件夹: Pivot0001 Pivot0002 Pivot0003

等等。

我还有一些名为Pivot0001的用户组,以及需要访问该文件夹的用户。我现在需要做的是设置每个文件夹的权限(我有400左右)。我知道我需要做一个循环并设置权限。我不知道该怎么做是获取所有文件夹的列表,然后设置该文件夹的权限。

修改 我忘了说这是针对SharePoint的...抱歉

这是最终的代码(不是很干净,但它有效)

[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;

3 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

$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
}

以上适用于普通的本地文件系统路径,但SharePoint具有不同的文件夹安全模型。我找到了一个blog post by Robert Gruen,它解释了如何以编程方式设置权限。他给出了这个C#代码示例:

// 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);

我确信有一点翻译,这可以适应PowerShell。

答案 1 :(得分:0)

获取文件夹列表并不简单,但易于理解:

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

然后,您可以将其进一步导入ForEach-Object cmdlet,您可以在其中设置权限:

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

答案 2 :(得分:0)

好,找出我所缺少的,这是我的代码:

关键是提供要检查的文件夹的完整URL路径。

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