Тут показані розбіжності між вибраною ревізією та поточною версією сторінки.
| Наступна ревізія | Попередня ревізія | ||
|
software:os:windows:powershell:files [2025/05/26 03:02] charon створено |
software:os:windows:powershell:files [2025/05/26 03:09] (поточний) charon |
||
|---|---|---|---|
| Рядок 1: | Рядок 1: | ||
| ====== Working with files and folders ====== | ====== Working with files and folders ====== | ||
| - | View owner and current permissions: | + | ===== View owner and current permissions |
| <code powershell> | <code powershell> | ||
| $myfile = " | $myfile = " | ||
| (Get-Acl $myfile).Owner | (Get-Acl $myfile).Owner | ||
| (Get-Acl $myfile).Access | Format-Table IdentityReference, | (Get-Acl $myfile).Access | Format-Table IdentityReference, | ||
| + | </ | ||
| + | |||
| + | ===== Set permissions for private folder ===== | ||
| + | Only owner and //SYSTEM// has access to folder and subfolders. All inherited permissions removed: | ||
| + | <code powershell> | ||
| + | $folder = " | ||
| + | |||
| + | # Get current ACL | ||
| + | $acl = Get-Acl $folder | ||
| + | |||
| + | # Remove all explicit access rules | ||
| + | $acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) } | ||
| + | |||
| + | # Disable inheritance and remove inherited permissions | ||
| + | $acl.SetAccessRuleProtection($true, | ||
| + | |||
| + | # Get current user | ||
| + | $currentUser = [System.Security.Principal.WindowsIdentity]:: | ||
| + | $acl.SetOwner([System.Security.Principal.NTAccount]$currentUser) | ||
| + | |||
| + | # Define new access rules | ||
| + | $ownerRule = New-Object System.Security.AccessControl.FileSystemAccessRule($currentUser, | ||
| + | $systemRule = New-Object System.Security.AccessControl.FileSystemAccessRule(" | ||
| + | |||
| + | # Apply rules | ||
| + | $acl.SetAccessRule($ownerRule) | ||
| + | $acl.SetAccessRule($systemRule) | ||
| + | |||
| + | # Save updated ACL | ||
| + | Set-Acl -Path $folder -AclObject $acl | ||
| + | |||
| + | Write-Output " | ||
| + | </ | ||
| + | |||
| + | ===== Set permissions for normal folder ===== | ||
| + | Owner, SYSTEM and Administrators has full access to folder and subfolders, Authenticated Users can read. | ||
| + | <code powershell> | ||
| + | $folder = " | ||
| + | |||
| + | # Get current ACL | ||
| + | $acl = Get-Acl $folder | ||
| + | |||
| + | # Remove all explicit access rules | ||
| + | $acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) } | ||
| + | |||
| + | # Disable inheritance and remove inherited permissions | ||
| + | $acl.SetAccessRuleProtection($true, | ||
| + | |||
| + | # Get current user | ||
| + | $currentUser = [System.Security.Principal.WindowsIdentity]:: | ||
| + | $acl.SetOwner([System.Security.Principal.NTAccount]$currentUser) | ||
| + | |||
| + | # Define new access rules | ||
| + | $ownerRule = New-Object System.Security.AccessControl.FileSystemAccessRule($currentUser, | ||
| + | $systemRule = New-Object System.Security.AccessControl.FileSystemAccessRule(" | ||
| + | $adminsRule = New-Object System.Security.AccessControl.FileSystemAccessRule(" | ||
| + | $authUsersRule = New-Object System.Security.AccessControl.FileSystemAccessRule(" | ||
| + | |||
| + | # Apply rules | ||
| + | $acl.SetAccessRule($ownerRule) | ||
| + | $acl.SetAccessRule($systemRule) | ||
| + | $acl.SetAccessRule($adminsRule) | ||
| + | $acl.SetAccessRule($authUsersRule) | ||
| + | |||
| + | # Save updated ACL | ||
| + | Set-Acl -Path $folder -AclObject $acl | ||
| + | |||
| + | Write-Output " | ||
| </ | </ | ||