我正在處理一個 Windows Server 檔案安全專案,并且需要我們的“O:”和所有子檔案夾的顯式 ACL 權限。我想將其匯出為 CSV 以便于格式化。我正在尋找可以列出檔案夾名稱和有權訪問該檔案夾的安全組或用戶的 Powershell 腳本。
$rootpath = "O:\ADSMO"
$outfile = "ExplicitACLs.txt"
New-Variable acl
$Report = @"
Explicit permissions on folders under parent $rootpath.
"@
$Report | out-file -Encoding ASCII -FilePath $outfile
Get-ChildItem -Recurse $rootpath -Exclude "*.*" | Where-Object {$_.PSisContainer } | ForEach-Object {
$acl = Get-Acl -Path $_.FullName
$access = $acl.access
if ( $access | Where-Object { $_.IsInherited -eq $False }) {
Add-Content -Path $outfile $_
$access | Where-Object { $_.IsInherited -eq $False } | ForEach-Object {
$i = $_.IdentityReference
$t = "`t"
$r = $_.FileSystemRights
$c = "$i" "$t" "$t" "$t" "$r"
Add-Content -Path $outfile $c
}
Add-Content -Path $outfile ""
}
Clear-Variable acl
Clear-Variable access
}
Add-Content -Path $outfile ""
uj5u.com熱心網友回復:
好像您正在嘗試手動構建 CSV,這絕對不推薦。您可以使用Export-Csv將報告匯出為 CSV。從我所看到的,您的代碼可以簡化為:
Get-ChildItem O:\ADSMO -Directory -Recurse | ForEach-Object {
foreach($access in (Get-Acl $_.FullName).Access) {
# if `IsInherited = $true` go to next iteration
if($access.IsInherited) { continue }
[pscustomobject]@{
FolderName = $_.Name
FolderPath = $_.FullName
IdentityReference = $access.IdentityReference
FileSystemRights = $access.FileSystemRights
}
}
} | Export-Csv 'C:\path\to\acls.csv' -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437590.html
