$vms = @(Get-VM -ErrorAction Stop) $disks = @( foreach ($vm in $vms) { # 現在と各チェックポイントの接続情報 $sources = @( [pscustomobject]@{ Source = "VirtualMachine" CheckpointName = $null CheckpointId = $null Drives = @($vm | Get-VMHardDiskDrive) } foreach ($snapshot in ($vm | Get-VMSnapshot)) { [pscustomobject]@{ Source = "CheckPoint" CheckpointName = $snapshot.Name CheckpointId = $snapshot.Id Drives = @( Get-VMHardDiskDrive -VMSnapshot $snapshot ) } } ) $seenPaths = @{} # VMごとに重複除去 foreach ($source in $sources) { foreach ($drive in $source.Drives) { if (-not $drive.Path) { continue } $diskPath = $drive.Path $chainLevel = 0 while ($diskPath) { if ($seenPaths.ContainsKey($diskPath)) { # VM内で処理済みのディスクはスキップ break } try { $vhd = Get-VHD -ComputerName $vm.Computername -Path $diskPath -ErrorAction Stop $seenPaths[$diskPath] = $true } catch { Write-Warning "取得失敗: VM=$($vm.Name), Path=$diskPath : $_" break } $directory = [IO.Path]::GetDirectoryName($diskpath) # ルートパス(集計用) if ($diskpath -match '^([A-Za-z]:\\ClusterStorage\\[^\\]+)') { #CSV(C:\ClusterStorage\folder) $root = $matches[1] } elseif ($directory -match '^([A-Za-z]:\\[^\\]+)') { #ドライブ文字:\フォルダー $root = $matches[1] } else { #UNC(\\SERVER\share)またはドライブ直下(D:¥) $root = [IO.Path]::GetPathRoot($diskpath) } [pscustomobject]@{ ComputerName = $vm.ComputerName VMName = $vm.Name VMId = $vm.Id CheckpointName = $source.CheckpointName CheckpointId = $source.CheckpointId ControllerType = $drive.ControllerType ControllerNumber = $drive.ControllerNumber ControllerLocation = $drive.ControllerLocation Controller = '{0} {1}:{2}' -f $drive.ControllerType, $drive.ControllerNumber, $drive.ControllerLocation ChainLevel = $chainLevel Path = $vhd.Path ParentPath = $vhd.ParentPath VhdType = $vhd.VhdType VhdFormat = $vhd.VhdFormat VhdSize = $vhd.Size VhdSizeGB = [math]::Round($vhd.Size / 1GB, 2) FileSize = $vhd.FileSize FileSizeGB = [math]::Round($vhd.FileSize / 1GB, 2) StorageRoot = $root } $diskPath = $vhd.ParentPath $chainLevel++ } } } } ) $uniqueDisks = @( $disks | Group-Object -Property ComputerName, Path | ForEach-Object { $_.Group[0] } ) Write-Host "Total Disk: $($disks.count)" Write-Host "Unique Disk: $($uniqueDisks.count)" $fileName = '.\VMDiskReport-{0}-{1}.csv' -f $env:COMPUTERNAME, (Get-Date -Format 'yyyyMMdd-HHmmss') $disks | Export-Csv -Path $fileName -NoTypeInformation -Encoding UTF8 $disks Write-Host "CSV out to: $($fileName)"