【问题标题】:Get groups in folder permissions, and export the members of the groups to csv获取文件夹权限中的组,并将组的成员导出到csv
【发布时间】:2017-03-24 13:15:16
【问题描述】:

我正在尝试获取驱动器的每个文件夹背后的组权限以及组中每个成员的用户名,然后将其导出到 CSV 文件。

问题是该组在该组之前具有目录名称,例如gescogroup/GR_blablabla,所以我使用(Remove(0,12)) 删除Gescogroup\

如您所见,我首先让用户选择他们要分析的驱动器的文件夹,然后代码运行驱动器内的文件夹,并为每个文件夹获取组,然后使用组的名称(删除“创建所有者”、“管理员”、“内置”等)我应该获取成员。

我的代码不起作用,但我没有收到任何错误。你们能帮帮我吗?

 cls
    $result = [System.Windows.MessageBox]::Show("Select the path of the drive.`n"+"Once the Drive is selected, a excel file will be made with all the informations`n"+"of the selected Harddrive's directory premissions")
    if ($result = "OK")
    {
    Add-Type -AssemblyName System.Windows.Forms
    $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
    [void]$FolderBrowser.ShowDialog()
    $Selection = $FolderBrowser.SelectedPath
    }
    $result2 = [System.Windows.MessageBox]::Show("Pleas select where to save your file")
    if ($result2 = "OK")
    {
    Add-Type -AssemblyName System.Windows.Forms
    $FolderBrowser2 = New-Object System.Windows.Forms.FolderBrowserDialog
    [void]$FolderBrowser2.ShowDialog()
    $FolderBrowser2.Description = "Select where to save your file"
    $Selection2 = $FolderBrowser2.SelectedPath
    }
    $title = "ADPermissions.csv"
    $title2 = "ADPermissions2.csv"
    $ss =$Selection1 -replace '[\W]', ''
    $subtitle = "HardDrive"+ $ss



    $OutFile = ($Selection2+"\"+$subtitle+$title)
    Write-Host = $OutFile



       Remove-Item $OutFile
         $Header = "Folder Path,IdentityReference,names"


            $RootPath = $Selection

            $Folders = dir $RootPath  | where {$_.psiscontainer -eq $true}
      try {
       foreach ($Folder in $Folders){$ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access  }
                    Foreach ($ACL in $ACLs){
                    if($ACL.IdentityReference -notlike "Administrators" -and $ACL.IdentityReference -notlike "Creator Owner"-and $ACL.IdentityReference -notlike "BUILTIN\Administrators" -and $ACL.IdentityReference -notlike "NT AUTHORITY\SYSTEM" -and  $ACL.IdentityReference -notlike "System")
                        {
                       $strAcl = $ACL.IdentityReference


                              $strAcls = $ACL.IdentityReference.ToString()
                              $strUsers=@()
                             $strNames=$strAcls.Remove(0,12)
                             $A = Get-ADGroupMember -identity $strNames -Recursive | Get-ADUser -Property DisplayName | Select Name | Sort-Object Name


                                   foreach($strNames in $A)
                                   {
                                      $strUsers+=$A

                                   }

                           }

                             }
                             }


                                 $OutInfo =  $Folder.fullname + "," + $trAcl+$strUsers
                                Add-Content -Value $OutInfo -Path $OutFile | sort-Object -Unique

                            }catch [System.IO.IOException] {

        }

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    这是解决方案:

    try {
    
            foreach ($Folder in $Folders){$ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access  }
                    Foreach ($ACL in $ACLs){
                    if($ACL.IdentityReference -notlike "Administrators" -and $ACL.IdentityReference -notlike "Creator Owner"-and $ACL.IdentityReference -notlike "BUILTIN\Administrators" -and $ACL.IdentityReference -notlike "NT AUTHORITY\SYSTEM" -and  $ACL.IdentityReference -notlike "System")
                       {
                                $strAcls = $ACL.IdentityReference.ToString()
                              $strUsers=@()
                             $strNames=$strAcls.Remove(0,12)
                             $A = Get-ADGroupMember -identity $strNames -Recursive | Get-ADUser -Property DisplayName | Select Name | Sort-Object Name
    
                             foreach ($env:USERNAME in $A){
                              $strUsers +=$env:USERNAME
                             }
                              $OutInfo =  $Folder.fullname + "," + $ACL.IdentityReference + $strUsers
                            }
                           }
    
                            }
                            }catch [System.IO.IOException] {
    
        }
    
    }
    

    【讨论】:

    • 解释你的代码是个好习惯,如果简单的话,几句话就足够了
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2014-05-04
    • 2019-09-08
    相关资源
    最近更新 更多