Powershell 筛选器获取ACL输出-仅列出具有特定用户(如*xyz)的文件夹*

Powershell 筛选器获取ACL输出-仅列出具有特定用户(如*xyz)的文件夹*,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我编写了两个函数(不是直接从我这里),它们使用文件夹ACL创建了一个文件。 第一个函数使用max.depth参数创建文件夹列表 第二个函数根据第一个函数的输出为每个文件夹创建一个带有ACL的输出文件 欢迎使用代码或语法增强/bugrixe。 对不起,我的英语不好 输出示例: Path : D:\pub\ AccessToString : NT-AUTORITÄT\Authentifizierte Benutzer Allow ReadAndExecute, Synchron

我编写了两个函数(不是直接从我这里),它们使用文件夹ACL创建了一个文件。 第一个函数使用max.depth参数创建文件夹列表 第二个函数根据第一个函数的输出为每个文件夹创建一个带有ACL的输出文件

欢迎使用代码或语法增强/bugrixe。 对不起,我的英语不好

输出示例:

Path           : D:\pub\
AccessToString : 
NT-AUTORITÄT\Authentifizierte Benutzer Allow ReadAndExecute, Synchronize
Domain\Group_Sales_RW Allow  Modify, Synchronize
Domain\User4711 Allow  Modify, Synchronize
NT-AUTORITÄT\SYSTEM Allow  FullControl
VORDEFINIERT\Administratoren Allow  FullControl
Domain\Administrator Allow  FullControl
这两个功能:

function ListSubDir  {
    <#
    .Synopsis
    Lists Subfolders
    .Description
    Lists Subfolders
    .Example
    ListSubDir -Searchpath "D:" -Depth "2"
    #>
 param ( $Searchpath=$env:USERPROFILE,
         $Depth=2 )
        if ($Depth -gt 0) {
            GCI $Searchpath -ea SilentlyContinue |Where-Object {$_.psiscontainer} | % { ListSubDir -Searchpath $_.fullname -Depth ($Depth-1) 
            return $_ } 
            }

    }

function Get-DirectoryRights {
<#
.Synopsis
.Description
Exports Direcory ACLs to a .txt File
.Example
Foreach  ($Path in (GCI "D:\Users" -recurse -ea SilentlyContinue | where {$_.psiscontainer})) {get-directoryrights -Searchpath $Path.fullname -output "d:\ACL-Log.txt"}
.Example

#>

param   ( $Searchpath="$ENV:Userprofile",
          $Output="$Env:Temp\AusgabeACL.txt",
          $XMLTemp="$env:temp\acldata.xml")

$Folder= $Searchpath | Get-ACL -ea SilentlyContinue

$Folder| Select @{Name=”Path”;Expression={$_.PSPath.Substring($_.PSPath.IndexOf(“:”)+2) }},Owner,Access,AccessToString,@{Name=”Reported”;Expression={(Get-Date).ToShortDateString()}}, @{Name=”Computername”;Expression={$env:computername}} | Export-Clixml $XMLTemp

$acl = Import-Clixml $XMLTemp

$acl | where {$_.access } | format-list path,AccessToString | out-file $Output -append
}
为了简化文件服务器管理,我们希望消除所有特定于用户的ACL,并用组ACL替换它们。 当前输出看起来不错,但有可能只列出存在特定用户/或OU成员的文件夹?

例如:在第三个函数中,我想列出以字符“w01”(w01代表site01->our Headership)开头的用户名,并通过管道将它们传递到其他函数,以仅获取授予用户权限的文件夹,如:

Path           : D:\pub\
AccessToString : 
Domain\W01Username Allow  Modify, Synchronize
第三项职能:

function GetADUser {
<#
.Description
List AD User Details
.Example
getaduser -SearchString "w01*"
#>
param ( $Searchbase = "OU=_Benutzer,DC=Vogler-GMBH,DC=com",
        $SearchString = "*"
      )
get-aduser -Filter 'SamAccountName -like $Searchstring -or Givenname -like $Searchstring' -SearchBase $Searchbase | select *
}
函数GetADUser{ param($Searchbase=“OU=\u Benutzer,DC=Vogler GMBH,DC=com”, $SearchString=“*” ) get aduser-Filter'SamAccountName-like$Searchstring-或Givenname-like$Searchstring'-SearchBase$SearchBase |选择* }
通常,您应该避免将函数输出到文件中。让它输出一个对象,然后您可以格式化该对象并将其输出到一个文件(如果您愿意)。这种情况就是一个很好的例子。稍微修改脚本并将其输出到对象。我还为用户添加了一点过滤器。看看这个:

function Get-DirectoryRights {
<#
.Synopsis
.Description
Exports Direcory ACLs (optionally filters for a user name)
.Example
Foreach  ($Path in (GCI "D:\Users" -recurse -ea SilentlyContinue | where {$_.psiscontainer})) {get-directoryrights -Searchpath $Path.fullname -UserFilter "JDoe"}
.Example

#>

param   ( $Searchpath="$ENV:Userprofile",
          $UserFilter )
if($UserFilter){
    $Searchpath | Get-ACL -ea SilentlyContinue | Where {($_.Access|select -ExpandProperty IdentityReference) -match $UserFilter} | Select @{Name=”Path”;Expression={$_.PSPath.Substring($_.PSPath.IndexOf(“:”)+2) }},Owner,Access,AccessToString,@{Name=”Reported”;Expression={(Get-Date).ToShortDateString()}}, @{Name=”Computername”;Expression={$env:computername}}
}else{
    $Searchpath | Get-ACL -ea SilentlyContinue | Where {$_.Access} | Select @{Name=”Path”;Expression={$_.PSPath.Substring($_.PSPath.IndexOf(“:”)+2) }},Owner,Access,AccessToString,@{Name=”Reported”;Expression={(Get-Date).ToShortDateString()}}, @{Name=”Computername”;Expression={$env:computername}}
}
}
如果要筛选以W01开头的域用户,请执行以下操作:

Get-DirectoryRights -Searchpath $Folderlist.fullname -UserFilter "YourDomainName\W01" |FL Path,AccessToString | Out-File D:\ausgabe.txt   

将-match更改为-like后,脚本工作正常!干得好谢谢!!:)非常感谢你的解释!
$Folderlist= ListSubDir -Searchpath d:\pub -Depth 2
Get-directoryrights -Searchpath $Folderlist.fullname |FL Path,AccessToString | Out-File D:\ausgabe.txt   
Get-DirectoryRights -Searchpath $Folderlist.fullname -UserFilter "YourDomainName\W01" |FL Path,AccessToString | Out-File D:\ausgabe.txt