List folders and files in a UNC root folder

让人想犯罪 __ 提交于 2019-12-23 02:53:22

问题


I am trying to get folders and files from the root of a UNC path name. I am using Get-ChildItem.

I can get results from a subfolder via the UNC path, but not the root folder. If run the command Get-ChildItem \\sf1\user1 from the command line, results are returned.

    Directory: \\sf1\user1


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        11/14/2013   3:40 PM            1.ISGROUP

When I try to execute Get-ChildItem \\sf1 from the command line I get errors.

Get-ChildItem : Cannot find path '\\sf1' because it does not exist.
At line:1 char:1
+ Get-ChildItem \\sf1
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\sf1:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Any suggestions on how to get folders and files from the root of a UNC path?


回答1:


As Mathias pointed out, a valid UNC path consists of at least server and share (\\servername\sharename), optionally subfolders too (\\servername\sharename\sub\folder). You cannot list files/folders directly from a server. If you want to enumerate shares on the remote server use net view \\servername (as Anthony Stringer mentioned). Note that this will list only visible shares. If you also want to list hidden shares (shares whose name ends with a $) you need to run net share on the server

Invoke-Command -Computer servername -ScriptBlock { & net share }

or use WMI

Get-WmiObject -Computer servername -Class Win32_Share


来源:https://stackoverflow.com/questions/35068220/list-folders-and-files-in-a-unc-root-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!