
Eu tenho uma pasta com muitas subpastas
- D:\Dados\Subpasta1
- D:\Dados\Subpasta2
- D:\Dados\Subpasta3
- D:\Dados\Subpasta4
- D:\Dados\Subpasta5
- ...
Preciso criar três grupos de diretórios ativos para cada subpasta como esta.
- FS_Data-Subfolder1_Read
- FS_Data-Subfolder1_Change
- FS_Data-Subfolder1_Full
e depois disso, tenho que mapear a pasta, o grupo Activedirectory e a permissão.
Definir a permissão é a parte difícil. foi assim que cheguei. não sei como vincular o grupo à permissão e depois aplicá-lo à pasta.
$SharePath = "\\fs\data\"
$FSGroupPath = "OU=GROUPS,OU=Data,DC=DOMAIN,DC=LOCAL"
Get-ChildItem $SharePath | ForEach-Object {
$GroupNameRead = "FS_Data-" + $_ + "_Read"
$GroupNameChange = "FS_Data-" + $_ + "_Change"
$GroupNameFull = "FS_Data-" + $_ + "_Full"
New-ADGroup -Name $GroupNameRead -DisplayName $GroupNameRead -GroupScope DomainLocal -GroupCategory Security -Path $FSGroupPath -Description "Ger Läs Rättigheter till sökväg: FS\Data\$_"
New-ADGroup -Name $GroupNameChange -DisplayName $GroupNameChange -GroupScope DomainLocal -GroupCategory Security -Path $FSGroupPath -Description "Ger Ändra Rättigheter till sökväg: FS\Data\$_"
New-ADGroup -Name $GroupNameFull -DisplayName $GroupNameFull -GroupScope DomainLocal -GroupCategory Security -Path $FSGroupPath -Description "Ger Fulla Rättigheter till sökväg: FS\Data\$_"
$set_Group = $GroupNameFull
$set_rights = Modify
$acl = Get-Acl $SharePath
$permission = $set_user,$set_rights,"Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $SharePath
}
Responder1
Veja seeste exemplo de Don Jonesajuda você: você basicamente pega o objeto acl existente da pasta, adiciona uma nova regra a ele (SetAccessRule), e a regra contém o principal (usuário ou grupo, o direito e se é permitido ou negado). O aclobject atualizado é então aplicado ao arquivo/pasta usando set-acl.
#ChangeACL.ps1
$Right="FullControl"
#The possible values for Rights are
# ListDirectory, ReadData, WriteData
# CreateFiles, CreateDirectories, AppendData
# ReadExtendedAttributes, WriteExtendedAttributes, Traverse
# ExecuteFile, DeleteSubdirectoriesAndFiles, ReadAttributes
# WriteAttributes, Write, Delete
# ReadPermissions, Read, ReadAndExecute
# Modify, ChangePermissions, TakeOwnership
# Synchronize, FullControl
$StartingDir=Read-Host "What directory do you want to start at?"
$Principal=Read-Host "What security principal do you want to grant" `
"$Right to? `n Use format domain\username or domain\group"
#define a new access rule.
#note that the $rule line has been artificially broken for print purposes.
#it needs to be one line. the online version of the script is properly
#formatted.
$rule=new-object System.Security.AccessControl.FileSystemAccessRule($Principal,$Right,"Allow")
foreach ($file in $(Get-ChildItem $StartingDir -recurse)) {
$acl=get-acl $file.FullName
#Add this access rule to the ACL
$acl.SetAccessRule($rule)
#Write the changes to the object
set-acl $File.Fullname $acl
}
Responder2
Quando você especifica o -PassThru
parâmetro no New-ADGroup
cmdlet, ele retorna o novo grupo. O objeto ADGroup que você recebe contém uma propriedade SID que você pode usar para passar IdentityReference
a regra de acesso:
$readGroup = New-ADGroup -Name $GroupNameRead -DisplayName $GroupNameRead -GroupScope DomainLocal -GroupCategory Security -Path $FSGroupPath -Description "Ger Läs Rättigheter till sökväg: FS\Data\$_" -PassThru
if(-not($readGroup)) # Make sure it got created, if not, handle the error
{
# Error handling in here
}
else
{
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($readGroup.SID,Read,Allow)
}