Search code examples
powershellget-childitemnetwork-shares

Using Get-Childitem at root of UNC Path \\SERVERNAME


We're trying to use PowerShell as a file-replication tool, but having an issue.

The source server is a Linux-based closed OS, where users, through a GUI, create multiple network shares, which are then accessible by the UNC path \\SERVERNAME\NETWORKSHARE. In the OS, of course, the NETWORKSHARE is actually located multiple levels deeper than the root.

We've talked to the developer of the OS, and they cannot/will not enable access to the root of the folder where all the NETWORKSHARE folders are stored.

I've turned to PowerShell to try and find a way. My initial thinking was to send a Get-ChildItem -Directory to \\SERVERNAME to grab all the NETWORKSHARES, and then pipe that to Copy-Item/Robocopy, but it doesn't work.

I know the server needs their own local (non-AD) credentials.

Is there a way to do what I want to do that I'm just not seeing ?

Thanks in advance!


Solution

  • The net view command with an IP will enumerate accessible SMB/Samba shares.
    This PowerShell script parses the output and returns a [PSCustomObject] with Server and Share properties.

    ## Enum-SambaShares.ps1
    $ServerList = ('192.168.133.67','192.168.133.46','192.168.133.76','192.168.133.28')
    $ServerShares = ForEach ($Server in $ServerList) {
        net view "\\$Server" 2>$Null | Select-String '^([^ ]+)\s+(Disk|Platte)' | 
            ForEach-Object {
                [PSCustomObject]@{
                    Server = $Server
                    Share  = $_.matches.groups[1].Value
                }
            }
    }
    $ServerShares
    

    Sample output:

    Server        Share
    ------        -----
    192.168.133.67 Backup
    192.168.133.67 Daten
    192.168.133.67 Video-Unc
    192.168.133.46 SambaShare
    192.168.133.46 UserName
    192.168.133.76 C
    192.168.133.28 McDaten
    192.168.133.28 Music
    192.168.133.28 UserName