Skip to content

Commit 85c5b5f

Browse files
Update run.ps1
1 parent 1373067 commit 85c5b5f

File tree

1 file changed

+198
-56
lines changed

1 file changed

+198
-56
lines changed

run.ps1

Lines changed: 198 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,116 @@
1-
# Set output encoding to UTF-8
1+
# Set the output encoding to UTF-8
22
$OutputEncoding = [System.Text.Encoding]::UTF8
33
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
44

5-
# Color definitions
5+
# Color definition
66
$RED = "`e[31m"
77
$GREEN = "`e[32m"
88
$YELLOW = "`e[33m"
99
$BLUE = "`e[34m"
1010
$NC = "`e[0m"
1111

12-
# Configuration file paths
12+
# Configuration file path
1313
$STORAGE_FILE = "$env:APPDATA\Cursor\User\globalStorage\storage.json"
1414
$BACKUP_DIR = "$env:APPDATA\Cursor\User\globalStorage\backups"
1515

16-
# Check for administrator privileges
16+
# Check administrator privileges
1717
function Test-Administrator {
1818
$user = [Security.Principal.WindowsIdentity]::GetCurrent()
1919
$principal = New-Object Security.Principal.WindowsPrincipal($user)
2020
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
2121
}
2222

2323
if (-not (Test-Administrator)) {
24-
Write-Host "$RED[Error]$NC Please run this script as an administrator."
25-
Write-Host "Right-click the script and select 'Run as Administrator'."
24+
Write-Host "$RED[error]$NC Please run this script as an administrator"
25+
Write-Host "Right-click the script and select 'Run as Administrator'"
2626
Read-Host "Press Enter to exit"
2727
exit 1
2828
}
2929

3030
# Display Logo
3131
Clear-Host
32-
Write-Host "@"
33-
Write-Host "
34-
██████╗██╗ ██╗██████╗ ███████╗ ██████╗ ██████╗
35-
██╔════╝██║ ██║██╔══██╗██╔════╝██╔═══██╗██╔══██╗
36-
██║ ██║ ██║██████╔╝███████╗██║ ██║██████╔╝
37-
██║ ██║ ██║██╔══██╗╚════██║██║ ██║██╔══██╗
38-
╚██████╗╚██████╔╝██║ ██║███████║╚██████╔╝██║ ██║
39-
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
40-
"
32+
Write-Host @"
33+
34+
35+
██╔════╝██║ ██║██╔══██╗██╔════╝██╔═══██╗██╔══██╗
36+
37+
██║ ██║ ██║██╔══██╗╚════██║██║ ██║██╔══██╗
38+
*
39+
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
40+
41+
"@
4142
Write-Host "$BLUE================================$NC"
42-
Write-Host "$GREEN Cursor ID Modification Tool $NC"
43+
Write-Host "$GREEN Cursor ID Modification Tool $NC"
4344
Write-Host "$BLUE================================$NC"
4445
Write-Host ""
4546

46-
# Check and close Cursor processes
47-
Write-Host "$GREEN[Info]$NC Checking for Cursor processes..."
47+
# Check and close the Cursor process
48+
Write-Host "$GREEN[INFO]$NC Check Cursor Process..."
4849

4950
function Get-ProcessDetails {
5051
param($processName)
51-
Write-Host "$BLUE[Debug]$NC Retrieving details for $processName:"
52-
Get-WmiObject Win32_Process -Filter "name='$processName'" |
53-
Select-Object ProcessId, ExecutablePath, CommandLine |
52+
Write-Host "$BLUE[debug]$NC Getting $processName process details:"
53+
Get-WmiObject Win32_Process -Filter "name='$processName'" |
54+
Select-Object ProcessId, ExecutablePath, CommandLine |
5455
Format-List
5556
}
5657

57-
# Define retry settings
58+
# Define the maximum number of retries and waiting time
5859
$MAX_RETRIES = 5
5960
$WAIT_TIME = 1
6061

61-
# Handle process closure
62+
# Process shutdown
6263
function Close-CursorProcess {
6364
param($processName)
64-
65+
6566
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue
6667
if ($process) {
67-
Write-Host "$YELLOW[Warning]$NC Found $processName running."
68+
Write-Host "$YELLOW[Warning]$NC found $processName running"
6869
Get-ProcessDetails $processName
69-
70-
Write-Host "$YELLOW[Warning]$NC Attempting to close $processName..."
70+
71+
Write-Host "$YELLOW[WARN]$NC Trying to shut down $processName..."
7172
Stop-Process -Name $processName -Force
72-
73+
7374
$retryCount = 0
7475
while ($retryCount -lt $MAX_RETRIES) {
7576
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue
7677
if (-not $process) { break }
77-
78+
7879
$retryCount++
7980
if ($retryCount -ge $MAX_RETRIES) {
80-
Write-Host "$RED[Error]$NC Unable to close $processName after $MAX_RETRIES attempts."
81+
Write-Host "$RED[ERROR]$NC Failed to close $processName after $MAX_RETRIES attempts"
8182
Get-ProcessDetails $processName
82-
Write-Host "$RED[Error]$NC Please manually close the process and try again."
83+
Write-Host "$RED[error]$NC Please manually close the process and try again"
8384
Read-Host "Press Enter to exit"
8485
exit 1
8586
}
86-
Write-Host "$YELLOW[Warning]$NC Waiting for process closure, attempt $retryCount/$MAX_RETRIES..."
87+
Write-Host "$YELLOW[WARN]$NC Waiting for process to close, trying $retryCount/$MAX_RETRIES..."
8788
Start-Sleep -Seconds $WAIT_TIME
8889
}
89-
Write-Host "$GREEN[Info]$NC Successfully closed $processName."
90+
Write-Host "$GREEN[info]$NC $processName successfully closed"
9091
}
9192
}
9293

9394
# Close all Cursor processes
9495
Close-CursorProcess "Cursor"
9596
Close-CursorProcess "cursor"
9697

97-
# Create backup directory
98+
# Create a backup directory
9899
if (-not (Test-Path $BACKUP_DIR)) {
99100
New-Item -ItemType Directory -Path $BACKUP_DIR | Out-Null
100101
}
101102

102-
# Backup existing configuration
103+
# Back up the existing configuration
103104
if (Test-Path $STORAGE_FILE) {
104-
Write-Host "$GREEN[Info]$NC Backing up configuration file..."
105+
Write-Host "$GREEN[INFO]$NC Backing up configuration files..."
105106
$backupName = "storage.json.backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
106107
Copy-Item $STORAGE_FILE "$BACKUP_DIR\$backupName"
107108
}
108109

109-
# Generate new ID
110-
Write-Host "$GREEN[Info]$NC Generating new ID..."
110+
# Generate a new ID
111+
Write-Host "$GREEN[INFO]$NC Generating new ID..."
111112

113+
# Function to generate random byte array and convert to hexadecimal string
112114
function Get-RandomHex {
113115
param (
114116
[int]$length
@@ -121,49 +123,189 @@ function Get-RandomHex {
121123
}
122124

123125
$UUID = [System.Guid]::NewGuid().ToString()
126+
# Convert auth0|user_ to hexadecimal byte array
124127
$prefixBytes = [System.Text.Encoding]::UTF8.GetBytes("auth0|user_")
125128
$prefixHex = -join ($prefixBytes | ForEach-Object { '{0:x2}' -f $_ })
129+
# Generate a 32-byte (64 hexadecimal characters) random number as the random part of machineId
126130
$randomPart = Get-RandomHex -length 32
127131
$MACHINE_ID = "$prefixHex$randomPart"
128132
$MAC_MACHINE_ID = Get-RandomHex -length 32
129133
$SQM_ID = "{$([System.Guid]::NewGuid().ToString().ToUpper())}"
130134

131-
# Update configuration file
132-
Write-Host "$GREEN[Info]$NC Updating configuration..."
135+
# Create or update configuration files
136+
Write-Host "$GREEN[INFO]$NC Updating configuration..."
133137

134138
try {
139+
# Make sure the directory exists
135140
$storageDir = Split-Path $STORAGE_FILE -Parent
136141
if (-not (Test-Path $storageDir)) {
137142
New-Item -ItemType Directory -Path $storageDir -Force | Out-Null
138143
}
139144

145+
# Write configuration
140146
$config = @{
141147
'telemetry.machineId' = $MACHINE_ID
142148
'telemetry.macMachineId' = $MAC_MACHINE_ID
143149
'telemetry.devDeviceId' = $UUID
144150
'telemetry.sqmId' = $SQM_ID
145151
}
146152

147-
$jsonContent = $config | ConvertTo-Json
148-
[System.IO.File]::WriteAllText(
149-
[System.IO.Path]::GetFullPath($STORAGE_FILE),
150-
$jsonContent,
151-
[System.Text.Encoding]::UTF8
152-
)
153-
Write-Host "$GREEN[Info]$NC Configuration file updated successfully."
153+
# Use System.IO.File methods to write to a file
154+
try {
155+
$jsonContent = $config | ConvertTo-Json
156+
[System.IO.File]::WriteAllText(
157+
[System.IO.Path]::GetFullPath($STORAGE_FILE),
158+
$jsonContent,
159+
[System.Text.Encoding]::UTF8
160+
)
161+
Write-Host "$GREEN[INFO]$NC Successfully wrote configuration file"
162+
} catch {
163+
throw "Failed to write file: $_"
164+
}
165+
166+
# Try setting file permissions
167+
try {
168+
# Use the current username and domain name
169+
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
170+
$userAccount = "$($env:USERDOMAIN)\$($env:USERNAME)"
171+
172+
# Create a new access control list
173+
$acl = New-Object System.Security.AccessControl.FileSecurity
174+
175+
# Add full control permissions for the current user
176+
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
177+
$userAccount, # Use domain name\user name format
178+
[System.Security.AccessControl.FileSystemRights]::FullControl,
179+
[System.Security.AccessControl.InheritanceFlags]::None,
180+
[System.Security.AccessControl.PropagationFlags]::None,
181+
[System.Security.AccessControl.AccessControlType]::Allow
182+
)
183+
184+
try {
185+
$acl.AddAccessRule($accessRule)
186+
Set-Acl -Path $STORAGE_FILE -AclObject $acl -ErrorAction Stop
187+
Write-Host "$GREEN[INFO]$NC Successfully set file permissions"
188+
} catch {
189+
# If the first method fails, try icacls
190+
Write-Host "$YELLOW[WARNING]$NC Using alternate method to set permissions..."
191+
$result = Start-Process "icacls.exe" -ArgumentList "`"$STORAGE_FILE`" /grant `"$($env:USERNAME):(F)`"" -Wait -NoNewWindow -PassThru
192+
if ($result.ExitCode -eq 0) {
193+
Write-Host "$GREEN[INFO]$NC Successfully used icacls to set file permissions"
194+
} else {
195+
Write-Host "$YELLOW[Warning]$NC Failed to set file permissions, but the file was written successfully"
196+
}
197+
}
198+
} catch {
199+
Write-Host "$YELLOW[WARNING]$NC Failed to set file permissions: $_"
200+
Write-Host "$YELLOW[WARNING]$NC Attempting to use the icacls command..."
201+
try {
202+
$result = Start-Process "icacls.exe" -ArgumentList "`"$STORAGE_FILE`" /grant `"$($env:USERNAME):(F)`"" -Wait -NoNewWindow -PassThru
203+
if ($result.ExitCode -eq 0) {
204+
Write-Host "$GREEN[INFO]$NC Successfully used icacls to set file permissions"
205+
} else {
206+
Write-Host "$YELLOW[Warning]$NC All permission setting methods failed, but the file was written successfully"
207+
}
208+
} catch {
209+
Write-Host "$YELLOW[WARNING]$NC icacls command failed: $_"
210+
}
211+
}
212+
154213
} catch {
155-
Write-Host "$RED[Error]$NC Failed to update configuration: $_"
156-
Read-Host "Press Enter to exit"
157-
exit 1
214+
Write-Host "$RED[ERROR]$NC Primary operation failed: $_"
215+
Write-Host "$YELLOW[Try]$NC Use alternative method..."
216+
217+
try {
218+
# Alternative method: Use Add-Content
219+
$tempFile = [System.IO.Path]::GetTempFileName()
220+
$config | ConvertTo-Json | Set-Content -Path $tempFile -Encoding UTF8
221+
Copy-Item -Path $tempFile -Destination $STORAGE_FILE -Force
222+
Remove-Item -Path $tempFile
223+
Write-Host "$GREEN[INFO]$NC Successfully wrote configuration using alternative method"
224+
} catch {
225+
Write-Host "$RED[ERROR]$NC All attempts failed"
226+
Write-Host "Error Details: $_"
227+
Write-Host "Destination File: $STORAGE_FILE"
228+
Write-Host "Please make sure you have sufficient permissions to access the file"
229+
Read-Host "Press Enter to exit"
230+
exit 1
231+
}
232+
}
233+
234+
# Display results
235+
Write-Host ""
236+
Write-Host "$GREEN[INFO]$NC Updated configuration:"
237+
Write-Host "$BLUE[debug]$NC machineId:$MACHINE_ID"
238+
Write-Host "$BLUE[debug]$NC macMachineId:$MAC_MACHINE_ID"
239+
Write-Host "$BLUE[debug]$NC devDeviceId:$UUID"
240+
Write-Host "$BLUE[DEBUG]$NC sqmId: $SQM_ID"
241+
242+
# Display the file tree structure
243+
Write-Host ""
244+
Write-Host "$GREEN[INFO]$NC File Structure:"
245+
Write-Host "$BLUE$env:APPDATA\Cursor\User$NC"
246+
Write-Host "├── globalStorage"
247+
Write-Host "│ ├── storage.json (modified)"
248+
Write-Host "│ └── backups"
249+
250+
# List backup files
251+
$backupFiles = Get-ChildItem "$BACKUP_DIR\*" -ErrorAction SilentlyContinue
252+
if ($backupFiles) {
253+
foreach ($file in $backupFiles) {
254+
Write-Host "│ └── $($file.Name)"
255+
}
256+
} else {
257+
Write-Host "│ └── (empty)"
158258
}
159259

260+
# Display public account information
160261
Write-Host ""
161-
Write-Host "$GREEN[Info]$NC Configuration updated:"
162-
Write-Host "$BLUE[Debug]$NC machineId: $MACHINE_ID"
163-
Write-Host "$BLUE[Debug]$NC macMachineId: $MAC_MACHINE_ID"
164-
Write-Host "$BLUE[Debug]$NC devDeviceId: $UUID"
165-
Write-Host "$BLUE[Debug]$NC sqmId: $SQM_ID"
262+
Write-Host "$GREEN================================$NC"
263+
Write-Host "$YELLOW Follow the public account [Jianbing Guozijuan AI] to exchange more Cursor skills and AI knowledge $NC"
264+
Write-Host "$GREEN================================$NC"
265+
Write-Host ""
266+
Write-Host "$GREEN[info]$NC Please restart Cursor to apply the new configuration"
267+
Write-Host ""
268+
269+
# Ask if you want to disable automatic updates
270+
Write-Host ""
271+
Write-Host "$YELLOW[Ask]$NC Do you want to disable the automatic update feature of the Cursor?"
272+
Write-Host "0) No - Keep default settings (Press Enter)"
273+
Write-Host "1) Yes - Disable automatic updates"
274+
$choice = Read-Host "Please enter an option (1 or press Enter)"
275+
276+
if ($choice -eq "1") {
277+
Write-Host ""
278+
Write-Host "$GREEN[INFO]$NC Processing automatic updates..."
279+
$updaterPath = "$env:LOCALAPPDATA\cursor-updater"
280+
281+
if (Test-Path $updaterPath) {
282+
try {
283+
# Force delete directory
284+
Remove-Item -Path $updaterPath -Force -Recurse -ErrorAction Stop
285+
Write-Host "$GREEN[INFO]$NC Successfully deleted cursor-updater directory"
286+
287+
# Create a file with the same name
288+
New-Item -Path $updaterPath -ItemType File -Force | Out-Null
289+
Write-Host "$GREEN[INFO]$NC Successfully created block file"
290+
}
291+
catch {
292+
Write-Host "$RED[ERROR]$NC Error processing cursor-updater: $_"
293+
}
294+
}
295+
else {
296+
# Create a blocking file directly
297+
New-Item -Path $updaterPath -ItemType File -Force | Out-Null
298+
Write-Host "$GREEN[INFO]$NC Successfully created block file"
299+
}
300+
}
301+
elseif ($choice -ne "") {
302+
Write-Host "$YELLOW[info]$NC Keep the default settings and do not change"
303+
}
304+
else {
305+
Write-Host "$YELLOW[info]$NC Keep the default settings and do not change"
306+
}
307+
308+
166309

167-
Write-Host "$GREEN[Info]$NC Please restart Cursor to apply the new configuration."
168310
Read-Host "Press Enter to exit"
169311
exit 0

0 commit comments

Comments
 (0)