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