Advertisement
Guest User

List-Permissions.ps1

a guest
Sep 30th, 2011
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -version 2
  2. # ==================================================================================================
  3. #
  4. # Microsoft PowerShell Source File
  5. #
  6. # NAME:      List-Permissions v2.0
  7. #
  8. # AUTHOR:    YellowOnline
  9. # DATE  :    27/09/2011
  10. #
  11. # USAGE: List-Permissions.ps1 <local path or UNC>
  12. # ==================================================================================================
  13. ####################################################################################################
  14.  
  15. # #region PARAMETERS
  16. Param([string]$ParamPath = $(Throw "ERROR: Parameter missing. Please provide a path or UNC."))
  17. If ($(Test-Path $ParamPath) -EQ $False) {Throw "ERROR: The provided path does not exist."}
  18. # #endregion
  19.  
  20. # #region VARIABLES
  21. $ScriptVersion = "2.00"
  22. $ScriptPath = Split-Path -Parent $myInvocation.MyCommand.Definition
  23. $ScriptName = Split-Path -Leaf $myInvocation.MyCommand.Definition
  24. $LogFile = "$env:Public"+"\"+"YellowOnline\"+$($ScriptName.Replace(".ps1",""))+"\"+$($ScriptName.Replace(".ps1",""))+".log"
  25.  
  26. $OutputFile = "$ScriptPath\List-Permissions.xlsx"
  27.  
  28. $colOutput = @()
  29. # #endregion
  30.  
  31. # #region FUNCTIONS
  32. Function Write-Log($LogMessage, $LogFile)
  33.     {
  34.     $LogPath = Split-Path -Path $LogFile -Parent
  35.     $LogFile = Split-Path -Path $LogFile -Leaf
  36.     $LogTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  37.     If (-NOT (Test-Path -Path $LogPath))
  38.         {
  39.         New-Item -Path $LogPath -ItemType Directory | Out-Null
  40.         }
  41.     If (-NOT (Test-Path -Path $LogPath/$LogFile))
  42.         {
  43.         New-Item -Path $LogPath/$LogFile -ItemType File | Out-Null
  44.         }
  45.     Write-Output "$LogTime $LogMessage" | Out-File -FilePath $LogPath/$LogFile -Append
  46.     Write-Host "$LogTime $LogMessage"
  47.     }
  48.    
  49. Function Use-Culture ([System.Globalization.CultureInfo]$Culture,[ScriptBlock]$Script)
  50.     {
  51.     $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
  52.     Trap
  53.         {
  54.         [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
  55.         }
  56.     [System.Threading.Thread]::CurrentThread.CurrentCulture = $Culture
  57.     Invoke-Command $Script
  58.     [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
  59.     }
  60.    
  61. Function Get-Permissions ($InputPath)
  62.     {
  63.     #INPUT VALIDATION
  64.     If ($($InputPath -Match "^\\\\[a-zA-Z0-9\.]+\\[a-zA-Z]\$\\") -EQ $True)
  65.         {
  66.         #Administrative share
  67.         $ComputerName = $($InputPath.Split("\"))[2]
  68.         $Win32_Shares = Get-WmiObject -Class Win32_Share -ComputerName $ComputerName
  69.         $Win32_LogicalShareSecuritySettings = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -ComputerName $ComputerName
  70.         $FolderPath = $($InputPath).Replace("$",":").Replace("$("\\"+$Computername+"\")","")
  71.         }
  72.     ElseIf ($($InputPath -Match "^[a-zA-Z]:\\") -EQ $True)
  73.         {
  74.         #Local Path
  75.         $ComputerName = "."
  76.         $Win32_Shares = Get-WmiObject -Class Win32_Share -ComputerName $ComputerName
  77.         $Win32_LogicalShareSecuritySettings = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -ComputerName $ComputerName
  78.         $FolderPath = $InputPath
  79.         }
  80.     ElseIf ($($InputPath -Match "^\\\\[a-zA-Z0-9\.]+\\") -EQ $True)
  81.         {
  82.         #Share
  83.         $ComputerName = $($InputPath.Split("\"))[2]
  84.         $Win32_Shares = Get-WmiObject -Class Win32_Share -ComputerName $ComputerName
  85.         $Win32_LogicalShareSecuritySettings = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -ComputerName $ComputerName
  86.         $FolderPath = $($Win32_Shares | Where-Object {$_.Path -EQ $($InputPath.Replace("$("\\"+$Computername+"\")",""))}).Name
  87.         }
  88.     Else
  89.         {
  90.         #Unknown
  91.         Exit;
  92.         }
  93.    
  94.     #NTFS
  95.     $NTFSPermissions = @()
  96.     $NTFSPermissions = Get-Acl $InputPath -ErrorAction SilentlyContinue | Select-Object Access
  97.     $colNTFSPermissions = $NTFSPermissions.Access
  98.    
  99.     #SHARE
  100.     $LocalShareName = $Win32_Shares | Where-Object {$_.Path -EQ $FolderPath}
  101.     $colSharePermissions = @()
  102.     If ($LocalShareName -NE $Null)
  103.         {
  104.         $ShareName = $Win32_LogicalShareSecuritySettings | Where-Object {$_.Name -EQ $LocalShareName.Name}
  105.         $SecurityDescriptor = $ShareName.GetSecurityDescriptor()
  106.         ForEach ($DACL in $SecurityDescriptor.Descriptor.DACL)
  107.             {
  108.             # #region Code from Hugo Peeters, http://www.peetersonline.nl/
  109.             $objSharePermissions = "" | Select-Object    Domain, ID, IdentityReference, AccessMask, AceType
  110.             $objSharePermissions.Domain = $DACL.Trustee.Domain
  111.             $objSharePermissions.ID = $DACL.Trustee.Name
  112.             Switch ($DACL.AccessMask)
  113.                 {
  114.                 2032127 {$AccessMask = "FullControl"}
  115.                 1179785 {$AccessMask = "Read"}
  116.                 1180063 {$AccessMask = "Read, Write"}
  117.                 1179817 {$AccessMask = "ReadAndExecute"}
  118.                 -1610612736 {$AccessMask = "ReadAndExecuteExtended"}
  119.                 1245631 {$AccessMask = "ReadAndExecute, Modify, Write"}
  120.                 1180095 {$AccessMask = "ReadAndExecute, Write"}
  121.                 268435456 {$AccessMask = "FullControl (Sub Only)"}
  122.                 Default {$AccessMask = $DACL.AccessMask}
  123.                 }
  124.             $objSharePermissions.AccessMask = $AccessMask
  125.             Switch ($DACL.AceType)
  126.                 {
  127.                 0 {$AceType = "Allow"}
  128.                 1 {$AceType = "Deny"}
  129.                 2 {$AceType = "Audit"}
  130.                 }
  131.             $objSharePermissions.AceType = $AceType
  132.             # #endregion
  133.             If ($objSharePermissions.Domain -EQ $Null)
  134.                 {
  135.                 $objSharePermissions.IdentityReference = $objSharePermissions.ID
  136.                 }
  137.             Else
  138.                 {
  139.                 $objSharePermissions.IdentityReference = $objSharePermissions.Domain + "\" + $objSharePermissions.ID
  140.                 }
  141.             $colSharePermissions += $objSharePermissions
  142.             }
  143.         }
  144.    
  145.     #MERGED
  146.     $colPermissions = @()
  147.     $colIdentities = @()
  148.     ForEach ($objNTFSPermissions in $colNTFSPermissions)
  149.         {
  150.         $objPermissions = "" | Select-Object Path, Domain, ID, IdentityReference, FileSystemRights, AccessControlType, IsInherited, InheritanceFlags, PropagationFlags, AccessMask, AceType
  151.         $objPermissions.Path = $InputPath
  152.         Switch ($objNTFSPermissions.IdentityReference)
  153.                 {
  154.                 "Everyone" {$objPermissions.Domain = "";$objPermissions.ID = "Everyone"}
  155.                 "CREATOR OWNER" {$objPermissions.Domain = "";$objPermissions.ID = "CREATOR OWNER"}
  156.                 Default {$objPermissions.Domain = $($objNTFSPermissions.IdentityReference.Value.Split("\"))[0];$objPermissions.ID = $($objNTFSPermissions.IdentityReference.Value.Split("\"))[1]}
  157.                 }
  158.         $objPermissions.IdentityReference = $objNTFSPermissions.IdentityReference
  159.         $objPermissions.FileSystemRights = $objNTFSPermissions.FileSystemRights
  160.         $objPermissions.AccessControlType = $objNTFSPermissions.AccessControlType
  161.         $objPermissions.IsInherited = $objNTFSPermissions.IsInherited
  162.         $objPermissions.InheritanceFlags = $objNTFSPermissions.InheritanceFlags
  163.         $objPermissions.PropagationFlags = $objNTFSPermissions.PropagationFlags
  164.         $objPermissions.AccessMask = $($colSharePermissions | Where-Object {$_.IdentityReference -EQ $objNTFSPermissions.IdentityReference}).AccessMask
  165.         $objPermissions.AceType = $($colSharePermissions | Where-Object {$_.IdentityReference -EQ $objNTFSPermissions.IdentityReference}).AceType
  166.         $colPermissions += $objPermissions
  167.         $colIdentities += $objNTFSPermissions.IdentityReference
  168.         }
  169.     ForEach ($objSharePermissions in $colSharePermissions)
  170.         {
  171.         If ($colIdentities -NotContains $objSharePermissions.IdentityReference)
  172.             {
  173.             $objPermissions = "" | Select-Object Path, Domain, ID, IdentityReference, FileSystemRights, AccessControlType, IsInherited, InheritanceFlags, PropagationFlags, AccessMask, AceType
  174.             $objPermissions.Path = $InputPath
  175.             $objPermissions.Domain = $objSharePermissions.Domain
  176.             $objPermissions.ID = $objSharePermissions.ID
  177.             $objPermissions.IdentityReference = $objSharePermissions.IdentityReference
  178.             $objPermissions.FileSystemRights = $Null
  179.             $objPermissions.AccessControlType = $Null
  180.             $objPermissions.IsInherited = $Null
  181.             $objPermissions.InheritanceFlags = $Null
  182.             $objPermissions.PropagationFlags = $Null
  183.             $objPermissions.AccessMask = $objSharePermissions.AccessMask
  184.             $objPermissions.AceType = $objSharePermissions.AceType
  185.             $colPermissions += $objPermissions
  186.             }
  187.         }
  188.     Return $colPermissions
  189.     #EOF
  190.     }
  191.  
  192. Function Enumerate-Folders ()
  193.     {
  194.     Write-Log "Enumerating folders..." $LogFile
  195.     $colFolders = @()
  196.     Get-ChildItem $ParamPath -Recurse -ErrorAction SilentlyContinue| Where-Object {$_.PSisContainer -EQ $True}| ForEach-Object {$colFolders += $_.FullName}
  197.     $colFolders += $ParamPath
  198.     $colFolders = $colFolders | Sort-Object
  199.        
  200.     ForEach ($objFolders in $colFolders)
  201.         {
  202.         $script:colOutput += Get-Permissions $objFolders
  203.         }
  204.     Write-Log "    Done!" $LogFile
  205.     }
  206.    
  207. Function Export-Excel ()
  208.     {
  209.     # #region CONSTANTS
  210.     #Lines
  211.     $xlAutomatic        = -4105
  212.     $xlBottom           = -4107
  213.     $xlCenter           = -4108
  214.     $xlContext          = -5002
  215.     $xlContinuous       =     1
  216.     $xlDiagonalDown     =     5
  217.     $xlDiagonalUp       =     6
  218.     $xlEdgeBottom       =     9
  219.     $xlEdgeLeft         =     7
  220.     $xlEdgeRight        =    10
  221.     $xlEdgeTop          =     8
  222.     $xlInsideHorizontal =    12
  223.     $xlInsideVertical   =    11
  224.     $xlNone             = -4142
  225.     $xlAutomatic        = -4105
  226.     $xlThin             =     2
  227.     $xlMedium           = -4138
  228.     $xlThick            =     4
  229.    
  230.     #VerticalAlignment
  231.     $xlTop              = -4160
  232.     $xlCenter           = -4108
  233.     $xlBottom           = -4107
  234.  
  235.     #HorizontalAlignment
  236.     $xlLeft             = -4131
  237.     $xlCenter           = -4108
  238.     $xlRight            = -4152
  239.     # #endregion
  240.    
  241.     # #region INITIALIZATION
  242.     Write-Log "Starting Excel..." $LogFile
  243.     Try
  244.         {
  245.         $Excel = New-Object -ComObject Excel.Application
  246.         $Workbook = $Excel.Workbooks.Add()
  247.         $Excel.Visible = $False
  248.         }
  249.     Catch
  250.         {
  251.         Write-Log "    ERROR: Could not start Excel! Error: $($error[0])" $LogFile
  252.         Write-Log "Quitting." $LogFile
  253.         Exit;
  254.         }
  255.     Write-Log "    Done!" $LogFile
  256.     # #endregion
  257.    
  258.     # #region GENERAL
  259.     Write-Log "Creating worksheet GENERAL..." $LogFile
  260.     $UserName = "$env:userdomain\$env:username"
  261.     $Worksheet1 = $Workbook.Worksheets.Item(1)
  262.     $Worksheet1.Name = "General"
  263.     $Worksheet1.Columns.Item("A").ColumnWidth = 40
  264.     $Worksheet1.Columns.Item("A").Font.Bold = $True
  265.     $Worksheet1.Columns.Item("B").ColumnWidth = 15
  266.     $Worksheet1.Columns.Item("B").Font.Bold = $True
  267.     $Worksheet1.Columns.Item("C").ColumnWidth = 40
  268.     $Worksheet1.Columns.Item("C").Font.Bold = $False
  269.     $Worksheet1.Cells.Item(1,1) = "File Permission Report"
  270.     $Worksheet1.Cells.Item(1,1).Font.Size = 20
  271.     $Worksheet1.Cells.Item(5,2) = "Generated on:"  
  272.     $Worksheet1.Cells.Item(5,3) = "$(Get-Date -Format F)"  
  273.     $Worksheet1.Cells.Item(6,2) = "By:"
  274.     $Worksheet1.Cells.Item(6,3) = "$UserName"
  275.     $Worksheet1.Range("A1:C1").MergeCells = $True
  276.     $Worksheet1.Range("A1:C1").BorderAround(1,4,1)
  277.     $Worksheet1.Range("A1:C1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.  
  278.     Write-Log "    Done!" $LogFile
  279.     # #endregion
  280.    
  281.     # #region NTFS SIMPLIFIED
  282.         # #region INIT
  283.     Write-Log "Creating worksheet SIMPLIFIED..." $LogFile
  284.     $Worksheet2 = $Workbook.Worksheets.Item(2)
  285.     $Worksheet2.Name = "Simplified"
  286.         # #endregion
  287.    
  288.         # #region DESIGN
  289.     #Font
  290.     $Worksheet2.Range("1:2").Font.Bold = $True
  291.        
  292.     #Width & Height
  293.     $Worksheet2.Range("A:B").ColumnWidth = 45
  294.     $Worksheet2.Range("C:C").ColumnWidth = 3
  295.     $Worksheet2.Range("D:D").ColumnWidth = 25
  296.     $Worksheet2.Range("E:E").ColumnWidth = 10
  297.     $Worksheet2.Range("F:F").ColumnWidth = 3
  298.     $Worksheet2.Range("G:G").ColumnWidth = 45
  299.     $Worksheet2.Range("2:2").RowHeight = 125
  300.    
  301.     #Panes
  302.     $Worksheet2.Select()
  303.     $Worksheet2.Range("C3:C3").Select()
  304.     $Excel.ActiveWindow.FreezePanes = $True
  305.    
  306.     #Lines
  307.     $Worksheet2.Range("B:B").Borders.Item($xlEdgeLeft).LineStyle = $xlContinuous
  308.     $Worksheet2.Range("B:B").Borders.Item($xlEdgeLeft).Color = 1
  309.     $Worksheet2.Range("B:B").Borders.Item($xlEdgeLeft).Weight = $xlThin
  310.     $Worksheet2.Range("E:E").Borders.Item($xlEdgeLeft).LineStyle = $xlContinuous
  311.     $Worksheet2.Range("E:E").Borders.Item($xlEdgeLeft).Color = 1
  312.     $Worksheet2.Range("E:E").Borders.Item($xlEdgeLeft).Weight = $xlThin
  313.  
  314.     $Worksheet2.Range("A:B").BorderAround(1,4,1)
  315.     $Worksheet2.Range("D:E").BorderAround(1,4,1)
  316.     $Worksheet2.Range("G:G").BorderAround(1,4,1)
  317.    
  318.     $Worksheet2.Range("A2:B2").Borders.Item($xlInsideVertical).LineStyle = $xlNone
  319.     $Worksheet2.Range("A2:B2").BorderAround(1,4,1)
  320.     $Worksheet2.Range("D2:E2").Borders.Item($xlInsideVertical).LineStyle = $xlNone
  321.     $Worksheet2.Range("D2:E2").BorderAround(1,4,1)
  322.     $Worksheet2.Range("G2:G2").BorderAround(1,4,1)
  323.    
  324.     #Colour
  325.     $Worksheet2.Range("A1:B1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.
  326.     $Worksheet2.Range("D1:E1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.
  327.     $Worksheet2.Range("G1:G1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.
  328.     $Worksheet2.Range("A2:B2").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  329.     $Worksheet2.Range("D2:E2").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  330.     $Worksheet2.Range("G2:G2").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  331.  
  332.     #Merging
  333.     $Worksheet2.Range("A1:B1").MergeCells = $True
  334.     $Worksheet2.Range("D1:E1").MergeCells = $True
  335.         # #endregion       
  336.    
  337.         # #region DATA
  338.     $Row = 0
  339.     $Row++
  340.     $Worksheet2.Cells.Item($Row,1) = "System"
  341.     $Worksheet2.Cells.Item($Row,4) = "Share"
  342.     $Worksheet2.Cells.Item($Row,7) = "NTFS"
  343.     $Row++
  344.     $Worksheet2.Cells.Item($Row,1) = "Path"
  345.     $Worksheet2.Cells.Item($Row,2) = "IdentityReference"
  346.    
  347.     $Worksheet2.Cells.Item($Row,4) = "AccessMask"
  348.     $Worksheet2.Cells.Item($Row,5) = "AceType"
  349.    
  350.     $Worksheet2.Cells.Item($Row,7) = "FileSystemRights"
  351.     $Row++
  352.    
  353.     ForEach ($objOutput in $colOutput)
  354.         {
  355.         $Worksheet2.Cells.Item($Row,1) = "$($objOutput.Path)"
  356.         $Worksheet2.Cells.Item($Row,2) = "$($objOutput.IdentityReference)"
  357.         $Worksheet2.Cells.Item($Row,4) = "$($objOutput.AccessMask)"
  358.         $Worksheet2.Cells.Item($Row,5) = "$($objOutput.AceType)"
  359.         $Worksheet2.Cells.Item($Row,7) = "$($objOutput.FileSystemRights)"
  360.         $Row++
  361.         }
  362.     Write-Log "    Done!" $LogFile
  363.         # #endregion
  364.    
  365.     # #endregion
  366.    
  367.     # #region NTFS ADVANCED
  368.         # #region INIT
  369.     Write-Log "Creating worksheet ADVANCED..." $LogFile
  370.     $Worksheet3 = $Workbook.Worksheets.Item(3)
  371.     $Worksheet3.Name = "Advanced"
  372.         # #endregion
  373.        
  374.         # #region DESIGN
  375.     #Font
  376.     $Worksheet3.Range("1:2").Font.Bold = $True
  377.        
  378.     #Width & Height
  379.     $Worksheet3.Range("A:B").ColumnWidth = 45
  380.     $Worksheet3.Range("C:C").ColumnWidth = 3
  381.     $Worksheet3.Range("D:D").ColumnWidth = 25
  382.     $Worksheet3.Range("E:E").ColumnWidth = 10
  383.     $Worksheet3.Range("F:F").ColumnWidth = 3
  384.     $Worksheet3.Range("G:G").ColumnWidth = 45
  385.     $Worksheet3.Range("F:AL").ColumnWidth = 3
  386.     $Worksheet3.Range("A:E").HorizontalAlignment = $xlLeft
  387.     $Worksheet3.Range("2:2").RowHeight = 125
  388.     $Worksheet3.Range("F2:AL2").Orientation = -90
  389.    
  390.     #Panes
  391.     $Worksheet3.Select()
  392.     $Worksheet3.Range("C3:C3").Select()
  393.     $Excel.ActiveWindow.FreezePanes = $True
  394.    
  395.     #Lines
  396.     $Worksheet3.Range("B:B").Borders.Item($xlEdgeLeft).LineStyle = $xlContinuous
  397.     $Worksheet3.Range("B:B").Borders.Item($xlEdgeLeft).Color = 1
  398.     $Worksheet3.Range("B:B").Borders.Item($xlEdgeLeft).Weight = $xlThin
  399.     $Worksheet3.Range("E:E").Borders.Item($xlEdgeLeft).LineStyle = $xlContinuous
  400.     $Worksheet3.Range("E:E").Borders.Item($xlEdgeLeft).Color = 1
  401.     $Worksheet3.Range("E:E").Borders.Item($xlEdgeLeft).Weight = $xlThin
  402.  
  403.     $Worksheet3.Range("A:B").BorderAround(1,4,1)
  404.     $Worksheet3.Range("D:E").BorderAround(1,4,1)
  405.     $Worksheet3.Range("G:AL").BorderAround(1,4,1)
  406.    
  407.     $Worksheet3.Range("A2:B2").Borders.Item($xlInsideVertical).LineStyle = $xlNone
  408.     $Worksheet3.Range("A2:B2").BorderAround(1,4,1)
  409.     $Worksheet3.Range("D2:E2").Borders.Item($xlInsideVertical).LineStyle = $xlNone
  410.     $Worksheet3.Range("D2:E2").BorderAround(1,4,1)
  411.     $Worksheet3.Range("G2:AL2").BorderAround(1,4,1)
  412.    
  413.     #Colour
  414.     $Worksheet3.Range("G:G").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  415.     $Worksheet3.Range("I:I").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  416.     $Worksheet3.Range("K:K").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  417.     $Worksheet3.Range("M:M").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  418.     $Worksheet3.Range("O:O").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  419.     $Worksheet3.Range("Q:Q").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  420.     $Worksheet3.Range("S:S").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  421.     $Worksheet3.Range("U:U").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  422.     $Worksheet3.Range("W:W").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  423.     $Worksheet3.Range("Y:Y").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  424.     $Worksheet3.Range("AA:AA").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  425.     $Worksheet3.Range("AC:AC").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  426.     $Worksheet3.Range("AE:AE").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  427.     $Worksheet3.Range("AG:AG").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  428.     $Worksheet3.Range("AI:AI").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  429.     $Worksheet3.Range("AK:AK").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  430.    
  431.     $Worksheet3.Range("A1:B1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.
  432.     $Worksheet3.Range("D1:E1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.
  433.     $Worksheet3.Range("G1:AL1").Interior.Color = 0x00538DD5 # RGB(83,141,213), but not the proper colour. Excel messes up RGB.
  434.     $Worksheet3.Range("A2:B2").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  435.     $Worksheet3.Range("D2:E2").Interior.Color = 0x008DB4E2 # RGB (141,180,226), but not the proper colour. Excel messes up RGB.
  436.  
  437.     #Merging
  438.     $Worksheet3.Range("A1:B1").MergeCells = $True
  439.     $Worksheet3.Range("D1:E1").MergeCells = $True
  440.     $Worksheet3.Range("G1:AL1").MergeCells = $True
  441.         # #endregion       
  442.    
  443.         # #region DATA     
  444.     $Row = 0
  445.     $FolderIndex = 0
  446.     $Row++
  447.     $Worksheet3.Cells.Item($Row,1) = "System"
  448.     $Worksheet3.Cells.Item($Row,4) = "Share"
  449.     $Worksheet3.Cells.Item($Row,7) = "NTFS"
  450.     $Row++
  451.     $Worksheet3.Cells.Item($Row,1) = "Path"
  452.     $Worksheet3.Cells.Item($Row,2) = "IdentityReference"
  453.     $Worksheet3.Cells.Item($Row,3) = ""
  454.     $Worksheet3.Cells.Item($Row,4) = "AccessMask"
  455.     $Worksheet3.Cells.Item($Row,5) = "AceType"
  456.     $Worksheet3.Cells.Item($Row,6) = ""
  457.     $Worksheet3.Cells.Item($Row,7) = "GENERIC_READ"
  458.     $Worksheet3.Cells.Item($Row,8) = "GENERIC_WRITE"
  459.     $Worksheet3.Cells.Item($Row,9) = "GENERIC_EXECUTE"
  460.     $Worksheet3.Cells.Item($Row,10) = "GENERIC_ALL"
  461.     $Worksheet3.Cells.Item($Row,11) = "Reserved"
  462.     $Worksheet3.Cells.Item($Row,12) = "Reserved"
  463.     $Worksheet3.Cells.Item($Row,13) = "MAXIMUM_ALLOWED"
  464.     $Worksheet3.Cells.Item($Row,14) = "READ_SETACL"
  465.     $Worksheet3.Cells.Item($Row,15) = "Unused"
  466.     $Worksheet3.Cells.Item($Row,16) = "Unused"
  467.     $Worksheet3.Cells.Item($Row,17) = "Unused"
  468.     $Worksheet3.Cells.Item($Row,18) = "SYNCHRONIZE"
  469.     $Worksheet3.Cells.Item($Row,19) = "WRITE_OWNER"
  470.     $Worksheet3.Cells.Item($Row,20) = "WRITE_DAC"
  471.     $Worksheet3.Cells.Item($Row,21) = "READ_CONTROL"
  472.     $Worksheet3.Cells.Item($Row,22) = "DELETE"
  473.     $Worksheet3.Cells.Item($Row,23) = "Unused"
  474.     $Worksheet3.Cells.Item($Row,24) = "Unused"
  475.     $Worksheet3.Cells.Item($Row,25) = "Unused"
  476.     $Worksheet3.Cells.Item($Row,26) = "Unused"
  477.     $Worksheet3.Cells.Item($Row,27) = "Unused"
  478.     $Worksheet3.Cells.Item($Row,28) = "Unused"
  479.     $Worksheet3.Cells.Item($Row,29) = "Unused"
  480.     $Worksheet3.Cells.Item($Row,30) = "FILE_WRITE_ATTRIBUTES"
  481.     $Worksheet3.Cells.Item($Row,31) = "FILE_READ_ATTRIBUTES"
  482.     $Worksheet3.Cells.Item($Row,32) = "FILE_DELETE_CHILD"
  483.     $Worksheet3.Cells.Item($Row,33) = "FILE_EXECUTE"
  484.     $Worksheet3.Cells.Item($Row,34) = "FILE_WRITE_EA"
  485.     $Worksheet3.Cells.Item($Row,35) = "FILE_READ_EA"
  486.     $Worksheet3.Cells.Item($Row,36) = "FILE_APPEND_DATA"
  487.     $Worksheet3.Cells.Item($Row,37) = "FILE_WRITE_DATA"
  488.     $Worksheet3.Cells.Item($Row,38) = "FILE_READ_DATA"
  489.     $Row++
  490.    
  491.     ForEach ($objOutput in $colOutput)
  492.         {
  493.         $Columns = 38
  494.         $BitsIndex = 1
  495.         $Worksheet3.Cells.Item($Row,1) = "$($objOutput.Path)"
  496.         $Worksheet3.Cells.Item($Row,2) = "$($objOutput.IdentityReference)"
  497.         $Worksheet3.Cells.Item($Row,4) = "$($objOutput.AccessMask)"
  498.         $Worksheet3.Cells.Item($Row,5) = "$($objOutput.AceType)"
  499.        
  500.         Do
  501.             {
  502.             [string]$Bits = [Convert]::ToString($($objOutput.FileSystemRights.value__), 2)
  503.             If ($Bits[-$BitsIndex] -EQ "1") {$Worksheet3.Cells.Item($Row,$Columns) = "X"}
  504.             $Columns--
  505.             $BitsIndex++
  506.             }
  507.         While ($BitsIndex -NE 32)
  508.         $Row++
  509.         }
  510.     Write-Log "    Done!" $LogFile
  511.         # #endregion
  512.    
  513.     # #region OUTPUT
  514.     $Worksheet1.Select()
  515.     Write-Log "Saving data to $OutputFile..." $LogFile
  516.     $Workbook.SaveAs($OutputFile)
  517.     Write-Log "    Done!" $LogFile
  518.     Write-Log "Quitting Excel..." $LogFile
  519.     $Excel.Quit()
  520.     Write-Log "    Done!" $LogFile
  521.     # #endregion
  522.     }
  523. # #endregion
  524.    
  525. # #region MAIN
  526. Write-Log "" $logFile
  527. Write-Log "============================================================" $logFile
  528. Write-Log "$env:userdomain\$env:username logged on from $env:computername." $logFile
  529. Write-Log "Running $ScriptName v$ScriptVersion from $ScriptPath" $logFile
  530. Write-Log "" $LogFile
  531. Use-Culture en-us {Enumerate-Folders;Export-Excel}
  532. #For debugging, run functions outside of Use-Culture on a US-EN configured system
  533. #When running through Use-Culture, any error will point simply to the Use-Culture function
  534. #When run on a non US-EN system without this function, Excel will fail due to bad internalization from Microsoft
  535. #Enumerate-Folders
  536. #Export-Excel
  537. Write-Log "" $logFile
  538. Write-Log "============================================================" $logFile
  539. # #endregion
  540. #EOF
  541.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement