Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param(
- [parameter(ValueFromPipeline=$true)]
- [string[]]$ComputerName = "localhost",
- [string]$file,
- [string]$outfile,
- [Byte]$ToDepth = 2,
- [switch]$includeInherited
- )
- $stopwatch = [system.diagnostics.stopwatch]::StartNew()
- Function Get-ACLToDepth {
- Param(
- [String]$Path = $PWD,
- [Byte]$ToDepth = 2,
- [string]$ComputerName
- )
- $results = @()
- $Directories = @()
- $Directories = @(Get-Item -Path $Path)
- $Directories += @(Get-ChildItem -Path $Path -Depth ($ToDepth -1) -Directory | Sort-Object FullName)
- Foreach($Directory in $Directories){
- $depth = [regex]::matches($Directory.FullName,"\\").count - [regex]::matches($Path,"\\").count
- $Directory | Get-ACL | ForEach-Object {
- $IsInherited = $_.Access.IsInherited | Sort-Object -unique
- $aclObj = [PSCustomObject]@{
- Computer = $computer
- Share = $Share.Name
- FullSharePath = "\\" + $computer + "\" + $Share.Name + $Directory.FullName.replace($Path,"")
- RelativePath = $Directory.FullName.replace($Path,"")
- LocalPath = $Directory.FullName
- ParentPath = $Directory.PSParentPath.replace("Microsoft.PowerShell.Core\FileSystem::","")
- Depth = $depth
- Characters = $($Directory.FullName.replace($Path,"")).Length
- Owner = $_.Owner
- IsInherited = $IsInherited
- }
- $accessGroup = $_.access | Group-Object FileSystemRights
- Foreach($access in $accessGroup.Name){
- $aclObj | Add-Member -type NoteProperty -Name $access.replace(", Synchronize","") -Value ($accessGroup | ? {$_.name -eq $access} | % {$_.group.identityreference})
- }
- ### CreateHash
- $aclHash = @{
- $Directory.FullName = @{
- Computer = $computer
- Share = $Share.Name
- FullSharePath = "\\" + $computer + "\" + $Share.Name + $Directory.FullName.replace($Path,"")
- RelativePath = $Directory.FullName.replace($Path,"")
- LocalPath = $Directory.FullName
- ParentPath = $Directory.PSParentPath.replace("Microsoft.PowerShell.Core\FileSystem::","")
- Depth = $depth
- Characters = $($Directory.FullName.replace($Path,"")).Length
- Owner = $_.Owner
- IsInherited = $IsInherited
- }
- }
- $accessGroup | ForEach-Object {
- $aclHash[$Directory.FullName] += @{
- $_.Name.replace(", Synchronize","") = $_.Group.IdentityReference.Value -join ','
- }
- }
- }
- $resultsHash += $aclHash
- $resultsObj += @($aclObj)
- }
- Return $resultsObj , $resultsHash
- }
- $FunctionDefs = "Function Get-ACLToDepth { ${function:Get-ACLToDepth} }"
- $Credential = Get-Credential
- If($file){ $Computers= Get-Content $file }
- Else{ $Computers = $ComputerName }
- Foreach($computer in $Computers)
- {
- $OutputObject = @()
- $OutputHash = @{}
- try {
- $OutputObject, $OutputHash = Invoke-Command -Credential $Credential -Computer $computer -ArgumentList $FunctionDefs, $ToDepth -ScriptBlock {
- param ($FunctionDefs,$ToDepth)
- . ([ScriptBlock]::Create($FunctionDefs))
- $Computer = Hostname
- $Shares = Get-WmiObject -Class Win32_Share -ErrorAction Stop | Where-Object { $_.Description -eq "$null" }
- Foreach($Share in $Shares) {
- Get-ACLToDepth -Path $($Share.Path) -ToDepth $ToDepth
- }
- }
- $Output += $OutputObject
- $Hash += $OutputHash
- }
- catch {
- Write-Error "Failed to connect to $computer"
- }
- }
- switch ($includeInherited){
- $true {
- $Output = $Output
- $Hash = $Hash
- }
- $false {
- $Output = $Output | Where-Object { $_.IsInherited -eq $false -Or $_.Depth -eq 0 }
- $Hash = $Hash.GetEnumerator() | Where-Object { $_.Value.IsInherited -eq $false -Or $_.Value.Depth -eq 0 }
- }
- }
- switch ($outfile.Split(".")[-1]){
- "xml" {
- $Hash | Export-CliXML $outfile
- }
- "csv" {
- $Output | Export-CSV $outfile -NoTypeInformation
- }
- Default {
- $Output
- $Output | measure-object | Select-Object Count
- }
- }
- $stopwatch | Select-Object ElapsedMilliseconds
Add Comment
Please, Sign In to add comment