Answers:
net view
不会返回隐藏的股票。
从我所知道的命令行远程枚举共享只有一种方法,那就是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] } }
如果要查找本地计算机的共享,您可以执行以下操作Get-SmbShare
:
> Get-SmbShare
Name ScopeName Path Description
---- --------- ---- -----------
ADMIN$ * C:\WINDOWS Remote Admin
C$ * C:\ Default share
扩展马克亨德森的答案:
$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
}
感谢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
在Windows 8或更高版本以及Windows Server 2012或更高版本上,您可以使用SmbShare模块中的Get-SmbShare。
这是一个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
当网络视图抛出错误时,整个事情并不完美,但是我根据这里的评论写了它并且它工作得很好,并且对我需要的东西很有用,所以我想我会分享。:)