0

The local network contains a few file server that each has multiple shares.

I would like to get a complete recursive directory listing for a given host. Unfortunately dir /S /B \\servername does not work.

I would have to call dir /S /B \\servername\share for each share separately.

Is there a more easy way?

Also the process should not stop or abort if directory has no access rights.

2
  • You can start by looking at this question.
    – user1018743
    Nov 13, 2019 at 12:49
  • thank you. that link helps. however it seems it is not feasable to do without net view and result parsing Nov 14, 2019 at 9:24

3 Answers 3

0

This PowerShell one-liner will list all the shares of the specified computer, and for each share will list all its files:

foreach($Share in Get-WmiObject Win32_Share -ComputerName 'COMPUTER') {Get-ChildItem "\\$($Share.__SERVER)\$($Share.Name)"}

You may all invoke it from a Command Prompt (cmd) or batch (.bat) as:

powershell "foreach($Share in Get-WmiObject Win32_Share -ComputerName 'COMPUTER') {Get-ChildItem "\\$($Share.__SERVER)\$($Share.Name)"}" >\path\outfile.txt

EDIT

For querying a non-Windows server, the problem is that the above requires an RPC server which is not available.

You might try using WMIC instead. Enter in the PowerShell command prompt the following two lines for one such Linux server:

$allshares = (net view \\SERVER) | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split('  ')[0] }}
foreach ($Share in $allshares) {Get-ChildItem "\\SERVER\\$Share"}
5
  • 1
    that does not work. this gives error that there is no RPC server that can execute wmi-requests (the server are either simple NAS or some other linux based file servers). it seems that there is no way around net view server and parsing the result with some script language Nov 14, 2019 at 9:22
  • It worked for me. Perhaps the difference is that a firewall is blocking your RPC. The ports are 135 and dynamic ports above 1024. Perhaps this link might be useful for troubleshooting.
    – harrymc
    Nov 14, 2019 at 9:29
  • since some are not a windows machine they do not have a RPC service that answe WMI-Reqests Nov 14, 2019 at 10:01
  • I added an alternative syntax that uses WMIC instead. Try to see if it works better.
    – harrymc
    Nov 14, 2019 at 10:47
  • I hoped that there would a simple command. I knew the net view approach but it has a lot of gotchas: net view is localized and its output depends on the language settings. in German Windows eg. Disk is translated Plattte. My implemented approach is looking for the delimiter line, go two up, look at which character the second column starts and how long the line is and then checking in each following line if the the line length matches and the last character of the first column is a space because splitting at Space is also not possible (white spaces in share names) Nov 14, 2019 at 13:28
-1

through explorer open the folder containing the shares. Hold shift and right click. Choose open powershell or cmd here and type: dir > textfile.txt

1
  • this would create a file on the remote server that may not even be possible. but beside from this, this is exactly that i suggested in the "I would have to call...." sentence Nov 13, 2019 at 13:34
-1

Create a text file called C:\Folders.txt

Run this in Powershell :

Get-ChildItem "\\192.168.2.4\Home" -Recurse -Name -Directory | Out-File "C:\Folders.txt"
1
  • but this already accessed the share Home. my question is about only passing a host name. your answer is identical to my dir /AD /S /B \\somehost\Home (and i do not even want the /AD) Nov 14, 2019 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .