网络视图 - 获取“共享名称”

问题描述 投票:0回答:3

我需要获取某些存储中的所有共享名称。 我使用

Net view $StorageName
,它以表格格式显示结果:

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.

很好,但我只需要共享名。 我需要帮助。 谢谢你!

powershell
3个回答
4
投票

这是使用网络视图输出实现此目的的一种方法:

(net view $StorageName | Where-Object { $_ -match '\sDisk\s' }) -replace '\s\s+', ',' | ForEach-Object{ ($_ -split ',')[0] }

基本上就是说找到 Disk 被空格包围的行,以防其他名称中可能包含 Disk。然后用逗号替换多个空格。然后,对于每一行,再次用逗号分隔并取第一个值,即共享名。

如果您使用的是 Windows 8/2012 或更高版本的系统(并尝试枚举其他 Windows 系统上的共享),您可以使用 CIM 会话和 Get-SmbShare 而不是网络视图,后者会将结果作为对象返回并允许您以本机 PowerShell 方式选择所需的字段。

例如:

$cimSession = New-CimSession $StorageName
Get-SmbShare -CimSession $cimSession | Select Name

Name
----
Admin
ADMIN$
C$
IPC$
J$
print$
Public
Software

0
投票

解析网络视图输出的另一个选项,它依赖于定位而不是正则表达式匹配。我个人觉得它更容易阅读并且同样可靠。

function get-shares {

  param($server)

  $rawShares = net view \\$server
  $shares = $rawShares[7..($s.Count - 3)]

  $shares | . {process{$_.substring(0,($_.indexof(" ")))}}
}

0
投票

PowerShell SMB 不起作用,因此您必须转到

Netapi32
界面。以下是使用此接口枚举共享的本机 PowerShell 代码。这适用于提供 SMB 共享但不基于 Windows 的系统,例如 Nutanix AFS。

$TypeDefinition=@"
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;

public class GetNetShares {

  [DllImport("Netapi32.dll", SetLastError = true)]
    static extern int NetApiBufferFree(IntPtr Buffer);

  [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
    private static extern int NetShareEnum(
      StringBuilder ServerName,
     int level,
       ref IntPtr bufPtr,
      uint prefmaxlen,
      ref int entriesread,
      ref int totalentries,
      ref int resume_handle);

  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_1 {
      public string shi1_netname;
      public uint shi1_type;
      public string shi1_remark;
      public SHARE_INFO_1(string sharename, uint sharetype, string remark) {
        this.shi1_netname = sharename;
        this.shi1_type = sharetype;
        this.shi1_remark = remark;
      }
      public override string ToString() { return shi1_netname; }
    }

  const uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF;
  const int NERR_Success = 0;

  private enum NetError : uint {
    NERR_Success = 0,
    NERR_BASE = 2100,
    NERR_UnknownDevDir = (NERR_BASE + 16),
    NERR_DuplicateShare = (NERR_BASE + 18),
    NERR_BufTooSmall = (NERR_BASE + 23),
  }

  private enum SHARE_TYPE : uint {
    STYPE_DISKTREE = 0,
    STYPE_PRINTQ = 1,
    STYPE_DEVICE = 2,
    STYPE_IPC = 3,
    STYPE_SPECIAL = 0x80000000,
  }

  public SHARE_INFO_1[] EnumNetShares(string Server) {
    List<SHARE_INFO_1> ShareInfos = new List<SHARE_INFO_1>();
    int entriesread = 0;
    int totalentries = 0;
    int resume_handle = 0;
    int nStructSize = Marshal.SizeOf(typeof(SHARE_INFO_1));
    IntPtr bufPtr = IntPtr.Zero;
    StringBuilder server = new StringBuilder(Server);
    int ret = NetShareEnum(server, 1, ref bufPtr, MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);
    if (ret == NERR_Success) {
      IntPtr currentPtr = bufPtr;
      for (int i = 0; i < entriesread; i++) {
        SHARE_INFO_1 shi1 = (SHARE_INFO_1)Marshal.PtrToStructure(currentPtr, typeof(SHARE_INFO_1));
        ShareInfos.Add(shi1);
        currentPtr += nStructSize;
      }
    
      NetApiBufferFree(bufPtr);
      return ShareInfos.ToArray();
    }
    else {
      ShareInfos.Add(new SHARE_INFO_1(ret.ToString(),10,"ERROR"));
      return ShareInfos.ToArray();
    }
  }
}
"@
Add-Type -TypeDefinition $TypeDefinition

$x=[GetNetShares]::new()
$x.EnumNetShares("<computer name>")

<#
Errorlist:
5   : The user does not have access to the requested information.
124 : The value specified for the level parameter is not valid.
87  : The specified parameter is not valid.
234 : More entries are available. Specify a large enough buffer to receive all entries.
8   : Insufficient memory is available.
2312: A session does not exist with the computer name.
2351: The computer name is not valid.
2221: Username not found.
53  : Hostname could not be found.
#>
© www.soinside.com 2019 - 2024. All rights reserved.