Shared Folder ACL remotely

Welcome Forums General PowerShell Q&A Shared Folder ACL remotely

Viewing 2 reply threads
  • Author
    Posts
    • #195131
      Participant
      Topics: 1
      Replies: 1
      Points: 17
      Rank: Member

      Hi I am trying to get shared folder reports from multiple servers remotely, I am able to get the reports using below code, but the thing is if a folder is shared with 3 user account then i am getting those detail in 3 separate line , because of get-smbshareaccess , need help in joining those details in single line seperated by any delimiter

      $cred = Get-Credential
      
      
      $result = Invoke-Command -ComputerName 'ESHWAR' -ScriptBlock {
      Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) }|
      ForEach-Object { Get-SmbShareAccess -Name $_.Name } | Sort-Object Name
      
      } -Credential $cred | Select-Object -Property @{ Label="Server_Name";Expression={($_.PSComputerName).Tostring()}},
      @{Label = "Share_Name";Expression={($_.Name).ToString()}},
      @{Label = "User_Name";Expression={($_.AccountName).ToString()}},
      @{Label = "Rights";Expression={($_.AccessRight).ToString()}} | Export-Csv -Path report.csv -Append -NoTypeInformation

       

    • #195149
      Moderator
      Topics: 3
      Replies: 169
      Points: 891
      Helping Hand
      Rank: Major Contributor

      Hi Eshwar,

      I just tweaked your code a bit, and now give it a try...

      $Cred = Get-Credential
      
      $ScriptBlock = [scriptblock]::Create({
          Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) } | ForEach-Object { Get-SmbShareAccess -Name $_.Name } 
      })
      
      $Result = Invoke-Command -ComputerName 'ESHWAR' -ScriptBlock $ScriptBlock -Credential $Cred
      
      $UniqueInResult = $Result | Select-Object -Property PSComputerName, Name, AccessRight -Unique
      
      $Output = @()
      
      $Output += ForEach ($Item in $UniqueInResult)
      {
          $UserName   = ($Result | Where-Object {$_.Name -eq $Item.Name -and $_.PSComputerName -eq $Item.PSComputerName -and $_.AccessRight -eq $Item.AccessRight}).AccountName -join ';'
          New-Object -TypeName psobject -Property @{
              Server_Name = $Item.PSComputerName
              Share_Name  = $Item.Name
              User_Name   = $UserName
              Rights  = $Item.AccessRight
          }
      }
      
      $Output | Select-Object -Property Server_Name, Share_Name, User_Name, Rights | Export-Csv -Path report.csv -Append -NoTypeInformation

      Thank you.

    • #195155
      Participant
      Topics: 1
      Replies: 1
      Points: 17
      Rank: Member

      Hi Kiran,

      Thanks a lot man, really helped me a lot, i got the desired output but i did some little tweak to the code you provided.

      I had 3 folders , Initially from your code for 1st folder it was good, but for the 2nd and 3rd, it again went to next line, so removed AcessRights evaluaion from foreach loop, because for some reason O/P for Access rights was coming in numerical format and rights was not mandatory for me now.

      After that got some duplicate lines so used unique parameter in last select-object just before passing to the files

      $Cred = Get-Credential
      
      $ScriptBlock = [scriptblock]::Create({
      Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) } | ForEach-Object { Get-SmbShareAccess -Name $_.Name } 
      })
      
      $Result = Invoke-Command -ComputerName 'ESHWAR' -ScriptBlock $ScriptBlock -Credential $Cred
      
      $UniqueInResult = $Result | Select-Object -Property PSComputerName, Name -Unique
      
      $Output = @()
      
      $Output += ForEach ($Item in $UniqueInResult)
      {
      $UserName = ($Result | Where-Object {$_.Name -eq $Item.Name -and $_.PSComputerName -eq $Item.PSComputerName }).AccountName -join ';'
      New-Object -TypeName psobject -Property @{
      Server_Name = $Item.PSComputerName
      Share_Name = $Item.Name
      User_Name = $UserName
      
      }
      }
      
      $Output | Select-Object * -Unique | Export-Csv -Path report.csv -Append -NoTypeInformation
      
      
      
      

      Thanks again

       

Viewing 2 reply threads
  • You must be logged in to reply to this topic.