代码之家  ›  专栏  ›  技术社区  ›  Mitchell Skurnik

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

  •  0
  • Mitchell Skurnik  · 技术社区  · 17 年前

    数据透视0001 Pivot0002 Pivot0003

    等等。

    编辑

    这是最终有效的代码(虽然不太干净,但确实有效)

    [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 回复  |  直到 17 年前
        1
  •  3
  •   bobbymcr    17 年前

    这样的东西应该奏效:

    $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

    // 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。

        2
  •  0
  •   Joey Gumbo    17 年前

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

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

    Get-ChildItem \downloads\pivots |
    Where-Object { $_.PSIsContainer } |
    ForEach-Object {
        $_.SetAccessControl( ... )
    }
    
        3
  •  0
  •   Commander SharePoint    8 年前

    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 
    
    推荐文章