-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualStudioCode+Auto-Setup(Run_Admin).ps1
93 lines (80 loc) · 3.1 KB
/
VisualStudioCode+Auto-Setup(Run_Admin).ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Function to check if the script is running as an administrator
function Test-IsAdmin {
$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Function to re-run the script with elevated privileges
function Run-AsAdmin {
if (-not (Test-IsAdmin)) {
$scriptPath = $MyInvocation.MyCommand.Path
Start-Process powershell -ArgumentList "-File `"$scriptPath`"" -Verb RunAs
exit
}
}
# Check and re-run as admin if necessary
Run-AsAdmin
# Define the URL for the VS Code installer
$url = "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-user"
# Define the path where the installer will be downloaded
$installerPath = "$env:TEMP\VSCodeSetup.exe"
# Define the path to the VS Code executable
$vsCodePath = "C:\Program Files\Microsoft VS Code\bin"
# Define the path to the log file
$logFilePath = "$env:TEMP\VSCodeInstallLog.txt"
# Function to log messages
function Log-Message {
param (
[string]$message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp - $message"
Write-Host $logEntry
Add-Content -Path $logFilePath -Value $logEntry
}
# Download the installer
Log-Message "Downloading Visual Studio Code installer..."
try {
Invoke-WebRequest -Uri $url -OutFile $installerPath -ErrorAction Stop
Log-Message "Download completed."
} catch {
Log-Message "Failed to download Visual Studio Code installer: $_"
exit 1
}
# Run the installer with system-level installation
Log-Message "Running the Visual Studio Code installer..."
try {
Start-Process -FilePath $installerPath -ArgumentList "/verysilent", "/mergetasks=!runcode" -Wait -ErrorAction Stop
Log-Message "Installation completed."
} catch {
Log-Message "Failed to run the Visual Studio Code installer: $_"
exit 1
}
# Check if VS Code was installed successfully
if (-Not (Test-Path "$vsCodePath\code.cmd")) {
Log-Message "Visual Studio Code installation failed or VS Code executable not found."
exit 1
}
# Add VS Code to the system PATH
Log-Message "Adding Visual Studio Code to the system PATH..."
try {
$oldPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if ($oldPath -notlike "*$vsCodePath*") {
$newPath = "$oldPath;$vsCodePath"
[Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::Machine)
Log-Message "Visual Studio Code path added to system PATH."
} else {
Log-Message "Visual Studio Code path already exists in system PATH."
}
} catch {
Log-Message "Failed to update system PATH: $_"
exit 1
}
# Clean up the installer file
Log-Message "Cleaning up..."
try {
Remove-Item -Path $installerPath -ErrorAction Stop
Log-Message "Cleanup completed."
} catch {
Log-Message "Failed to clean up the installer file: $_"
}
Log-Message "Visual Studio Code installation completed."