2009-10-10 4 views
0

J'ai une charge de bateau de tableaux croisés dynamiques que je télécharge tous les jours dans des dossiers tels que: Pivot0001 Pivot0002 Pivot0003powershell utilisateur pour obtenir une liste de tous les dossiers/téléchargements/pivots

et ainsi de suite.

J'ai également des groupes d'utilisateurs appelés Pivot0001 et ainsi de suite avec les utilisateurs qui ont besoin d'accéder à ce dossier. Ce que je dois maintenant faire est de définir les autorisations sur chaque dossier (j'en ai environ 400). Je sais que je dois faire une boucle et définir des permissions. Ce que je ne sais pas comment faire est d'obtenir une liste de tous les dossiers, puis définir les autorisations pour ce dossier.

EDIT J'ai oublié de dire que c'est pour SharePoint ... désolé

Voici le code final qui a fonctionné (pas vraiment propre, mais ça marche)

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

Répondre

3

Quelque chose comme cela devrait fonctionner:

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

Les travaux ci-dessus pour un chemin de système de fichiers local normal, mais SharePoint a un modèle de sécurité de dossier différent. J'ai trouvé un blog post by Robert Gruen qui explique comment définir par programme les autorisations. Il donne cet exemple de code 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); 

Je suis sûr qu'avec un peu de traduction, cela pourrait être adapté à PowerShell.

+1

+1. Mais j'aime mieux la propriété PSIsContainer :-) – Joey

+0

J'ai oublié de dire que c'est pour SharePoint ... désolé à ce sujet .. je vais essayer avec UNC pour voir si cela fonctionne –

+0

Cela devrait fonctionner tant que l'utilisateur que vous utilisez le script a la permission du chemin distant. – bobbymcr

0

Obtenir un liste de dossiers n'est pas exactement simple, mais facilement compréhensible:

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

Vous pouvez ensuite t chapeau plus loin dans l'applet de commande ForEach-Object où vous pouvez définir les autorisations:

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

Votre méthode est plus propre mais le bobby était plus complet :) –

Questions connexes