列出UNC根文件夹中的文件夹和文件

时间:2016-01-28 17:36:35

标签: powershell directory subdirectory unc

我正在尝试从UNC路径名的根目录中获取文件夹和文件。我正在使用Get-ChildItem

我可以通过UNC路径从子文件夹获取结果,但不能从根文件夹获取结果。如果从命令行运行命令Get-ChildItem \\sf1\user1,则返回结果。

    Directory: \\sf1\user1


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

当我尝试从命令行执行Get-ChildItem \\sf1时,我收到错误。

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

有关如何从UNC路径的根目录获取文件夹和文件的任何建议吗?

1 个答案:

答案 0 :(得分:4)

正如Mathias指出的那样,有效的UNC路径至少包含服务器和共享(\\servername\sharename),也可以包含子文件夹(\\servername\sharename\sub\folder)。您无法直接从服务器列出文件/文件夹。如果要枚举远程服务器上的共享,请使用net view \\servername(如Anthony Stringer所述)。请注意,这将仅列出可见共享。如果您还想列出隐藏的共享(名称以$结尾的共享),您需要在服务器上运行net share

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

或使用WMI

Get-WmiObject -Computer servername -Class Win32_Share
相关问题