PowerShell获取共享文件夹列表


17

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

\\MYPC\Test1

\\MYPC\Test2

这是我目前的代码:

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

Write-Host $FolderList

但这就出现了“无法找到路径”。我可以看到如何\\Server\Share为目录执行此操作的示例,但是可以只搜索\\Server

Answers:



14

从我所知道的命令行远程枚举共享只有一种方法,那就是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.

这本身并不是特别可解析的,但是,您可以将其放入数组中以逐行处理数据:

$sharedFolders = (NET.EXE VIEW \\enetsqnap01) 

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

$sharedFolders[7].split('  ')[0]
Backups

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

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

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

仅供参考:我添加了一个辅助函数来包装调用并以半智能的方式分解文本输出...希望它有意义/帮助一些人在那里。
JohnLBevan 2016年

1
@JohnLBevan我在这里看不到。编辑可能被拒绝了吗?如果你再次提交,我会看到我是否可以及时查看它,然后才能有其他人访问它。
Mark Henderson

谢谢@Mark Henderson。从评论说明(superuser.com/review/suggested-edits/535793)看来,人们似乎更愿意将我的代码发布在单独的答案中,所以我发布在这里:superuser.com/a/1079174/156700。希望这对其他人有用。再次感谢您的解决方案。
JohnLBevan 2016年

7

如果要查找本地计算机的共享,您可以执行以下操作Get-SmbShare

> Get-SmbShare

Name                          ScopeName                     Path                          Description
----                          ---------                     ----                          -----------
ADMIN$                        *                             C:\WINDOWS                    Remote Admin
C$                            *                             C:\                           Default share

3

扩展马克亨德森的答案

$Servers = ( Get-ADComputer -Filter { DNSHostName -Like '*' }  | Select -Expand Name )
foreach ($Server in $Servers)
{
    (net view $Server) | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split('      ')[0] } } | out-file C:\file_shares\$Server.txt
}

2
你能解释一下你的扩展吗?
bertieb 2015年

3

感谢Mark Henderson 的解决方案。我添加了一个包装函数来帮助使这个函数调用更加PowerShell友好。我使用了一种不同的方法来分解数据(更复杂,更好); 可以根据偏好轻松切换。

clear-host
function Get-SharedFolder {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$ComputerName 
        ,
        [Parameter(Mandatory = $false)]
        [switch]$GetItem
        ,
        [Parameter(Mandatory = $false)]
        [string[]]$ColumnHeadings = @('Share name','Type','Used as','Comment')  #I suspect these differ depending on OS language?  Therefore made customisable
        ,
        [Parameter(Mandatory = $false)]
        [string]$ShareName = 'Share name' #tell us which of the properties relates to the share name
        #,
        #[Parameter(Mandatory = $false)]
        #[string[]]$Types = @('Disk') # again, likely differs with language.  Also there may be other types to include?
    )
    begin {
        [psobject[]]$Splitter = $ColumnHeadings | %{
            $ColumnHeading = $_
            $obj = new-object -TypeName PSObject -Property @{
                Name = $ColumnHeading
                StartIndex = 0
                Length = 0
            }
            $obj | Add-Member -Name Initialise -MemberType ScriptMethod {
                param([string]$header)
                process {
                    $_.StartIndex = $header.indexOf($_.Name)
                    $_.Length = ($header -replace ".*($($_.Name)\s*).*",'$1').Length
                }
            }
            $obj | Add-Member -Name GetValue -MemberType ScriptMethod {
                param([string]$line)
                process {
                    $line -replace ".{$($_.StartIndex)}(.{$($_.Length)}).*",'$1'
                }
            }
            $obj | Add-Member -Name Process -MemberType ScriptMethod {
                param([psobject]$obj,[string]$line)
                process {
                    $obj | Add-Member -Name $_.Name -MemberType NoteProperty -Value ($_.GetValue($line))
                }
            }
            $obj
        }
    }
    process {
        [string[]]$output = (NET.EXE VIEW $ComputerName)
        [string]$headers = $output[4] #find the data's heading row
        $output = $output[7..($output.Length-3)] #keep only the data rows
        $Splitter | %{$_.Initialise($headers)}
        foreach($line in $output) { 
            [psobject]$result = new-object -TypeName PSObject -Property @{ComputerName=$ComputerName;}
            $Splitter | %{$_.Process($result,$line)}
            $result | Add-Member '_ShareNameColumnName' -MemberType NoteProperty -Value $ShareName
            $result | Add-Member 'Path' -MemberType ScriptProperty -Value {("\\{0}\{1}" -f $this.ComputerName,$this."$($this._ShareNameColumnName)")}
            $result | Add-Member 'Item' -MemberType ScriptProperty -Value {Get-Item ($this.Path)}
            $result | Add-Member -MemberType MemberSet -Name PSStandardMembers -Value ([System.Management.Automation.PSMemberInfo[]]@(New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]](@('ComputerName','Path') + $ColumnHeadings))))
            $result
        }
    }
}

[string[]]$myServers = 'myServer1','myServer2' #amend this line to get the servers you're interested in
[psobject[]]$shares = $myServers | Get-SharedFolder
write-host 'List of Shares' -ForegroundColor Cyan
$shares  | ft -AutoSize
write-host 'Shares as Get-Item output' -ForegroundColor Cyan
$shares  | select -expand Item

0

Windows资源工具包工具:rmtshare

在远程服务器上以具有管理员权限的id运行,或者与远程服务器建立ipc $连接。

rmtshare \\servername

您能否进一步扩展您的答案以包括解决问题所需的步骤?
Cfinley 2015年

0

在Windows 8或更高版本以及Windows Server 2012或更高版本上,您可以使用SmbShare模块中的Get-SmbShare


0

这是一个PowerShell one liner,它使用net view来枚举用户可以看到的所有远程共享 - 并不意味着他们可以访问。

net view | Where {$_ -like "\\*"} | %{$comp = $_.Split(" ")[0]; net view $comp | Where {$_ -like "*Disk*"} | %{$share = $_.Split(" ")[0]; $fullpath = Join-Path $comp $share; $fullpath}}

如果您想查看他们是否(至少)具有读访问权限,您可以运行:

Net view | Where {$_ -like "\\*"} | %{$comp = $_.Split(" ")[0]; net view $comp | Where {$_ -like "*Disk*"} | %{$share = $_.Split(" ")[0]; $fullpath = Join-Path $comp $share; [PSCustomObject]@{Path=$fullpath;HasAccess=$(Test-Path $fullpath -ErrorAction SilentlyContinue)}}}

如果您需要保存输出,则可以通过在最后一个括号之后抛出以下内容将其传递给Export-CSV:

| Export-CSV "\\path\to\file.csv" -NoTypeInformation

当网络视图抛出错误时,整个事情并不完美,但是我根据这里的评论写了它并且它工作得很好,并且对我需要的东西很有用,所以我想我会分享。:)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.