PowerShell 的 chmod 函数

11

我在寻找如何在Windows 7 Power Shell中更改文件权限的方法。因此,我找到了不同的代码片段(对我来说有些奇怪,因为我习惯于简单的chmod命令),并想知道是否可以将这些奇怪的命令封装在一个chmod函数中,并将其编写到Power Shell的$profile文件中。我猜这就是许多以前使用Linux shell,但现在使用Power Shell的用户用于更改文件权限的方法。

我对Power Shell很陌生,请帮忙提供代码。


你尝试过 Get-Help Set-ACL -full 吗?http://chrisfederico.wordpress.com/2008/02/01/setting-acl-on-a-file-or-directory-in-powershell/ - CB.
2个回答

8

这里有一个使用ACL和ACE的本地方式示例。您需要围绕它构建自己的函数。

# Get the Access Control List from the file
# Be careful $acl is more a security descriptor with more information than ACL
$acl = Get-Acl "c:\temp\test.txt"


# Show here how to refer to useful enumerate values (see MSDN)
$Right = [System.Security.AccessControl.FileSystemRights]::FullControl
$Control = [System.Security.AccessControl.AccessControlType]::Allow

# Build the Access Control Entry ACE 
# Be careful you need to replace "everybody" by the user or group you want to add rights to
$ace = New-Object System.Security.AccessControl.FileSystemAccessRule ("everybody", $Right, $Control)

# Add ACE to ACL
$acl.AddAccessRule($ace)

# Put ACL to the file
Set-Acl "c:\temp\test.txt" $acl
(Get-Acl "c:\temp\test.txt").access
Read-Host "--------- Test Here --------------"

# Remove ACE from ACL
$acl.RemoveAccessRule($ace)
Set-Acl "c:\temp\test.txt" $acl
(Get-Acl "c:\temp\test.txt").access

如果您想要移除继承 ($true) 并且不复制现有的访问控制列表 ($false),那么您可以在 $acl.AddAccessRule($ace) 之前先运行 $Acl.SetAccessRuleProtection($true,$false) - Florian Feldhaus

3

请看下面的内容:

  • Set-Acl - 运行命令Get-Help Set-Acl -Full

  • attrib.exe - 设置文件属性的标准Windows工具,不是Powershell特有的,但当然在Powershell中仍然可以使用。

  • icacls.exe - 设置ACL的标准Windows工具,不是Powershell特有的,但当然在Powershell中仍然可以使用。

来源:http://www.cs.wright.edu/~pmateti/Courses/233/Labs/Scripting/bashVsPowerShellTable.html 只需在网络上搜索chmod powershell即可。


4
只需在网络上搜索“chmod powershell”即可。 XD 这正是我来到这里的原因。 - Melchior
2
失效的链接,请移除。 - Nick McVroom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接