-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall_VSCode.ps1
161 lines (136 loc) · 5.06 KB
/
Install_VSCode.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Param
(
[Parameter(Mandatory = $false)]
[Hashtable]$DynParameters
)
#region functions
function Write-Log {
<#
.SYNOPSIS
Creates a log file and stores logs based on categories with tab seperation
.PARAMETER category
Category to put into the trace
.PARAMETER message
Message to be loged
.EXAMPLE
Log 'Info' 'Message'
#>
Param (
[Parameter(Mandatory=$false, Position=0)]
[ValidateSet("Info","Warning","Error")]
$category = 'Info',
[Parameter(Mandatory=$true, Position=1)]
$message
)
$date = get-date
$content = "[$date]`t$category`t`t$message`n"
Write-Verbose "$Script:Name $content" -verbose
if (! $script:Log) {
$File = Join-Path $env:TEMP "log.log"
$File = Join-Path -Path $env:TEMP -ChildPath "$Script:Name.log"
Write-Warning "Log file not found, create new $File"
}
else {
$File = $script:Log
}
Add-Content $File $content -ErrorAction Stop
}
function New-Log {
<#
.SYNOPSIS
Sets default log file and stores in a script accessible variable $script:Log
Log File name "packageExecution_$date.log"
.PARAMETER Path
Path to the log file
.EXAMPLE
New-Log c:\Windows\Logs
Create a new log file in c:\Windows\Logs
#>
Param (
[Parameter(Mandatory = $true, Position=0)]
[string] $Path
)
# Create central log file with given date
$date = Get-Date -UFormat "%Y-%m-%d %H-%M-%S"
Set-Variable logFile -Scope Script
$script:logFile = "$Script:Name-$date.log"
if ((Test-Path $path ) -eq $false) {
$null = New-Item -Path $path -type directory
}
$script:Log = Join-Path $path $logfile
Add-Content $script:Log "Date`t`t`tCategory`t`tDetails"
}
Function Set-RegistryValue {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullorEmpty()]
[string]$Key,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory = $true)]
$Value,
[Parameter(Mandatory = $false)]
[ValidateSet('Binary', 'DWord', 'ExpandString', 'MultiString', 'None', 'QWord', 'String', 'Unknown')]
[Microsoft.Win32.RegistryValueKind]$Type = 'String'
)
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
If (-not (Get-ItemProperty -LiteralPath $key -Name $Name -ErrorAction 'SilentlyContinue')) {
If (-not (Test-Path -LiteralPath $key -ErrorAction 'Stop')) {
Try {
Write-Log -Info -Message "${CmdletName}: Create registry key [$key]."
# No forward slash found in Key. Use New-Item cmdlet to create registry key
If ((($Key -split '/').Count - 1) -eq 0) {
$null = New-Item -Path $key -ItemType 'Registry' -Force -ErrorAction 'Stop'
}
# Forward slash was found in Key. Use REG.exe ADD to create registry key
Else {
$null = & reg.exe Add "$($Key.Substring($Key.IndexOf('::') + 2))"
If ($global:LastExitCode -ne 0) {
Throw "Failed to create registry key [$Key]"
}
}
}
Catch {
Throw
}
}
Write-Log -category Info -Message "${CmdletName}: Set registry key value: [$key] [$name = $value]."
$null = New-ItemProperty -LiteralPath $key -Name $name -Value $value -PropertyType $Type -ErrorAction 'Stop'
}
## Update registry value if it does exist
Else {
If ($Name -eq '(Default)') {
## Set Default registry key value with the following workaround, because Set-ItemProperty contains a bug and cannot set Default registry key value
$null = $(Get-Item -LiteralPath $key -ErrorAction 'Stop').OpenSubKey('', 'ReadWriteSubTree').SetValue($null, $value)
}
Else {
Write-Log -category Info -Message "${CmdletName}: Update registry key value: [$key] [$name = $value]."
$null = Set-ItemProperty -LiteralPath $key -Name $name -Value $value -ErrorAction 'Stop'
}
}
}
#endregion Functions
#region Initialization
$Script:Name = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath)
New-Log "C:\Windows\Logs"
$ErrorActionPreference = 'Stop'
Write-Log -category Info -message "Starting '$PSCommandPath'."
#endregion
#region Install
$exefiles = Get-ChildItem -Path "$PSScriptRoot" -File -Filter '*.exe'
$VSCodeExe = $exefiles[0].FullName
Write-Log -message "Starting installation of VSCode."
$Arguments = "/VERYSILENT /NORESTART /MERGETASKS=!runcode"
Write-Log -message "Executing '$VSCodeexec $Arguments'"
$Installer = Start-Process -FilePath "$VSCodeExe" -ArgumentList $Arguments -Wait -PassThru
If ($($Installer.ExitCode) -eq 0 ) {
Write-Log -message "'VSCode' installed successfully."
}
Else {
Write-Log -category Error -message "The exit code is $($Installer.ExitCode)"
}
#endregion Install
Write-Log -Message "Ending '$PSCommandPath'."
Exit $($Installer.ExitCode)