在UNC Path \\ SERVERNAME

时间:2017-07-13 19:35:51

标签: powershell get-childitem network-shares

我们正在尝试将PowerShell用作文件复制工具,但遇到了问题。

源服务器是基于Linux的封闭式操作系统,用户通过GUI创建多个网络共享,然后由UNC路径\\SERVERNAME\NETWORKSHARE访问。当然,在操作系统中,NETWORKSHARE实际上位于比根更深的多个级别。

我们已经与操作系统的开发人员进行了交谈,他们不能/不会启用对存储所有NETWORKSHARE文件夹的文件夹的根目录的访问。

我转向PowerShell试图找到一种方法。我最初的想法是发送一个Get-ChildItem -Directory\\SERVERNAME来抓取所有的NETWORKSHARES,然后将其传递给Copy-Item/Robocopy,但它不起作用。

我知道服务器需要自己的本地(非AD)凭证。

有没有办法做我想做的事情,我只是没有看到?

提前致谢!

2 个答案:

答案 0 :(得分:0)

带有IP的net view命令将枚举可访问的SMB / Samba共享 此PowerShell脚本解析输出并返回带有服务器和共享属性的[PSCustomObject]

## Enum-SambaShares.ps1
$ServerList = ('192.168.133.67','192.168.133.46','192.168.133.76','192.168.133.28')
$ServerShares = ForEach ($Server in $ServerList) {
    net view "\\$Server" 2>$Null | Select-String '^([^ ]+)\s+(Disk|Platte)' | 
        ForEach-Object {
            [PSCustomObject]@{
                Server = $Server
                Share  = $_.matches.groups[1].Value
            }
        }
}
$ServerShares

示例输出:

Server        Share
------        -----
192.168.133.67 Backup
192.168.133.67 Daten
192.168.133.67 Video-Unc
192.168.133.46 SambaShare
192.168.133.46 UserName
192.168.133.76 C
192.168.133.28 McDaten
192.168.133.28 Music
192.168.133.28 UserName

答案 1 :(得分:0)

解析new view的输出有点笨拙。这是一种直接从PowerShell调用NetShareEnum Win32 API并将结果作为对象输出的方法:

# Get-NetShare.ps1

#requires -version 2

param(
  [String] $ComputerName = "."
)

Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Text;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHARE_INFO_1
{
  [MarshalAs(UnmanagedType.LPWStr)]
  public string shi1_netname;
  public uint shi1_type;
  [MarshalAs(UnmanagedType.LPWStr)]
  public string shi1_remark;
}
public static class NetApi32
{
  [DllImport("netapi32.dll", SetLastError = true)]
  public static extern int NetApiBufferFree(IntPtr Buffer);
  [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  public static extern int NetShareEnum(
    StringBuilder servername,
    int level,
    ref IntPtr bufptr,
    uint prefmaxlen,
    ref int entriesread,
    ref int totalentries,
    ref int resume_handle);
}
"@

$pBuffer = [IntPtr]::Zero
$entriesRead = $totalEntries = $resumeHandle = 0
$result = [NetApi32]::NetShareEnum(
  $ComputerName,        # servername
  1,                    # level
  [Ref] $pBuffer,       # bufptr
  [UInt32]::MaxValue,   # prefmaxlen
  [Ref] $entriesRead,   # entriesread
  [Ref] $totalEntries,  # totalentries
  [Ref] $resumeHandle   # resumehandle
)
if ( ($result -eq 0) -and ($pBuffer -ne [IntPtr]::Zero) -and ($entriesRead -eq $totalEntries) ) {
  $offset = $pBuffer.ToInt64()
  for ( $i = 0; $i -lt $totalEntries; $i++ ) {
    $pEntry = New-Object IntPtr($offset)
    $shareInfo = [Runtime.InteropServices.Marshal]::PtrToStructure($pEntry, [Type] [SHARE_INFO_1])
    $shareInfo
    $offset += [Runtime.InteropServices.Marshal]::SizeOf($shareInfo)
  }
  [Void] [NetApi32]::NetApiBufferFree($pBuffer)
}
if ( $result -ne 0 ) {
  Write-Error -Exception (New-Object ComponentModel.Win32Exception($result))
}

您可以获得远程计算机的磁盘共享:

Get-NetShare sambaservername | Where-Object { $_.shi1_type -eq 0 }