param( [Parameter(Mandatory = $true)] [pscustomobject[]]$Disks ) $uniqueDisks = @( $Disks | Group-Object -Property ComputerName,Path | ForEach-Object { $_.Group[0] } ) Write-Host "total disk: $($Disks.Count)" Write-Host "unique disk: $($uniqueDisks.Count)" $report = $uniqueDisks | Group-Object ComputerName,StorageRoot | ForEach-Object { $group = $_ $computerName = $group.Group[0].ComputerName $root = $group.Group[0].StorageRoot $vhdUsedGB = [math]::Round( ($group.Group | Measure-Object FileSize -Sum).Sum / 1GB, 2 ) $totalGB = $null $freeGB = $null # UNC: VHDの件数・容量のみ集計し、共有先の容量は取得しない if ($root.StartsWith('\\')) { # FreeGB、TotalGB、FreePercentは空欄 } # クラスター共有ボリューム elseif ($root -like 'C:\ClusterStorage\*') { $csv = Get-ClusterSharedVolume -Cluster $computerName| Where-Object { $_.SharedVolumeInfo.FriendlyVolumeName -eq $root } if ($csv) { $partition = $csv.SharedVolumeInfo.Partition $totalGB = [math]::Round($partition.Size / 1GB, 2) $freeGB = [math]::Round($partition.FreeSpace / 1GB, 2) } } # 通常のドライブ elseif ($root -match '^([A-Za-z]):\\') { $driveLetter = $matches[1] try { $cim = New-CimSession -ComputerName $computerName -ErrorAction Stop $volume = Get-Volume -DriveLetter $driveLetter ` -CimSession $cim -ErrorAction Stop $totalGB = [math]::Round($volume.Size / 1GB, 2) $freeGB = [math]::Round($volume.SizeRemaining / 1GB, 2) } catch { Write-Error -Message ( "容量取得に失敗しました(接続先: {0}、ドライブ: {1}:): {2}" -f $computerName, $driveLetter, $_.Exception.Message ) -ErrorAction Continue } finally { if ($null -ne $cim) { Remove-CimSession -CimSession $cim -ErrorAction Continue } } } [pscustomobject]@{ ComputerName = $computerName StorageRoot = $root VHDCount = $group.Count VHDUsedGB = $vhdUsedGB FreeGB = $freeGB TotalGB = $totalGB FreePercent = if ($totalGB) { [math]::Round(($freeGB / $totalGB) * 100, 1) } else { $null } } } $report