| 1 | $ErrorActionPreference = 'Stop' |
| 2 | $ProgressPreference = 'SilentlyContinue' |
| 3 | $id = [guid]$inputData.vm_id |
| 4 | $vm = Get-VM -Id $id |
| 5 | $recoveringDisk = $inputData.action -eq 'apply' -and $inputData.disk -ieq $inputData.source_disk |
| 6 | $stopped = $vm.State -eq 'Off' -or ($recoveringDisk -and $vm.State -eq 'OffCritical') |
| 7 | if (!$stopped -or $vm.Generation -ne 2 -or |
| 8 | @(Get-VMNetworkAdapter -VM $vm).Count -ne 0 -or |
| 9 | (Get-VMFirmware -VM $vm).SecureBoot -ne 'On' -or !(Get-VMSecurity -VM $vm).TpmEnabled) { |
| 10 | throw 'vm_changed' |
| 11 | } |
| 12 | $disks = @(Get-VMHardDiskDrive -VM $vm) |
| 13 | if ($disks.Count -ne 1) { throw 'vm_changed' } |
| 14 | function Read-Storage { |
| 15 | $current = Get-VM -Id $id |
| 16 | [pscustomobject]@{ |
| 17 | configuration = [string]$current.ConfigurationLocation |
| 18 | snapshots = [string]$current.SnapshotFileLocation |
| 19 | paging = [string]$current.SmartPagingFilePath |
| 20 | } |
| 21 | } |
| 22 | if ($inputData.action -eq 'inspect') { |
| 23 | Read-Storage | ConvertTo-Json -Compress |
| 24 | exit 0 |
| 25 | } |
| 26 | if ($inputData.action -eq 'export') { |
| 27 | $namespace = 'root\virtualization\v2' |
| 28 | $system = Get-WmiObject -Namespace $namespace -Class Msvm_ComputerSystem -Filter ("Name='" + $id.ToString('D') + "'") |
| 29 | $service = Get-WmiObject -Namespace $namespace -Class Msvm_VirtualSystemManagementService |
| 30 | $settings = ([wmiclass]'\\.\root\virtualization\v2:Msvm_VirtualSystemExportSettingData').CreateInstance() |
| 31 | $settings.CopySnapshotConfiguration = 0 |
| 32 | $settings.CopyVmRuntimeInformation = $true |
| 33 | $settings.CopyVmStorage = $false |
| 34 | $settings.CreateVmExportSubdirectory = $false |
| 35 | $result = $service.ExportSystemDefinition($system.__PATH, [string]$inputData.backup, $settings.GetText(1)) |
| 36 | if ($result.ReturnValue -eq 4096) { |
| 37 | $job = [wmi]$result.Job |
| 38 | while ($job.JobState -in @(2, 3, 4, 6)) { Start-Sleep -Milliseconds 200; $job.Get() } |
| 39 | if ($job.JobState -ne 7 -or $job.ErrorCode -ne 0) { throw 'storage_export_failed' } |
| 40 | } elseif ($result.ReturnValue -ne 0) { |
| 41 | throw 'storage_export_failed' |
| 42 | } |
| 43 | [pscustomobject]@{ exported = $true } | ConvertTo-Json -Compress |
| 44 | exit 0 |
| 45 | } |
| 46 | if ($inputData.action -ne 'apply') { throw 'invalid_operation' } |
| 47 | $old = $inputData.old |
| 48 | $next = $inputData.next |
| 49 | $current = Read-Storage |
| 50 | foreach ($field in @('configuration', 'snapshots', 'paging')) { |
| 51 | if ($current.$field -ine $old.$field -and $current.$field -ine $next.$field) { throw 'vm_changed' } |
| 52 | } |
| 53 | if ($disks[0].Path -ine $inputData.old_disk -and $disks[0].Path -ine $inputData.source_disk -and $disks[0].Path -ine $inputData.new_disk) { throw 'vm_changed' } |
| 54 | $target = $inputData.target |
| 55 | $move = @{} |
| 56 | if ($current.configuration -ine $target.configuration) { $move.VirtualMachinePath = [string]$target.configuration } |
| 57 | if ($current.snapshots -ine $target.snapshots) { $move.SnapshotFilePath = [string]$target.snapshots } |
| 58 | if ($current.paging -ine $target.paging) { $move.SmartPagingFilePath = [string]$target.paging } |
| 59 | if ($move.Count) { |
| 60 | # Hyper-V owns configuration, vTPM state and snapshot metadata. Its native |
| 61 | # storage operation preserves the registered VM identity and permissions. |
| 62 | # The verified VHDX copy is attached separately so its original is retained. |
| 63 | Move-VMStorage -VM $vm @move -Confirm:$false |
| 64 | } |
| 65 | $vm = Get-VM -Id $id |
| 66 | $disks = @(Get-VMHardDiskDrive -VM $vm) |
| 67 | if (($vm.State -ne 'Off' -and !($recoveringDisk -and $vm.State -eq 'OffCritical')) -or $disks.Count -ne 1) { throw 'vm_changed' } |
| 68 | if ($disks[0].Path -ine $inputData.disk) { |
| 69 | Set-VMHardDiskDrive -VMHardDiskDrive $disks[0] -Path ([string]$inputData.disk) |
| 70 | } |
| 71 | $readback = Read-Storage |
| 72 | foreach ($field in @('configuration', 'snapshots', 'paging')) { |
| 73 | if ($readback.$field -ine $target.$field) { throw 'storage_readback_failed' } |
| 74 | } |
| 75 | $disk = @(Get-VMHardDiskDrive -VM (Get-VM -Id $id)) |
| 76 | if ($disk.Count -ne 1 -or $disk[0].Path -ine $inputData.disk) { throw 'storage_readback_failed' } |
| 77 | [pscustomobject]@{ applied = $true } | ConvertTo-Json -Compress |