跳到主要内容

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

浏览文件

windows_lifecycle.ps1

29 行 · 2.2 KB历史下载
1param([Parameter(Mandatory)][string]$Query)
2$ErrorActionPreference='Stop'
3$script:targetId=[guid]'9e97dd9a-eb36-489a-bb1d-8d28a0d78682'
4$script:state='Off';$script:nics=0;$script:mutations=0;$script:generation=2;$script:secure='On'
5New-Module -Name Hyper-V -ScriptBlock {
6 function Get-VM { param([Guid]$Id) if ($Id -ne $script:targetId) { throw 'Unscoped VM operation.' }; [pscustomobject]@{Id=$Id;State=$script:state;Generation=$script:generation} }
7 function Get-VMFirmware { param($VM) if ($VM.Id -ne $script:targetId) { throw 'Wrong VM.' };[pscustomobject]@{SecureBoot=$script:secure;SecureBootTemplate='MicrosoftWindows'} }
8 function Get-VMSecurity { param($VM) [pscustomobject]@{TpmEnabled=$true} }
9 function Get-VMNetworkAdapter { param($VM) for($i=0;$i -lt $script:nics;$i++){[pscustomobject]@{Id=$i}} }
10 function Start-VM { param($VM) if($VM.Id -ne $script:targetId){throw 'Wrong VM.'};$script:state='Running';$script:mutations++ }
11 function Stop-VM { param($VM,[switch]$TurnOff,[switch]$Force) if($TurnOff -or $Force -or $VM.Id -ne $script:targetId){throw 'Unsafe stop.'};$script:state='Off';$script:mutations++ }
12 Export-ModuleMember -Function *
13} | Import-Module -Force
14# Module state is independent: configure it deliberately for each case.
15$module=Get-Module Hyper-V
16& $module { $script:targetId=[guid]'9e97dd9a-eb36-489a-bb1d-8d28a0d78682';$script:state='Off';$script:nics=0;$script:mutations=0;$script:generation=2;$script:secure='On' }
17$action='start';$result=(& $Query)|ConvertFrom-Json
18if($result.state -ne 'Running'){throw 'Start failed.'}
19$action='stop';$result=(& $Query)|ConvertFrom-Json
20if($result.state -ne 'Off'){throw 'Stop failed.'}
21$action='stop';$result=(& $Query)|ConvertFrom-Json
22if((& $module {$script:mutations}) -ne 2){throw 'Non-idempotent stop.'}
23foreach($unsafe in 'nic','generation','secure'){
24 & $module { $script:nics=0;$script:generation=2;$script:secure='On' }
25 switch($unsafe){'nic'{& $module {$script:nics=1}}'generation'{& $module {$script:generation=1}}'secure'{& $module {$script:secure='Off'}}}
26 $action='start';$rejected=$false;try{& $Query | Out-Null}catch{$rejected=$true}
27 if(!$rejected -or (& $module {$script:mutations}) -ne 2){throw "Unsafe $unsafe guest started."}
28}
29'Windows lifecycle exact VM and safe shutdown checks passed.'