-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.ps1
574 lines (472 loc) · 19.7 KB
/
install.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<#
.SYNOPSIS
Set File Type Association and Update PATH for aria2win.exe
.DESCRIPTION
Set File Type Association Default Application and Update PATH for aria2win.exe
Should work on Windows 8/10/11
Based on https://github.com/DanysysTeam/PS-SFTA and https://stackoverflow.com/a/69239861
#>
function Register-FTA {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[ValidateScript({ Test-Path $_ })]
[String]
$ProgramPath,
[Parameter(Position = 1, Mandatory = $false)]
[String]
$ProgId
)
Write-Verbose "Register Application + Set Association"
Write-Verbose "Application Path: $ProgramPath"
if (!$ProgId) {
$ProgId = "SFTA." + [System.IO.Path]::GetFileNameWithoutExtension($ProgramPath).replace(" ", "") + $Extension
}
$progCommand = """$ProgramPath"" ""%1"""
Write-Verbose "ApplicationId: $ProgId"
Write-Verbose "ApplicationCommand: $progCommand"
try {
$keyPath = "HKEY_CURRENT_USER\SOFTWARE\Classes\$ProgId\shell\open\command"
[Microsoft.Win32.Registry]::SetValue($keyPath, "", $progCommand)
Write-Verbose "Register ProgId and ProgId Command OK"
}
catch {
throw "Register ProgId and ProgId Command FAILED"
}
}
function Set-FTA {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String]
$ProgId,
[Parameter(Mandatory = $true)]
[Alias("Protocol")]
[String]
$Extension,
[String]
$Icon,
[switch]
$DomainSID
)
if (Test-Path -Path $ProgId) {
$ProgId = "SFTA." + [System.IO.Path]::GetFileNameWithoutExtension($ProgId).replace(" ", "") + $Extension
}
Write-Verbose "ProgId: $ProgId"
Write-Verbose "Extension/Protocol: $Extension"
#Write required Application Ids to ApplicationAssociationToasts
#When more than one application associated with an Extension/Protocol is installed ApplicationAssociationToasts need to be updated
function local:Write-RequiredApplicationAssociationToasts {
param (
[Parameter(Position = 0, Mandatory = $true)]
[String]
$ProgId,
[Parameter(Position = 1, Mandatory = $true)]
[String]
$Extension
)
try {
$keyPath = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts"
[Microsoft.Win32.Registry]::SetValue($keyPath, $ProgId + "_" + $Extension, 0x0)
Write-Verbose ("Write Reg ApplicationAssociationToasts OK: " + $ProgId + "_" + $Extension)
}
catch {
Write-Verbose ("Write Reg ApplicationAssociationToasts FAILED: " + $ProgId + "_" + $Extension)
}
$allApplicationAssociationToasts = Get-ChildItem -Path HKLM:\SOFTWARE\Classes\$Extension\OpenWithList\* -ErrorAction SilentlyContinue |
ForEach-Object {
"Applications\$($_.PSChildName)"
}
$allApplicationAssociationToasts += @(
ForEach ($item in (Get-ItemProperty -Path HKLM:\SOFTWARE\Classes\$Extension\OpenWithProgids -ErrorAction SilentlyContinue).PSObject.Properties ) {
if ([string]::IsNullOrEmpty($item.Value) -and $item -ne "(default)") {
$item.Name
}
})
$allApplicationAssociationToasts += Get-ChildItem -Path HKLM:SOFTWARE\Clients\StartMenuInternet\* , HKCU:SOFTWARE\Clients\StartMenuInternet\* -ErrorAction SilentlyContinue |
ForEach-Object {
(Get-ItemProperty ("$($_.PSPath)\Capabilities\" + (@("URLAssociations", "FileAssociations") | Select-Object -Index $Extension.Contains("."))) -ErrorAction SilentlyContinue).$Extension
}
$allApplicationAssociationToasts |
ForEach-Object { if ($_) {
if (Set-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts $_"_"$Extension -Value 0 -Type DWord -ErrorAction SilentlyContinue -PassThru) {
Write-Verbose ("Write Reg ApplicationAssociationToastsList OK: " + $_ + "_" + $Extension)
}
else {
Write-Verbose ("Write Reg ApplicationAssociationToastsList FAILED: " + $_ + "_" + $Extension)
}
}
}
}
function local:Update-RegistryChanges {
$code = @'
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
public static void Refresh() {
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
}
'@
try {
Add-Type -MemberDefinition $code -Namespace SHChange -Name Notify
}
catch {}
try {
[SHChange.Notify]::Refresh()
}
catch {}
}
function local:Set-Icon {
param (
[Parameter(Position = 0, Mandatory = $true)]
[String]
$ProgId,
[Parameter(Position = 1, Mandatory = $true)]
[String]
$Icon
)
try {
$keyPath = "HKEY_CURRENT_USER\SOFTWARE\Classes\$ProgId\DefaultIcon"
[Microsoft.Win32.Registry]::SetValue($keyPath, "", $Icon)
Write-Verbose "Write Reg Icon OK"
Write-Verbose "Reg Icon: $keyPath"
}
catch {
Write-Verbose "Write Reg Icon FAILED"
}
}
function local:Write-ExtensionKeys {
param (
[Parameter(Position = 0, Mandatory = $true)]
[String]
$ProgId,
[Parameter(Position = 1, Mandatory = $true)]
[String]
$Extension,
[Parameter(Position = 2, Mandatory = $true)]
[String]
$ProgHash
)
function local:Remove-UserChoiceKey {
param (
[Parameter(Position = 0, Mandatory = $true)]
[String]
$Key
)
$code = @'
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Registry {
public class Utils {
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult);
[DllImport("advapi32.dll", SetLastError=true, CharSet = CharSet.Unicode)]
private static extern uint RegDeleteKey(UIntPtr hKey, string subKey);
public static void DeleteKey(string key) {
UIntPtr hKey = UIntPtr.Zero;
RegOpenKeyEx((UIntPtr)0x80000001u, key, 0, 0x20019, out hKey);
RegDeleteKey((UIntPtr)0x80000001u, key);
}
}
}
'@
try {
Add-Type -TypeDefinition $code
}
catch {}
try {
[Registry.Utils]::DeleteKey($Key)
}
catch {}
}
try {
$keyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice"
Write-Verbose "Remove Extension UserChoice Key If Exist: $keyPath"
Remove-UserChoiceKey $keyPath
}
catch {
Write-Verbose "Extension UserChoice Key No Exist: $keyPath"
}
try {
$keyPath = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice"
[Microsoft.Win32.Registry]::SetValue($keyPath, "Hash", $ProgHash)
[Microsoft.Win32.Registry]::SetValue($keyPath, "ProgId", $ProgId)
Write-Verbose "Write Reg Extension UserChoice OK"
}
catch {
throw "Write Reg Extension UserChoice FAILED"
}
}
function local:Write-ProtocolKeys {
param (
[Parameter(Position = 0, Mandatory = $true)]
[String]
$ProgId,
[Parameter(Position = 1, Mandatory = $true)]
[String]
$Protocol,
[Parameter(Position = 2, Mandatory = $true)]
[String]
$ProgHash
)
try {
$keyPath = "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\$Protocol\UserChoice"
Write-Verbose "Remove Protocol UserChoice Key If Exist: $keyPath"
Remove-Item -Path $keyPath -Recurse -ErrorAction Stop | Out-Null
}
catch {
Write-Verbose "Protocol UserChoice Key No Exist: $keyPath"
}
try {
$keyPath = "HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\$Protocol\UserChoice"
[Microsoft.Win32.Registry]::SetValue($keyPath, "Hash", $ProgHash)
[Microsoft.Win32.Registry]::SetValue($keyPath, "ProgId", $ProgId)
Write-Verbose "Write Reg Protocol UserChoice OK"
}
catch {
throw "Write Reg Protocol UserChoice FAILED"
}
}
function local:Get-UserExperience {
[OutputType([string])]
$hardcodedExperience = "User Choice set via Windows User Experience {D18B6DD5-6124-4341-9318-804003BAFA0B}"
$userExperienceSearch = "User Choice set via Windows User Experience"
$userExperienceString = ""
$user32Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::SystemX86) + "\Shell32.dll"
$fileStream = [System.IO.File]::Open($user32Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
$binaryReader = New-Object System.IO.BinaryReader($fileStream)
[Byte[]] $bytesData = $binaryReader.ReadBytes(5mb)
$fileStream.Close()
$dataString = [Text.Encoding]::Unicode.GetString($bytesData)
$position1 = $dataString.IndexOf($userExperienceSearch)
$position2 = $dataString.IndexOf("}", $position1)
try {
$userExperienceString = $dataString.Substring($position1, $position2 - $position1 + 1)
}
catch {
$userExperienceString = $hardcodedExperience
}
Write-Output $userExperienceString
}
function local:Get-UserSid {
[OutputType([string])]
$userSid = ((New-Object System.Security.Principal.NTAccount([Environment]::UserName)).Translate([System.Security.Principal.SecurityIdentifier]).value).ToLower()
Write-Output $userSid
}
#use in this special case
#https://github.com/DanysysTeam/PS-SFTA/pull/7
function local:Get-UserSidDomain {
if (-not ("System.DirectoryServices.AccountManagement" -as [type])) {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
[OutputType([string])]
$userSid = ([System.DirectoryServices.AccountManagement.UserPrincipal]::Current).SID.Value.ToLower()
Write-Output $userSid
}
function local:Get-HexDateTime {
[OutputType([string])]
$now = [DateTime]::Now
$dateTime = [DateTime]::New($now.Year, $now.Month, $now.Day, $now.Hour, $now.Minute, 0)
$fileTime = $dateTime.ToFileTime()
$hi = ($fileTime -shr 32)
$low = ($fileTime -band 0xFFFFFFFFL)
$dateTimeHex = ($hi.ToString("X8") + $low.ToString("X8")).ToLower()
Write-Output $dateTimeHex
}
function Get-Hash {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[string]
$BaseInfo
)
function local:Get-ShiftRight {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[long] $iValue,
[Parameter(Position = 1, Mandatory = $true)]
[int] $iCount
)
if ($iValue -band 0x80000000) {
Write-Output (($iValue -shr $iCount) -bxor 0xFFFF0000)
}
else {
Write-Output ($iValue -shr $iCount)
}
}
function local:Get-Long {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[byte[]] $Bytes,
[Parameter(Position = 1)]
[int] $Index = 0
)
Write-Output ([BitConverter]::ToInt32($Bytes, $Index))
}
function local:Convert-Int32 {
param (
[Parameter(Position = 0, Mandatory = $true)]
[long] $Value
)
[byte[]] $bytes = [BitConverter]::GetBytes($Value)
return [BitConverter]::ToInt32($bytes, 0)
}
[Byte[]] $bytesBaseInfo = [System.Text.Encoding]::Unicode.GetBytes($baseInfo)
$bytesBaseInfo += 0x00, 0x00
$MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
[Byte[]] $bytesMD5 = $MD5.ComputeHash($bytesBaseInfo)
$lengthBase = ($baseInfo.Length * 2) + 2
$length = (($lengthBase -band 4) -le 1) + (Get-ShiftRight $lengthBase 2) - 1
$base64Hash = ""
if ($length -gt 1) {
$map = @{PDATA = 0; CACHE = 0; COUNTER = 0 ; INDEX = 0; MD51 = 0; MD52 = 0; OUTHASH1 = 0; OUTHASH2 = 0;
R0 = 0; R1 = @(0, 0); R2 = @(0, 0); R3 = 0; R4 = @(0, 0); R5 = @(0, 0); R6 = @(0, 0); R7 = @(0, 0)
}
$map.CACHE = 0
$map.OUTHASH1 = 0
$map.PDATA = 0
$map.MD51 = (((Get-Long $bytesMD5) -bor 1) + 0x69FB0000L)
$map.MD52 = ((Get-Long $bytesMD5 4) -bor 1) + 0x13DB0000L
$map.INDEX = Get-ShiftRight ($length - 2) 1
$map.COUNTER = $map.INDEX + 1
while ($map.COUNTER) {
$map.R0 = Convert-Int32 ((Get-Long $bytesBaseInfo $map.PDATA) + [long]$map.OUTHASH1)
$map.R1[0] = Convert-Int32 (Get-Long $bytesBaseInfo ($map.PDATA + 4))
$map.PDATA = $map.PDATA + 8
$map.R2[0] = Convert-Int32 (($map.R0 * ([long]$map.MD51)) - (0x10FA9605L * ((Get-ShiftRight $map.R0 16))))
$map.R2[1] = Convert-Int32 ((0x79F8A395L * ([long]$map.R2[0])) + (0x689B6B9FL * (Get-ShiftRight $map.R2[0] 16)))
$map.R3 = Convert-Int32 ((0xEA970001L * $map.R2[1]) - (0x3C101569L * (Get-ShiftRight $map.R2[1] 16) ))
$map.R4[0] = Convert-Int32 ($map.R3 + $map.R1[0])
$map.R5[0] = Convert-Int32 ($map.CACHE + $map.R3)
$map.R6[0] = Convert-Int32 (($map.R4[0] * [long]$map.MD52) - (0x3CE8EC25L * (Get-ShiftRight $map.R4[0] 16)))
$map.R6[1] = Convert-Int32 ((0x59C3AF2DL * $map.R6[0]) - (0x2232E0F1L * (Get-ShiftRight $map.R6[0] 16)))
$map.OUTHASH1 = Convert-Int32 ((0x1EC90001L * $map.R6[1]) + (0x35BD1EC9L * (Get-ShiftRight $map.R6[1] 16)))
$map.OUTHASH2 = Convert-Int32 ([long]$map.R5[0] + [long]$map.OUTHASH1)
$map.CACHE = ([long]$map.OUTHASH2)
$map.COUNTER = $map.COUNTER - 1
}
[Byte[]] $outHash = @(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
[byte[]] $buffer = [BitConverter]::GetBytes($map.OUTHASH1)
$buffer.CopyTo($outHash, 0)
$buffer = [BitConverter]::GetBytes($map.OUTHASH2)
$buffer.CopyTo($outHash, 4)
$map = @{PDATA = 0; CACHE = 0; COUNTER = 0 ; INDEX = 0; MD51 = 0; MD52 = 0; OUTHASH1 = 0; OUTHASH2 = 0;
R0 = 0; R1 = @(0, 0); R2 = @(0, 0); R3 = 0; R4 = @(0, 0); R5 = @(0, 0); R6 = @(0, 0); R7 = @(0, 0)
}
$map.CACHE = 0
$map.OUTHASH1 = 0
$map.PDATA = 0
$map.MD51 = ((Get-Long $bytesMD5) -bor 1)
$map.MD52 = ((Get-Long $bytesMD5 4) -bor 1)
$map.INDEX = Get-ShiftRight ($length - 2) 1
$map.COUNTER = $map.INDEX + 1
while ($map.COUNTER) {
$map.R0 = Convert-Int32 ((Get-Long $bytesBaseInfo $map.PDATA) + ([long]$map.OUTHASH1))
$map.PDATA = $map.PDATA + 8
$map.R1[0] = Convert-Int32 ($map.R0 * [long]$map.MD51)
$map.R1[1] = Convert-Int32 ((0xB1110000L * $map.R1[0]) - (0x30674EEFL * (Get-ShiftRight $map.R1[0] 16)))
$map.R2[0] = Convert-Int32 ((0x5B9F0000L * $map.R1[1]) - (0x78F7A461L * (Get-ShiftRight $map.R1[1] 16)))
$map.R2[1] = Convert-Int32 ((0x12CEB96DL * (Get-ShiftRight $map.R2[0] 16)) - (0x46930000L * $map.R2[0]))
$map.R3 = Convert-Int32 ((0x1D830000L * $map.R2[1]) + (0x257E1D83L * (Get-ShiftRight $map.R2[1] 16)))
$map.R4[0] = Convert-Int32 ([long]$map.MD52 * ([long]$map.R3 + (Get-Long $bytesBaseInfo ($map.PDATA - 4))))
$map.R4[1] = Convert-Int32 ((0x16F50000L * $map.R4[0]) - (0x5D8BE90BL * (Get-ShiftRight $map.R4[0] 16)))
$map.R5[0] = Convert-Int32 ((0x96FF0000L * $map.R4[1]) - (0x2C7C6901L * (Get-ShiftRight $map.R4[1] 16)))
$map.R5[1] = Convert-Int32 ((0x2B890000L * $map.R5[0]) + (0x7C932B89L * (Get-ShiftRight $map.R5[0] 16)))
$map.OUTHASH1 = Convert-Int32 ((0x9F690000L * $map.R5[1]) - (0x405B6097L * (Get-ShiftRight ($map.R5[1]) 16)))
$map.OUTHASH2 = Convert-Int32 ([long]$map.OUTHASH1 + $map.CACHE + $map.R3)
$map.CACHE = ([long]$map.OUTHASH2)
$map.COUNTER = $map.COUNTER - 1
}
$buffer = [BitConverter]::GetBytes($map.OUTHASH1)
$buffer.CopyTo($outHash, 8)
$buffer = [BitConverter]::GetBytes($map.OUTHASH2)
$buffer.CopyTo($outHash, 12)
[Byte[]] $outHashBase = @(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
$hashValue1 = ((Get-Long $outHash 8) -bxor (Get-Long $outHash))
$hashValue2 = ((Get-Long $outHash 12) -bxor (Get-Long $outHash 4))
$buffer = [BitConverter]::GetBytes($hashValue1)
$buffer.CopyTo($outHashBase, 0)
$buffer = [BitConverter]::GetBytes($hashValue2)
$buffer.CopyTo($outHashBase, 4)
$base64Hash = [Convert]::ToBase64String($outHashBase)
}
Write-Output $base64Hash
}
Write-Verbose "Getting Hash For $ProgId $Extension"
If ($DomainSID.IsPresent) { Write-Verbose "Use Get-UserSidDomain" } Else { Write-Verbose "Use Get-UserSid" }
$userSid = If ($DomainSID.IsPresent) { Get-UserSidDomain } Else { Get-UserSid }
$userExperience = Get-UserExperience
$userDateTime = Get-HexDateTime
Write-Debug "UserDateTime: $userDateTime"
Write-Debug "UserSid: $userSid"
Write-Debug "UserExperience: $userExperience"
$baseInfo = "$Extension$userSid$ProgId$userDateTime$userExperience".ToLower()
Write-Verbose "baseInfo: $baseInfo"
$progHash = Get-Hash $baseInfo
Write-Verbose "Hash: $progHash"
#Write AssociationToasts List
Write-RequiredApplicationAssociationToasts $ProgId $Extension
#Handle Extension Or Protocol
if ($Extension.Contains(".")) {
Write-Verbose "Write Registry Extension: $Extension"
Write-ExtensionKeys $ProgId $Extension $progHash
}
else {
Write-Verbose "Write Registry Protocol: $Extension"
Write-ProtocolKeys $ProgId $Extension $progHash
}
if ($Icon) {
Write-Verbose "Set Icon: $Icon"
Set-Icon $ProgId $Icon
}
Update-RegistryChanges
}
function Add-Path {
param(
[Parameter(Mandatory, Position=0)]
[string] $LiteralPath,
[ValidateSet('User', 'CurrentUser', 'Machine', 'LocalMachine')]
[string] $Scope
)
Set-StrictMode -Version 1; $ErrorActionPreference = 'Stop'
$isMachineLevel = $Scope -in 'Machine', 'LocalMachine'
if ($isMachineLevel -and -not $($ErrorActionPreference = 'Continue'; net session 2>$null)) { throw "You must run AS ADMIN to update the machine-level Path environment variable." }
$regPath = 'registry::' + ('HKEY_CURRENT_USER\Environment', 'HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment')[$isMachineLevel]
# Note the use of the .GetValue() method to ensure that the *unexpanded* value is returned.
$currDirs = (Get-Item -LiteralPath $regPath).GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split ';' -ne ''
if ($LiteralPath -in $currDirs) {
Write-Verbose "Already present in the persistent $(('user', 'machine')[$isMachineLevel])-level Path: $LiteralPath"
return
}
$newValue = ($currDirs + $LiteralPath) -join ';'
# Update the registry.
Set-ItemProperty -Type ExpandString -LiteralPath $regPath Path $newValue
# Broadcast WM_SETTINGCHANGE to get the Windows shell to reload the
# updated environment, via a dummy [Environment]::SetEnvironmentVariable() operation.
$dummyName = [guid]::NewGuid().ToString()
[Environment]::SetEnvironmentVariable($dummyName, 'foo', 'User')
[Environment]::SetEnvironmentVariable($dummyName, [NullString]::value, 'User')
# Finally, also update the current session's `$env:Path` definition.
# Note: For simplicity, we always append to the in-process *composite* value,
# even though for a -Scope Machine update this isn't strictly the same.
$env:Path = ($env:Path -replace ';$') + ';' + $LiteralPath
Write-Verbose "`"$LiteralPath`" successfully appended to the persistent $(('user', 'machine')[$isMachineLevel])-level Path and also the current-process value."
}
# Get exe path
$ProgramPath = Resolve-Path -Path "./aria2win.exe"
$ProgId = "Application/aria2win.exe"
# Register and set for .torrent
Register-FTA $ProgramPath $ProgId
Write-Output "Register Success"
# Set for .meta4
Set-FTA -ProgId $ProgId -Extension .meta4
Set-FTA -ProgId $ProgId -Extension .torrent
Write-Output "Set FileType Association Success"
# Add current dir to $PATH
$ProgramDir = Resolve-Path -Path "."
Add-Path $ProgramDir
Write-Output "Update PATH Success"
# Wait to exit
Read-Host -Prompt "Press any key to exit"