[英]Does “Set-Content” in PowerShell keep the File Access rights?
I plan to update some files via PowerShell. 我计划通过PowerShell更新一些文件。 Will Set-Content keep the access rights (ACL) or do I have to backup and restore these rights explicitly? Set-Content将保留访问权限(ACL),还是必须显式备份和还原这些权限?
Set-Content
(and Add-Content
) and Out-File
/ >
( >>
) do not recreate an existing target file, they replace (append to) its contents , so its ACLs are preserved . Set-Content
(和Add-Content
)和Out-File
/ >
( >>
)不会重新创建现有目标文件,它们会替换(附加)其内容 ,因此保留其ACL 。
You can verify this with the following example code: 您可以使用以下示例代码对此进行验证:
Push-Location $env:TEMP
Remove-Item tmp.txt -EA SilentlyContinue
# Create file 'tmp.txt with default ACL.
'original content' | Set-Content tmp.txt
# Modify the ACL to allow the Guests groups read access.
$acl = Get-Acl tmp.txt
$acl.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule Guests, Read, Allow))
Set-Acl tmp.txt $acl
'ACL *before* Set-Content:'
(Get-Acl tmp.txt).Access.IdentityReference | Out-Host
# Use Set-Content to replace the existing content.
'new content' | Set-Content tmp.txt
# Verify that the file's ACL hasn't changed.
'ACL *after* Set-Content:'
(Get-Acl tmp.txt).Access.IdentityReference | Out-Host
Remove-Item tmp.txt
The above yields something like the following, showing that the custom ACL was preserved even after replacing the file's content with Set-Content
: 上面的内容类似于以下内容,表明即使使用Set-Content
替换了文件内容后,仍保留了自定义ACL:
ACL *before* Set-Content:
Value
-----
BUILTIN\Guests
NT AUTHORITY\SYSTEM
BUILTIN\Administrators
WS1\jdoe
ACL *after* Set-Content:
Value
-----
BUILTIN\Guests
NT AUTHORITY\SYSTEM
BUILTIN\Administrators
WS1\jdoe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.