跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

windows_storage.ps1

77 行 · 3.8 KB历史下载
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')
7if (!$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)
13if ($disks.Count -ne 1) { throw 'vm_changed' }
14function 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}
22if ($inputData.action -eq 'inspect') {
23 Read-Storage | ConvertTo-Json -Compress
24 exit 0
25}
26if ($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}
46if ($inputData.action -ne 'apply') { throw 'invalid_operation' }
47$old = $inputData.old
48$next = $inputData.next
49$current = Read-Storage
50foreach ($field in @('configuration', 'snapshots', 'paging')) {
51 if ($current.$field -ine $old.$field -and $current.$field -ine $next.$field) { throw 'vm_changed' }
52}
53if ($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 = @{}
56if ($current.configuration -ine $target.configuration) { $move.VirtualMachinePath = [string]$target.configuration }
57if ($current.snapshots -ine $target.snapshots) { $move.SnapshotFilePath = [string]$target.snapshots }
58if ($current.paging -ine $target.paging) { $move.SmartPagingFilePath = [string]$target.paging }
59if ($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)
67if (($vm.State -ne 'Off' -and !($recoveringDisk -and $vm.State -eq 'OffCritical')) -or $disks.Count -ne 1) { throw 'vm_changed' }
68if ($disks[0].Path -ine $inputData.disk) {
69 Set-VMHardDiskDrive -VMHardDiskDrive $disks[0] -Path ([string]$inputData.disk)
70}
71$readback = Read-Storage
72foreach ($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))
76if ($disk.Count -ne 1 -or $disk[0].Path -ine $inputData.disk) { throw 'storage_readback_failed' }
77[pscustomobject]@{ applied = $true } | ConvertTo-Json -Compress