Guest User

Get-SharePermissions.ps1

a guest
Mar 12th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Param(
  2.     [parameter(ValueFromPipeline=$true)]
  3.     [string[]]$ComputerName = "localhost",
  4.     [string]$file,
  5.     [string]$outfile,
  6.     [Byte]$ToDepth = 2,
  7.     [switch]$includeInherited  
  8. )
  9. $stopwatch =  [system.diagnostics.stopwatch]::StartNew()
  10.  
  11. Function Get-ACLToDepth {
  12.     Param(
  13.         [String]$Path = $PWD,
  14.         [Byte]$ToDepth = 2,
  15.         [string]$ComputerName
  16.     )
  17.     $results = @()
  18.     $Directories = @()
  19.     $Directories = @(Get-Item -Path $Path)
  20.     $Directories += @(Get-ChildItem -Path $Path -Depth ($ToDepth -1) -Directory | Sort-Object FullName)
  21.     Foreach($Directory in $Directories){
  22.         $depth = [regex]::matches($Directory.FullName,"\\").count - [regex]::matches($Path,"\\").count
  23.         $Directory | Get-ACL | ForEach-Object {
  24.             $IsInherited = $_.Access.IsInherited | Sort-Object -unique
  25.             $aclObj = [PSCustomObject]@{
  26.                 Computer = $computer
  27.                 Share = $Share.Name
  28.                 FullSharePath = "\\" + $computer + "\" + $Share.Name + $Directory.FullName.replace($Path,"")
  29.                 RelativePath = $Directory.FullName.replace($Path,"")
  30.                 LocalPath = $Directory.FullName
  31.                 ParentPath = $Directory.PSParentPath.replace("Microsoft.PowerShell.Core\FileSystem::","")
  32.                 Depth = $depth
  33.                 Characters = $($Directory.FullName.replace($Path,"")).Length
  34.                 Owner = $_.Owner
  35.                 IsInherited = $IsInherited
  36.             }
  37.             $accessGroup = $_.access | Group-Object FileSystemRights
  38.             Foreach($access in $accessGroup.Name){
  39.                 $aclObj | Add-Member -type NoteProperty -Name $access.replace(", Synchronize","") -Value ($accessGroup | ? {$_.name -eq $access} | % {$_.group.identityreference})
  40.             }
  41.             ###  CreateHash
  42.             $aclHash = @{
  43.                 $Directory.FullName = @{
  44.                     Computer = $computer
  45.                     Share = $Share.Name
  46.                     FullSharePath = "\\" + $computer + "\" + $Share.Name + $Directory.FullName.replace($Path,"")
  47.                     RelativePath = $Directory.FullName.replace($Path,"")
  48.                     LocalPath = $Directory.FullName
  49.                     ParentPath = $Directory.PSParentPath.replace("Microsoft.PowerShell.Core\FileSystem::","")
  50.                     Depth = $depth
  51.                     Characters = $($Directory.FullName.replace($Path,"")).Length
  52.                     Owner = $_.Owner
  53.                     IsInherited = $IsInherited
  54.                 }
  55.             }
  56.             $accessGroup | ForEach-Object {
  57.                 $aclHash[$Directory.FullName] += @{
  58.                     $_.Name.replace(", Synchronize","") = $_.Group.IdentityReference.Value -join ','
  59.                 }
  60.             }
  61.         }
  62.         $resultsHash += $aclHash
  63.         $resultsObj += @($aclObj)
  64.     }
  65.     Return $resultsObj , $resultsHash
  66. }
  67.  
  68. $FunctionDefs = "Function Get-ACLToDepth { ${function:Get-ACLToDepth} }"
  69. $Credential = Get-Credential
  70. If($file){ $Computers= Get-Content $file }
  71. Else{ $Computers = $ComputerName }
  72.  
  73. Foreach($computer in $Computers)
  74. {
  75.     $OutputObject = @()
  76.     $OutputHash = @{}
  77.     try {
  78.         $OutputObject, $OutputHash = Invoke-Command -Credential $Credential -Computer $computer -ArgumentList $FunctionDefs, $ToDepth -ScriptBlock {
  79.             param ($FunctionDefs,$ToDepth)
  80.             . ([ScriptBlock]::Create($FunctionDefs))
  81.             $Computer = Hostname
  82.             $Shares = Get-WmiObject -Class Win32_Share -ErrorAction Stop | Where-Object { $_.Description -eq "$null" }
  83.             Foreach($Share in $Shares) {
  84.                 Get-ACLToDepth -Path $($Share.Path) -ToDepth $ToDepth
  85.             }
  86.         }
  87.         $Output += $OutputObject
  88.         $Hash += $OutputHash
  89.     }
  90.     catch {
  91.         Write-Error "Failed to connect to $computer"
  92.     }
  93. }
  94.  
  95. switch ($includeInherited){
  96.     $true {
  97.         $Output = $Output
  98.         $Hash = $Hash
  99.     }
  100.     $false {
  101.         $Output = $Output | Where-Object { $_.IsInherited -eq $false -Or $_.Depth -eq 0 }
  102.         $Hash = $Hash.GetEnumerator() | Where-Object { $_.Value.IsInherited -eq $false -Or $_.Value.Depth -eq 0 }
  103.     }
  104. }
  105.  
  106. switch ($outfile.Split(".")[-1]){
  107.     "xml" {
  108.         $Hash | Export-CliXML $outfile
  109.     }
  110.     "csv" {
  111.         $Output | Export-CSV $outfile -NoTypeInformation
  112.     }
  113.     Default {
  114.         $Output
  115.         $Output | measure-object | Select-Object Count
  116.     }
  117. }
  118.  
  119. $stopwatch | Select-Object ElapsedMilliseconds
Add Comment
Please, Sign In to add comment