PowerShell 获取共享文件夹列表

PowerShell 获取共享文件夹列表

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

\\MYPC\Test1

\\MYPC\Test2

这是我目前拥有的代码:

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

Write-Host $FolderList

但结果显示“找不到路径”。我可以看到如何对\\Server\Share目录执行此操作的示例,但是是否可以只搜索\\Server

答案1

尝试这个:

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

参考:使用 PowerShell 列出 Windows 中的共享

答案2

电源外壳无法使用SMB 协议以便列出远程计算机上的共享。据我所知,只有一种从命令行远程枚举共享的方法,那就是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] } }

答案3

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

> Get-SmbShare

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

答案4

感谢 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

相关内容