Skip to content

Commit f2e315b

Browse files
committed
优化cursor_win_id_modifier.ps1脚本,增强注册表操作的错误处理和用户提示信息,确保在获取和更新MachineGuid时提供更清晰的反馈。同时,简化了备份文件的创建逻辑,仅在原始值存在时进行备份,提升了脚本的稳定性和用户体验。
1 parent 5a8d32a commit f2e315b

1 file changed

Lines changed: 35 additions & 57 deletions

File tree

scripts/run/cursor_win_id_modifier.ps1

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -200,42 +200,51 @@ if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti
200200

201201
function Update-MachineGuid {
202202
try {
203-
# 先检查注册表路径是否存在
203+
# 检查注册表路径是否存在,不存在则创建
204204
$registryPath = "HKLM:\SOFTWARE\Microsoft\Cryptography"
205205
if (-not (Test-Path $registryPath)) {
206-
throw "注册表路径不存在: $registryPath"
206+
Write-Host "$YELLOW[警告]$NC 注册表路径不存在: $registryPath,正在创建..."
207+
New-Item -Path $registryPath -Force | Out-Null
208+
Write-Host "$GREEN[信息]$NC 注册表路径创建成功"
207209
}
208210

209-
# 获取当前的 MachineGuid
210-
$currentGuid = Get-ItemProperty -Path $registryPath -Name MachineGuid -ErrorAction Stop
211-
if (-not $currentGuid) {
212-
throw "无法获取当前的 MachineGuid"
211+
# 获取当前的 MachineGuid,如果不存在则使用空字符串作为默认值
212+
$originalGuid = ""
213+
try {
214+
$currentGuid = Get-ItemProperty -Path $registryPath -Name MachineGuid -ErrorAction SilentlyContinue
215+
if ($currentGuid) {
216+
$originalGuid = $currentGuid.MachineGuid
217+
Write-Host "$GREEN[信息]$NC 当前注册表值:"
218+
Write-Host "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography"
219+
Write-Host " MachineGuid REG_SZ $originalGuid"
220+
} else {
221+
Write-Host "$YELLOW[警告]$NC MachineGuid 值不存在,将创建新值"
222+
}
223+
} catch {
224+
Write-Host "$YELLOW[警告]$NC 获取 MachineGuid 失败: $($_.Exception.Message)"
213225
}
214226

215-
$originalGuid = $currentGuid.MachineGuid
216-
Write-Host "$GREEN[信息]$NC 当前注册表值:"
217-
Write-Host "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography"
218-
Write-Host " MachineGuid REG_SZ $originalGuid"
219-
220227
# 创建备份目录(如果不存在)
221228
if (-not (Test-Path $BACKUP_DIR)) {
222229
New-Item -ItemType Directory -Path $BACKUP_DIR -Force | Out-Null
223230
}
224231

225-
# 创建备份文件
226-
$backupFile = "$BACKUP_DIR\MachineGuid_$(Get-Date -Format 'yyyyMMdd_HHmmss').reg"
227-
$backupResult = Start-Process "reg.exe" -ArgumentList "export", "`"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography`"", "`"$backupFile`"" -NoNewWindow -Wait -PassThru
228-
229-
if ($backupResult.ExitCode -eq 0) {
230-
Write-Host "$GREEN[信息]$NC 注册表项已备份到:$backupFile"
231-
} else {
232-
Write-Host "$YELLOW[警告]$NC 备份创建失败,继续执行..."
232+
# 创建备份文件(仅当原始值存在时)
233+
if ($originalGuid) {
234+
$backupFile = "$BACKUP_DIR\MachineGuid_$(Get-Date -Format 'yyyyMMdd_HHmmss').reg"
235+
$backupResult = Start-Process "reg.exe" -ArgumentList "export", "`"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography`"", "`"$backupFile`"" -NoNewWindow -Wait -PassThru
236+
237+
if ($backupResult.ExitCode -eq 0) {
238+
Write-Host "$GREEN[信息]$NC 注册表项已备份到:$backupFile"
239+
} else {
240+
Write-Host "$YELLOW[警告]$NC 备份创建失败,继续执行..."
241+
}
233242
}
234243

235244
# 生成新GUID
236245
$newGuid = [System.Guid]::NewGuid().ToString()
237246

238-
# 更新注册表
247+
# 更新或创建注册表值
239248
Set-ItemProperty -Path $registryPath -Name MachineGuid -Value $newGuid -Force -ErrorAction Stop
240249

241250
# 验证更新
@@ -252,8 +261,8 @@ function Update-MachineGuid {
252261
catch {
253262
Write-Host "$RED[错误]$NC 注册表操作失败:$($_.Exception.Message)"
254263

255-
# 尝试恢复备份
256-
if ($backupFile -and (Test-Path $backupFile)) {
264+
# 尝试恢复备份(如果存在)
265+
if (($backupFile -ne $null) -and (Test-Path $backupFile)) {
257266
Write-Host "$YELLOW[恢复]$NC 正在从备份恢复..."
258267
$restoreResult = Start-Process "reg.exe" -ArgumentList "import", "`"$backupFile`"" -NoNewWindow -Wait -PassThru
259268

@@ -529,42 +538,11 @@ function Write-ConfigFile {
529538
}
530539
}
531540

532-
function Compare-Version {
533-
param (
534-
[string]$version1,
535-
[string]$version2
536-
)
537-
538-
try {
539-
$v1 = [version]($version1 -replace '[^\d\.].*$')
540-
$v2 = [version]($version2 -replace '[^\d\.].*$')
541-
return $v1.CompareTo($v2)
542-
}
543-
catch {
544-
Write-Host "$RED[错误]$NC 版本比较失败: $_"
545-
return 0
546-
}
547-
}
548-
549-
# 在主流程开始时添加版本检查
550-
Write-Host "$GREEN[信息]$NC 正在检查 Cursor 版本..."
541+
# 获取并显示版本信息
551542
$cursorVersion = Get-CursorVersion
552-
543+
Write-Host ""
553544
if ($cursorVersion) {
554-
$compareResult = Compare-Version $cursorVersion "0.45.0"
555-
if ($compareResult -ge 0) {
556-
Write-Host "$RED[错误]$NC 当前版本 ($cursorVersion) 暂不支持"
557-
Write-Host "$YELLOW[建议]$NC 请使用 v0.45.x 及以下版本"
558-
Write-Host "$YELLOW[建议]$NC 可以从以下地址下载支持的版本:"
559-
Write-Host "Windows: https://download.todesktop.com/230313mzl4w4u92/Cursor%20Setup%200.44.11%20-%20Build%20250103fqxdt5u9z-x64.exe"
560-
Write-Host "Mac ARM64: https://dl.todesktop.com/230313mzl4w4u92/versions/0.44.11/mac/zip/arm64"
561-
Read-Host "按回车键退出"
562-
exit 1
563-
}
564-
else {
565-
Write-Host "$GREEN[信息]$NC 当前版本 ($cursorVersion) 支持重置功能"
566-
}
567-
}
568-
else {
545+
Write-Host "$GREEN[信息]$NC 检测到 Cursor 版本: $cursorVersion,继续执行..."
546+
} else {
569547
Write-Host "$YELLOW[警告]$NC 无法检测版本,将继续执行..."
570548
}

0 commit comments

Comments
 (0)