PowerShell 获取共享文件夹列表

The*_*Woo 24 powershell shared-folders powershell-3.0

我正在尝试获取在文件共享上共享的文件夹列表。目前我有两个测试文件夹:

\\MYPC\Test1

\\MYPC\Test2
Run Code Online (Sandbox Code Playgroud)

这是我目前的代码:

$FileServer = Read-Host "Enter file server to search"
$FolderList = Get-ChildItem -Path $FileServer

Write-Host $FolderList
Run Code Online (Sandbox Code Playgroud)

But this comes up with "cannot find the path". I can see examples of how to do this for \\Server\Share as the directory, but is it possible to just search the \\Server?

Tam*_*erz 25

Try this:

get-WmiObject -class Win32_Share -computer dc1.krypted.com
Run Code Online (Sandbox Code Playgroud)

Ref: List Shares in Windows w/ PowerShell

  • 这将需要目标计算机上的 WMI 权限,这不是一个特别便携的解决方案。 (6认同)
  • 此外,它需要 RPC 通信,即使在允许通用 SMB 的情况下,也可能在许多配置中设置防火墙。诚然,`net view` 不会返回隐藏的共享。 (5认同)

Mar*_*son 19

There's only one way of enumerating shares remotely from the command line that I know of, and thats with net view:

C:\Users\mark.henderson>net view \\enetsqnap01
Shared resources at \\enetsqnap01



Share name             Type  Used as  Comment

-------------------------------------------------------------------------------
Backups                Disk
CallRecordings         Disk
Download               Disk           System default share
home                   Disk           Home
homes                  Disk           System default share
Installs               Disk
Justin                 Disk           Copy of files from Justin laptop
michael                Disk
Multimedia             Disk           System default share
Network Recycle Bin 1  Disk           [RAID5 Disk Volume: Drive 1 2 3 4]
Public                 Disk           System default share
Qsync                  Disk           Qsync
Recordings             Disk           System default share
Sales                  Disk           Sales Documents
SalesMechanix          Disk
Server2012             Disk           Windows Server 2012 Install Media
Usb                    Disk           System default share
VMWareTemplates        Disk
Web                    Disk           System default share
The command completed successfully.
Run Code Online (Sandbox Code Playgroud)

This is not particularly parsable on its own, but, you can throw it into an array to process the data line by line:

$sharedFolders = (NET.EXE VIEW \\enetsqnap01) 
Run Code Online (Sandbox Code Playgroud)

您现在拥有一个数组,并从$sharedFolders[7]您拥有共享开始。然后split,您可以使用双空格之类的东西 - 不太可能出现在共享名称本身中,除非您的共享名称很长,否则应该可以工作,只在共享名称和类型字段之间留下一个空格:

$sharedFolders[7].split('  ')[0]
Backups
Run Code Online (Sandbox Code Playgroud)

您可以使用 ForEach 和一些条件逻辑来处理这些。它不会是完美的,但它应该适用于大多数用例。

为简洁起见,只需将文件名输出到控制台:

(net view \\enetsqnap01) | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split('  ')[0] } }
Run Code Online (Sandbox Code Playgroud)


Kla*_*urn 13

如果你想找到本地机器的共享,你可以这样做Get-SmbShare

> Get-SmbShare

Name                          ScopeName                     Path                          Description
----                          ---------                     ----                          -----------
ADMIN$                        *                             C:\WINDOWS                    Remote Admin
C$                            *                             C:\                           Default share
Run Code Online (Sandbox Code Playgroud)