jeek_

Get-RemoteFolderAcl

Sep 6th, 2023 (edited)
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.55 KB | Source Code | 0 0
  1. function Get-RemoteFolderAcl {
  2.     <#
  3.     .SYNOPSIS
  4.     Returns to ACL of the specified folder from the specified computer
  5.  
  6.     .DESCRIPTION
  7.     Returns to ACL of the specified folder from the specified computer
  8.  
  9.     .PARAMETER ComputerName
  10.     The name of the computer
  11.  
  12.     .PARAMETER Path
  13.     The folder path
  14.  
  15.     .PARAMETER Credential
  16.     Credentials of the remote computer
  17.  
  18.     .EXAMPLE
  19.     $cred = Get-Credential
  20.     $folder = @(
  21.         'C:\temp\folder1'
  22.         'C:\temp\folder2'
  23.     )
  24.     $acls = Get-RemoteFolderAcl -computerName Computer1 -path $folder -Credential $cred
  25.     $acls | Select PSComputerName, Path, Identity, AccessType, AccessRights | ft
  26.  
  27.     .NOTES
  28.    
  29.     #>
  30.     [CmdletBinding()]
  31.     param (
  32.         [Parameter(Mandatory,ValueFromPipeline, ValueFromPipelineByPropertyName)]
  33.         [Alias('PSComputerName')]
  34.         [String[]]$computerName,
  35.  
  36.         [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
  37.         [alias('Fullname','Folder')]
  38.         [String[]]$path,
  39.  
  40.         [PSCredential]$Credential
  41.     )
  42.  
  43.     begin {
  44.         $getAcl = {
  45.             param (
  46.                 [String[]]$path
  47.             )
  48.             # permissions hash map
  49.             $permissions = @{
  50.                 '268435456' = 'FullControl'
  51.                 '-536805376' = 'Modify, Synchronize'
  52.                 '-1610612736' = 'ReadAndExecute, Synchronize'
  53.             }
  54.  
  55.             foreach ($filePath in $path) {
  56.                 if (Test-Path $filePath) {
  57.                     try {
  58.                         Get-Acl $filePath | ForEach-Object {
  59.                             foreach ($access in $_.Access) {
  60.                                 if ($access.FileSystemRights -match '\d') {
  61.                                     $fsr = $access.FileSystemRights.ToString()
  62.                                     $fileSystemRights = $($permissions[$fsr])
  63.                                 }
  64.                                 else {
  65.                                     $fileSystemRights = $access.FileSystemRights
  66.                                 }
  67.                                 [PSCustomObject]@{
  68.                                     Path              = (($_.Path) -split ("::"))[-1]
  69.                                     FileSystemRights  = $fileSystemRights
  70.                                     AccessControlType = $access.AccessControlType
  71.                                     IdentityReference = $access.IdentityReference
  72.                                     IsInherited       = $access.IsInherited
  73.                                     InheritanceFlags  = $access.InheritanceFlags
  74.                                     PropagationFlags  = $access.PropagationFlags
  75.                                 }
  76.                             }
  77.                         }
  78.                     }
  79.                     catch {
  80.                         $_.Exception.Message
  81.                     }
  82.                 }
  83.                 else {
  84.                     Write-Warning "Computer: [$env:ComputerName] - Folder: [$filePath] doesn't exist"
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     process {
  91.         $params = @{
  92.             ComputerName = $computer
  93.             # VMName = $computerName
  94.             ScriptBlock = $getAcl
  95.             ArgumentList = (,$path)
  96.         }
  97.         if ($Credential) {
  98.             $params.Add('Credential', $credential)
  99.         }
  100.         Invoke-Command @params
  101.         # Invoke-Command -VMName $computer -ScriptBlock $getAcl -ArgumentList (,$path)
  102.         # Invoke-Command -ComputerName $computer -ScriptBlock $getAcl -ArgumentList (,$path)
  103.     }
  104.  
  105.     end {
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment