PowerShell的chmod函数

时间:2012-09-13 06:11:49

标签: shell powershell windows-7 chmod

我正在四处寻找了解如何在Windows 7 Power Shell上chmod(更改文件的权限)文件。所以我找到了不同的(因为我习惯于简单的chmod命令,因为我已经习惯了简单的chmod命令)代码片段,并且想知道将有线命令包装在chmod函数中并将其写入$ profile文件中是很简单的。电源外壳。我想这就是许多ex-linux shell,但现在的power shell用户想要更改文件的权限。

我是Power Shell的新手。请帮我解释一下代码。

2 个答案:

答案 0 :(得分:5)

以下是使用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

答案 1 :(得分:2)

请看以下内容:

  • 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进行网络搜索。