从 Get-Acl 中获取组成员名称并附加到 CSV

Get group member names from Get-Acl and Append to CSV

我需要提取一个组的所有成员,然后将其格式化为名称,所以我只得到全名,例如 "Don Joe" is member of my-domain\UNCShareDrive

我需要在导出 ACL 列表后将其显示在我的 CSV 中。

举个例子:

Path                                                                 FileSystemRights AccessControlType IdentityReference          
----                                                                 ---------------- ----------------- -----------------          
Microsoft.PowerShell.Core\FileSystem::\fileshare\management\DK           FullControl             Allow MY-DOMAIN\Drev Management
Microsoft.PowerShell.Core\FileSystem::\fileshare\management\           FullControl             Allow BUILTIN\Administratorer    
Microsoft.PowerShell.Core\FileSystem::\fileshare\management\     FullControl             Allow MY-DOMAIN\Drev Management
Microsoft.PowerShell.Core\FileSystem::\fileshare\management\     FullControl             Allow BUILTIN\Administratorer    




**These persons has access to the drive:**

**Finding group:  MY-DOMAIN\Administrators** 

Name                   SamAccountName Mail                     
----                   -------------- ----                     
Administrator          Administrator  Administrator@my-domain.dk

这是我目前在 Powershell 中所做的:

$Searching = Get-ADGroup -Filter * -SearchBase "DC=my-domain,DC=local"

$mypath = "U:\mytest.csv"

if((Test-Path -Path "U:\mytest.csv" -pathtype Container) -eq $True){
  Write-Host -ForegroundColor red "We found a file: $mypath --> REMOVING!`r`n"
  Remove-Item $mypath -Force
}

$ShareName = "\\sharename\D$"

$shares = Get-Childitem -path $ShareName |
          Where-Object {$_.PSIsContainer} |
          Get-ACL |
          Select-Object Path -ExpandProperty Access |
          Select Path, FileSystemRights,AccessControlType,IdentityReference |
          export-csv $mypath -Delimiter ';' -NoTypeInformation -Encoding UTF8
$foldertotal = 0
Add-Content $mypath "" 
Add-Content $mypath "" 
Add-Content $mypath "Disse personer har adgang til share navnet"
Add-Content $mypath "" 
Add-Content $mypath ""

$myLoop = ''
foreach ($testLoop in $myLoop) {
  $Group = Get-ADGroup -Filter * -SearchBase "DC=my-domain,DC=local" -Properties IdentityReference |
           Select-Object Name # Need only groups in $Shares - who are displayed
  $myLoop += Write-Host -ForegroundColor Yellow "Finding group: $($Group.name).....`n"
  $myLoop += ForEach ($Group in $share) {
               Get-ADGroupMember -identity $($Group.name) -recursive |
                 Get-ADUser -Properties Name,SamAccountName,Mail |
                 Select-Object Name,SamAccountName,Mail
             } 
  $testLoop | Out-File -filePath $mypath -append -encoding utf8
}

Write-Host -ForegroundColor Cyan "$($Group.name) is now exported to: $mypath`n"

我也很难只过滤掉共享路径:

$shares.Replace('Microsoft.PowerShell.Core\FileSystem::', ' ') |
  Out-File -filePath $mypath -append -encoding utf8

当我使用用户输入而不是在没有控制台提示的情况下自动循环它时,为什么这个东西会起作用:

$Searching = Get-ADGroup -Filter * -SearchBase "DC=MY-DOMAIN,DC=local"

$mypath = "U:\mytest.csv"
$networkPath = "\ShareName\D$"
$acl = Get-acl -path $networkPath

if((Test-Path -Path "U:\mytest.csv" -pathtype Container) -eq $True){
  Write-Host -ForegroundColor red "We found a file: $mypath --> REMOVING!`r`n"
  Remove-Item $mypath -Force
}

Write-Host -ForegroundColor Yellow "Eksempel på share: "`r`n\ShareName\D$`r`n\ShareTwo\E$`r`n\ShareName\management`r`n""
$ShareName = Read-host "Hvilket sharenavn vil du finde Access Control List fra?"
if($ShareName -eq "1"){
  $Sharename = "\ShareName\D$"
}
if($ShareName -eq "2"){
  $Sharename = "\ShareTwo\E$"
}
if($ShareName -eq "3"){
  $Sharename = "\ShareName\management"
}

Get-Childitem -path $ShareName |
  Where-Object {$_.PSIsContainer} |
  Get-ACL |
  Select-Object Path -ExpandProperty Access |
  Select Path, FileSystemRights,AccessControlType,IdentityReference |
  FT -AutoSize |
  Out-File -Encoding utf8 $mypath
Add-Content $mypath "" 
Add-Content $mypath "" 
Add-Content $mypath "Disse personer har adgang til share navnet"
Add-Content $mypath "" 

$users = get-aduser -Filter {Name -Like "*"} -Searchbase "dc=MY-DOMAIN,dc=local" -Properties MemberOf |
         Where-Object { $_.Enabled -eq 'True' }

Write-Host -ForegroundColor red "Example:  Drev Management`r`n"
$myGroups = Read-Host "Hvilken Gruppe vil du bruge?"
if($myGroups -eq "1"){
  $myGroups = " Drev Management"
}
$Groups = Get-ADGroup -filter {Name -like $myGroups} | Select-Object Name 

Add-Content $mypath "Finding group: $($group.name)"
Write-Host -ForegroundColor Yellow "Finding group: $($Group.name).....`n"
Add-Content -Path $mypath $result

ForEach ($Group in $Groups) {
  Get-ADGroupMember -identity $($Group.name) -recursive |
    Get-ADUser -Properties Name,SamAccountName,Mail |
    Select-Object Name,SamAccountName,Mail |
    FT -AutoSize |
    Out-File -Append -encoding utf8 $mypath
}
Write-Host -ForegroundColor Cyan "$($Group.name) is now exported to: $mypath`n"

您的第二个代码示例有效,因为它不会犯与第一个相同的错误。

$myLoop = ''
foreach ($testLoop in $myLoop) {
  $Group = Get-ADGroup -Filter * ...
  $myLoop += Write-Host ...
  $myLoop += ForEach ($Group in $share) {
               Get-ADGroupMember ...
             } 
  $testLoop | Out-File -filePath $mypath -append -encoding utf8
}
  • 您将 $myLoop 设为一个空字符串,然后遍历 $myLoop 的每个元素(一个空字符串),因此无论您向 [=14 附加什么内容,您都会得到一个循环周期=] 循环中。
  • 您将 $testLoop(空字符串)的内容写入输出文件,而不是群组成员。
  • 你将AD组读入变量$Group,然后在foreach ($Group in $share)中使用$Group作为循环变量时覆盖其内容。
  • $share 是空的,因为它从未在任何地方初始化。即使 $shares 是空的,因为当您将 ACL 信息写入 CSV 时,没有留下可以分配给变量的输出。

附带说明:您不能将 Write-Host 输出附加到任何内容,因为 cmdlet 写入主机进程(即控制台),而不是可以捕获或重定向的流。

在写入文件之前,您需要捕获 $shares 中的 ACL 信息:

$shares = Get-Childitem -Path $ShareName |
          Where-Object {$_.PSIsContainer} |
          Get-ACL |
          Select-Object Path -ExpandProperty Access |
          Select Path, FileSystemRights,AccessControlType,IdentityReference
$shares | Export-Csv $mypath -Delimiter ';' -NoTypeInformation -Encoding UTF8

然后你可以像这样确定有权访问共享的组的成员:

$shares | Select-Object -Expand IdentityReference |
  Select-Object -Expand Value |
  ForEach-Object {
    $name = $_ -replace '^DOMAIN\'  # <-- replace with actual domain name
    Get-ADObject -Filter { Name -eq $name }
  } |
  Where-Object { $_.ObjectClass -eq 'group' } |
  Get-ADGroupMember |
  Get-ADUser -Properties * |
  Select-Object Name, SamAccountName, Mail |
  Out-File $mypath -Append -Encoding UTF8

请注意,这不会解析嵌套组,也不会区分共享。

如果要同时存储组名及其成员,则需要向管道添加循环:

$shares | Select-Object -Expand IdentityReference |
  Select-Object -Expand Value |
  ForEach-Object {
    $name = $_ -replace '^DOMAIN\'  # <-- replace with actual domain name
    Get-ADObject -Filter { Name -eq $name }
  } |
  Where-Object { $_.ObjectClass -eq 'group' } |
  ForEach-Object {
    $_
    Get-ADGroupMember -Identity $_ |
      Get-ADUser -Properties * |
      Select-Object Name, SamAccountName, Mail
  } |
  Out-File $mypath -Append -Encoding UTF8