0

目標: 次の情報を含む 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
4

1 に答える 1

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

You're using $shares instead of $_ for the Path and Description properties, so each of these properties is assigned a list of the values of the respective property of each element of the $shares collection.

Also, why are you building custom objects in the first place when you just need to filter the WMI query results? The computer name can be obtained from the __SERVER (or PSMachineName) property. Plus, type 0 means a shared disk drive, not an administrative share. You need to filter the latter by other criteria (usually description and/or share name).

$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
于 2015-11-23T15:40:55.060 に答える