2014-06-17 01:51:35 +0000 2014-06-17 01:51:35 +0000
19
19

PowerShell 获取共享的文件夹列表

我正试图获得一个文件共享上共享的文件夹列表。目前我有两个测试文件夹。

\MYPC\Test1

\MYPC\Test2

这是我现在的代码:

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

Write-Host $FolderList

但结果是 “找不到路径"。我可以看到以\Server\Share为目录的例子,但是否可以只搜索\Server

答案 (8)

24
24
24
2014-06-17 02:38:03 +0000

试试这个。

get-WmiObject -class Win32_Share -computer dc1.krypted.com

参考:List Shares in Windows w/PowerShell_。

14
14
14
2014-06-17 02:58:41 +0000

据我所知,只有一种方法可以从命令行远程枚举共享,那就是使用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] } }
8
8
8
2016-05-09 14:46:58 +0000

如果你想找到本地机器的份额,你可以直接做Get-SmbShare

> Get-SmbShare

Name ScopeName Path Description
---- --------- ---- -----------
ADMIN$ * C:\WINDOWS Remote Admin
C$ * C:\ Default share
3
3
3
2015-09-15 02:19:32 +0000

展开【马克-亨德森的回答】(https://superuser.com/a/769694/461558):

$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
}
3
3
3
2016-05-20 22:20:09 +0000

感谢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
1
1
1
2018-09-27 20:40:01 +0000

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

0
0
0
2019-04-08 20:22:36 +0000

这里有一个PowerShell单行本,它使用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

当net view出现错误时,整个过程并不完美,但我是根据这里的评论写的,它很好用,对我的需求很有帮助,所以我想我应该分享一下 :)

0
0
0
2015-04-10 17:14:25 +0000

Windows资源包工具。rmtshare

在远程服务器上以管理员权限在id下运行,或以ipc$连接到远程服务器。

rmtshare \servername