locked
Getting shared folders reports from multiple remote servers RRS feed

  • Question

  • Hi I am trying to get shared Folder list along with ACL from multiple remote servers, Below code gets the report but not in the desired format
    For Ex:- if a Folder is shared with 3 user , i am getting the ACL information in 3 multiple line, so how we avoid it and get it in a single line with any delimiter separating it


    $cred = Get-Credential
    $result = Invoke-Command -ComputerName 'server1' -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
     
     
     Getting output as below
     
     Server_Name--Share_Name---User_Name
    server1 -----test1----- Everyone
    server1 -----test1----BUILTIN\Administrators


    Desired Output
    Server_Name--Share_Name---User_Name
    server1 -----test1----- Everyone,BUILTIN\Administrators

    Wednesday, December 25, 2019 5:10 PM

All replies

  • Please read the following and edit your original post:


    \_(ツ)_/

    Wednesday, December 25, 2019 7:24 PM
  • Here is how to get remote share information.

    $server = 'myserver'
    Get-SmbShare -CimSession $server -Special $true|
        Get-SmbShareAccess |
    Sort Name | Select PsCOmputerName, Name, AccountName,AccessRight | Export-Csv report.csv


    \_(ツ)_/



    • Edited by jrv Wednesday, December 25, 2019 7:40 PM
    Wednesday, December 25, 2019 7:38 PM
  • Thanks for the response but using my code posted in initial post itself i got the output, but not in the desired format which  i was expecting, since duplicate items were there.

    Thanks to a friend I got the proper working code which gives a desired output without duplication

    $ScriptBlock = [scriptblock]::Create({
        Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) } | ForEach-Object { Get-SmbShareAccess -Name $_.Name } 
    })
    
    $Result = Invoke-Command -ComputerName 'server1' -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 -Property User_Name,Share_Name,Server_Name -Unique | Export-Csv -Path report.csv -Append -NoTypeInformation


    Wednesday, December 25, 2019 7:53 PM
  • Please fix your original post so others can read it correctly,'

    \_(ツ)_/

    Wednesday, December 25, 2019 8:05 PM
  • Your original code has too many issues that are creating problems. The code I posted will give you the desired output.  Have you tried it?


    \_(ツ)_/

    Wednesday, December 25, 2019 8:06 PM
  • The SMB CmdLets are designed to do their own remoting.

    If you need to use credentials then this is how to do that:

    $server = 'myserver'
    $cimsession = New-CimSession -ServerName $server -Credential $cred
    Get-SmbShare -CimSession $cimsession |
        Get-SmbShareAccess |
        Select PsCOmputerName, Name, AccountName,AccessRight |
        Export-Csv report.csv

    Part of the problem is that you are copying code that you do not understand and that is unnecessary,

    Until you have a more complete and correct understanding of PowerShell you need to stick to simple steps.  Copying poorly written code will only add to your confusion.


    \_(ツ)_/


    Wednesday, December 25, 2019 8:12 PM
  • If you really insist on renaming the properties this is how to do it in a controlled way.

    $server = 'myserver'
    $properties = @(
        @{n='Server_Name';e={$_.PSComputerName}},
        @{n='Share_Name';e={$_.Name}},
        @{n='User_Name';e={$_.AccountName}},
        @{n='Rights';e={$_.AccessRight}}
    )
    $cimsession = New-CimSession -ServerName $server -Credential $cred
    Get-SmbShare -CimSession $cimsession |
        Get-SmbShareAccess |
        Select $properties |
        Export-Csv report.csv
    


    \_(ツ)_/

    Wednesday, December 25, 2019 8:19 PM