网络视图 [Windows]

NET VIEW [Windows]

我正在尝试通过在 cmd 上使用 net view \server_name /all 命令将共享文件从服务器获取到 .txt 文件中,但有数百台服务器,我可以使用batch file 或者我可以在 .txt 文件上使用 net view,其中列出了所有服务器。

我遇到了这段代码,但它又是针对单个服务器的,可以修改它以用于 .txt 文件

# List the file shares on the remote server: SERVER64.

$shares = Get-WmiObject -class Win32_Share -computername SERVER64 -filter "Type=0"
$shares | foreach {
    $path=($_.path)
    $Description=($_.Description)
    $name=($_.name)
    $Caption=($_.Caption)

    "Share Name   : $name
     Source Folder: $path 
     Description  : $Description
     Caption : $Caption"
    }

-ComputerName 参数接受名称数组。它是从 help Get-WmiObject -full 中通过 [-ComputerName <String[]>] 参数得知的。

$computers = @('SERVER64', 'SERVER65', 'SERVER66')
$shares = Get-WmiObject -class Win32_Share -computername $computers -filter "Type=0"

这会将 PSComputerName 成员添加到输出中。

Microsoft 曾说过 CIM 是未来。

$shares = Get-CimInstance -ClassName Win32_Share -ComputerName $computers -Filter "Type=0"

我同意 lit 的观点,但这里有一个带有 [PSCustomObject]

示例的脚本
## Q:\Test18\SO_52991906.ps1

$ServerShares = ForEach ($Server in (Get-Content .\Servers.txt)){
  ForEach ($Share in (Get-WmiObject -Class Win32_Share -Computername $Server -filter "Type=0"|
                      Select-Object Name,Path,Description,Caption)){
        [PSCustomObject]@{
            Server          = $Server
            'Share Name'    = $Share.Name
            'Source Folder' = $Share.Path
            Description     = $Share.Description
            Caption         = $Share.Caption
        }
    }
}
$ServerShares | Format-Table -auto
#$ServerShares | Out-GridView
#$ServerShares | Export-Csv .\ServerShares.csv -NoTypeInformation

示例输出

> Q:\Test18\SO_52991906.ps1

Server   Share Name Source Folder                     Description      Caption
------   ---------- -------------                     -----------      -------
ServX    C          C:\                               System           System
ServX    D          D:\                               Daten            Daten
ServX    G          G:\                                                G
ServX    H          H:\                                                H
ServX    print$     C:\Windows\system32\spool\drivers Druckertreiber   Druckertreiber
ServX    LotPings   D:\Home\LotPings                                   LotPings
ServX    Users      C:\Users                                           Users
ServX    Virtual    D:\Virtual                                         Virtual
ServX    Winstall   D:\Winstall                                        Winstall
ServX    X          X:\                                                X
ServX    Y$         Y:\                               Standardfreigabe Standardfreigabe
ServX    Z$         Z:\                               Standardfreigabe Standardfreigabe