文件权限管理

u 案例需求

1. 在PowerShell中导入文件系统权限模块,对文件权限进行管理

2. 在PowerShell v2.0中,管理权限只有有限的两条命令Get-ACL与Set-ACL,难以对文件权限进行复杂管理,利用第三方的文件系统权限管理模块,可以非常方便的管理权限

u 知识提示

1. 权限模块的导入

将模块文件NTFSSecurity 1.2.zip解压后,拷贝NTFSSecurity文件夹到C:\windows\system32\WindowsPowerShell\v1.0\Modules中,然后在PowerShell中运行

clip_p_w_picpath002

PS C:\> Get-Module -ListAvailable

ModuleType Name ExportedCommands

---------- ---- ----------------

Manifest MSI {}

Manifest PSTerminalServices {}

Manifest BitsTransfer {}

Manifest LocalAccounts {}

Manifest NTFSSecurity {Get-OrphanedAce, Disable-Inheritance,

Script PowerFileWatcher {}

Script PowerNet {}

Manifest PSTerminalServices {}a

使用命令Import-Module NTFSSecurity导入权限模块

clip_p_w_picpath002[1]

PS C:\> Import-Module NTFSSecurity

Types added

NTFSSecurity Module loaded

查看可用的命令 Get-Command –Module NTFSSecurity

clip_p_w_picpath002[2]

PS C:\> Get-Command -Module NTFSSecurity

CommandType Name Definition

----------- ---- ----------

Function Add-Ace ...

Function Disable-Inheritance ...

Function Enable-Inheritance ...

Function Get-Ace ...

Function Get-EffectivePermissions ...

Function Get-Inheritance ...

Function Get-OrphanedAce ...

Function Get-Owner ...

Function Remove-Ace ...

Function Set-Owner ...

获取帮助,请使用Get-Help

clip_p_w_picpath002[3]

PS C:\> Get-Help Add-Ace -Detailed

2. 命令的使用

文件权限管理模块共提供了10条cmdlets来管理文件系统权限,像添加或移除ACEs(Access Control Entry-访问控制项,用来设置访问条件),设定权限继承,获取当前权限或某一个用户的有效权限。所有的cmdlets支持管道符“|”

Add-Ace

为当前对象添加设定的权限

clip_p_w_picpath002[4]

PS C:\> Get-Item d:\script | Add-Ace –Account Benet\JohnD –Acce***ights FullControl

Get-Ace

获取列出的所有权限,使用参数-ExcluedeInherited排除继承权限

clip_p_w_picpath002[5]

PS C:\> Get-Item d:\script |Get-Ace -ExcludeInherited

使用Where-Object筛选特定用户的权限

clip_p_w_picpath002[6]

PS C:\> Get-Item d:\script |Get-Ace | Where-Object {$_.ID –like “*users*”}

Get-OrphancedAce

列出无法解析的权限,这种情况一般在账户不存在或被删除后,权限的拥有者无法解析,只能显示SID的情况。可以使用Remove-Ace移除权限

clip_p_w_picpath002[7]

PS C:\> dir –Recurse |Get-OrphanedAce | Remove-Ace

Remove-Ace

移除特定账户的权限,可以通过管道符”|”接收移除对象

clip_p_w_picpath002[8]

PS C:\> Get-Ace d:\script | Remove-Ace

PS C:\> Get-OrphancedAce d:\script | Remove-Ace

Get-EffectivePermissions

显示某个账户拥有的实际权限,如果不指定账户名称,将使用当前账户

clip_p_w_picpath002[9]

PS C:\> Get-Item d:\script |Get-EffectivePermission –Account S-1-5-32-545

Get-Inheritance

显示继承是否被屏蔽,如果被屏蔽,那么InheritanceEnabled属性为False

clip_p_w_picpath002[10]

PS C:\> Get-Inheritance D:\script

Name InheritanceEnabled

---- ------------------

script True

Enable-Inheritance

启动继承

clip_p_w_picpath002[11]

PS C:\> Get-Item d:\script –Recurse | Enable-Inheritance

Disable-Inheritance

取消集成

clip_p_w_picpath002[12]

PS C:\> dir –Recurse |Disable-Inheritance

Get-Owner

获取文件或文件夹的所有者

clip_p_w_picpath002[13]

PS C:\> dir –Recurse |Get-Owner

Set-Owner

将某个特定账户设定为文件夹的所有者

clip_p_w_picpath002[14]

PS C:\> Get-Item d:\script |Set-Owner –Account builtin\administrators