Powershell: 获取指定深度的非继承目录的ACL信息
该Powershell脚本首先通过Get-ChildItemToDepth函数获取C盘下3层子目录的所有内容,然后通过管道导入getDirAcl过滤器,获取所有目录的Acl属性,再进入excludeAcl过滤器,以排除不需要的项。最后通过export-csv命令导出到csv文件。
Function Get-ChildItemToDepth {
...
# 单击链接查看函数定义
函数定义
}
# 获取目录的Acl属性
Filter getDirAcl {
$_ | where{$_.psiscontainer} | get-acl -ErrorAction SilentlyContinue | % {
$path = $_.path
$owner = $_.owner
$_.access | % {
New-Object PSObject -Property @{
Path = $path.Replace("Microsoft.PowerShell.Core\FileSystem::","")
Folder = $path.split("\\")[-1]
Owner = $owner
Access = $_.FileSystemRights
Control = $_.AccessControlType
User = $_.IdentityReference
Inheritance = $_.IsInherited
} # end New-Object
} # end access pipe
}
}
# 排除不必要的Acl项
Filter excludeAcl {
$_ | ? {-not $_.Inheritance} `
| ? {$_.User -notmatch "^BUILTIN"} `
| ? {$_.User -notmatch "^S-"} `
| ? {$_.User -notmatch "^CREATOR OWNER"} `
| ? {$_.User -notmatch "^NT AUTHORITY"}
}
# 捕捉脚本运行过程中遇到的错误
trap {
write "Oops..."
write $_
continue
}
调用
$dir = "C:\" Get-ChildItemToDepth $dir 3 ` | getDirAcl | excludeAcl ` | select Folder,Owner,User,Access,Path ` | export-csv "D:\szh_3.csv" -force -encoding "utf8"
版权声明
本文出自 Lesca 技术宅,转载时请注明出处及相应链接。
本文永久链接: https://lesca.me/archives/get-acl-info-of-non-inherit-directories-to-specified-depth.html

