param( [Parameter(Mandatory)] [string]$Path ) $used = [System.Collections.Generic.HashSet[string]]::new( [System.StringComparer]::OrdinalIgnoreCase ) # VMが参照しているVHDチェーンを収集 # VM一覧の取得に失敗した場合は、誤判定を避けるため停止 $vms = @(Get-VM -ErrorAction Stop) foreach ($vm in $vms) { $drives = @( Get-VMHardDiskDrive -VMName $vm.Name -ErrorAction Stop ) foreach ($drive in $drives) { $p = $drive.Path while ($p) { $full = [IO.Path]::GetFullPath($p) # 収集済みなら親ディスクも確認済み if (-not $used.Add($full)) { break } try { $p = (Get-VHD -Path $full -ErrorAction Stop).ParentPath } catch { throw "VMが使用するVHDチェーンを確認できません: $full / $($_.Exception.Message)" } } } } # 検索先を確認 $searchPath = Convert-Path -LiteralPath $Path -ErrorAction Stop if (-not (Test-Path -LiteralPath $searchPath -PathType Container)) { throw "フォルダーを指定してください: $Path" } $root = [IO.Path]::GetPathRoot($searchPath) # ドライブ/共有のルート直下だけを除外 $excludedPaths = @( (Join-Path $root '$RECYCLE.BIN') (Join-Path $root 'System Volume Information') ) $scanErrors = @() $folders = [System.Collections.Generic.Stack[string]]::new() $folders.Push($searchPath) # 除外フォルダーに入らずVHDファイルを探索 $files = @( while ($folders.Count -gt 0) { $folder = $folders.Pop() if ($folder.TrimEnd('\') -iin $excludedPaths) { continue } $items = @( Get-ChildItem -LiteralPath $folder -Force ` -ErrorAction SilentlyContinue ` -ErrorVariable +scanErrors ) foreach ($item in $items) { if ($item.PSIsContainer) { # ジャンクションなどのリンク先には入らない if (-not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { $folders.Push($item.FullName) } continue } if ($item.Extension -in '.vhd', '.vhdx', '.avhd', '.avhdx') { $item } } } ) # Hyper-VのVMから参照されていないVHDを抽出 $result = @( foreach ($diskFile in $files) { if ($used.Contains($diskFile.FullName)) { continue } $vhd = $null try { $vhd = Get-VHD -Path $diskFile.FullName -ErrorAction Stop } catch { Write-Warning "VHD情報を取得できません: $($diskFile.FullName)" } # パスとファイル名から用途のヒントを付与 $hints = @( if ( $diskFile.FullName -imatch 'windowsfilter' -or $diskFile.FullName -imatch 'docker' ) { 'docker?' } if ( $diskFile.FullName -imatch 'WSL' -or $diskFile.Name -ieq 'ext4.vhdx' ) { 'WSL2?' } if ($diskFile.FullName -imatch 'WindowsImageBackup') { 'Backup Data?' } if ($diskFile.Name -imatch 'UVHD') { 'RDS?' } if ($diskFile.FullName -imatch '\\Microsoft\\Windows\\Containers\\') { 'Windows Container or Windows Sandbox?' } ) [pscustomobject]@{ FileName = $diskFile.Name Path = $diskFile.FullName Hint = $hints -join ', ' VHDFormat = $vhd.VhdFormat VHDType = $vhd.VhdType VHDParent = $vhd.ParentPath SizeGB = [math]::Round($diskFile.Length / 1GB, 2) LastWriteTime = $diskFile.LastWriteTime } } ) # 検索エラーを簡潔に表示 foreach ($scanError in $scanErrors) { if ($scanError.CategoryInfo.Category -eq 'PermissionDenied') { Write-Warning "アクセス拒否: $($scanError.TargetObject)" } else { Write-Warning "検索エラー: $($scanError.TargetObject) / $($scanError.Exception.Message)" } } if ($result.Count -gt 0) { $result } else { Write-Host ( "Find-OrphandVhd.ps1: $Path の検索できた範囲では、" + "Hyper-VのVMから参照されていないVHDは見つかりませんでした。" ) -ForegroundColor Green }