繁体   English   中英

从多个Windows服务器获取非管理文件共享的列表

[英]Obtain a list of non-admin file shares from multiple Windows servers

目标:获取包含以下信息的CSV文件:

  • 电脑名称
  • 股份名称
  • 分享路径
  • 分享说明

列表(txt文件)中所有服务器上所有非管理员(类型0)的SMB共享。

初始代码:

param (
    [Parameter(Mandatory=$true,Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]
    $path
)

$computers = Get-Content $path
$shareInfo = @()

ForEach ($computer in $computers) {
    $shares = gwmi -Computer $computer -Class Win32_Share -filter "Type = 0" | Select Name,Path,Description

    $shares | % {
        $ShareName = $_.Name
        $Props = [ordered]@{
            Computer = $computer
            ShareName = $_.Name
            Path = $shares.Path
            Description = $shares.Description
        }
    }

    $ShareInfo += New-Object -TypeName PSObject -Property $Props
}

$shareInfo | Export-CSV -Path .\shares.csv -NoType

代码输出:

"Computer","ShareName","Path","Description"
"SERVER1","SHARE1","System.Object[]","System.Object[]"
"SERVER2","SHARE12","System.Object[]","System.Object[]"
"SERVER3","SHARE3","System.Object[]","System.Object[]"

问题:

虽然代码为每个服务器提供了输出,但似乎不包括服务器中的所有份额。 此外,“路径”和“描述”字段不会填充良好的信息。

附加信息:

编码:

$shares = gwmi -Computer $computer -Class Win32_Share -filter "Type = 0" | Select Name,Path,Description

产生如下的良好信息:

Name           Path                                Description
----           ----                                -----------
print$         C:\WINDOWS\system32\spool\drivers   Printer Drivers
Share          D:\Share
SHARE2         D:\SHARE2
Software       C:\Software                         The Software
$shares | % {
    $ShareName = $_.Name
    $Props = [ordered]@{
        Computer = $computer
        ShareName = $_.Name
        Path = $shares.Path
        Description = $shares.Description
    }
}

您将$shares而不是$_用于PathDescription属性,因此为每个属性分配了$shares集合的每个元素的相应属性值的列表。

另外,为什么只需要过滤WMI查询结果时为什么要首先构建自定义对象? 可以从__SERVER (或PSMachineName )属性获得计算机名称。 另外,类型0表示共享磁盘驱动器,而不是管理共享。 您需要通过其他条件(通常是描述和/或共享名)来过滤后者。

$filter = "Type = 0 And Description != 'Default Share' And " +
          "Name != 'ADMIN$' And Name != 'IPC$'"

$computers |
  ForEach-Object { Get-WmiObject -Computer $_ -Class Win32_Share -Filter $filter } |
  Select-Object @{n='Computer';e={$_.__SERVER}}, Name, Path, Description |
  Export-Csv -Path .\shares.csv -NoType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM