forked from microsoft/WindowsAppSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevCheck.ps1
637 lines (550 loc) · 17.1 KB
/
DevCheck.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# Make a valid AppxManifest.xml from a templated file and variable substititon
<#
.SYNOPSIS
Verify the environment for Windows App SDK development
.DESCRIPTION
Review the current environment and fix or warn if anything is amiss. This includes...
* TAEF service is installed and running
* Test certificate to sign test MSIX packages is installed
* Visual Studio 2022 is installed and properly configured
.PARAMETER CertPassword
Password for new certificates
.PARAMETER Clean
Run as if it's the first time (ignore any previous cached artifacts from previous runs).
.PARAMETER CheckAll
Check all. If not specified this is set to true if all other -Check... options are false
.PARAMETER CheckTAEFService
Check the TAEF service
.PARAMETER CheckTestCert
Check the Test certificate
.PARAMETER CheckTestPfx
Check the MSIX Test signing certificate (to sign developer test MSIXs)
.PARAMETER CheckVisualStudio
Check Visual Studio
.PARAMETER NoInteractive
Run in non-interactive mode (fail if any need for user input)
.PARAMETER Offline
Do not access the network
.PARAMETER RemoveAll
Remove all.
.PARAMETER RemoveTestCert
Remove the Test certificate (i.e. undoc CheckTestCert)
.PARAMETER RemoveTestPfx
Remove the MSIX Test signing certificate (i.e. undoc CheckTestPfx)
.PARAMETER Verbose
Display detailed information
.EXAMPLE
DevCheck -Verbose
#>
Param(
[String]$CertPassword=$null,
[String]$CertPasswordFile=$null,
[String]$CertPasswordUser=$true,
[Switch]$CheckAll=$false,
[Switch]$CheckTAEFService=$false,
[Switch]$CheckTestCert=$false,
[Switch]$CheckTestPfx=$false,
[Switch]$CheckVisualStudio=$false,
[Switch]$Clean=$false,
[Switch]$NoInteractive=$false,
[Switch]$Offline=$false,
[Switch]$RemoveAll=$false,
[Switch]$RemoveTestCert=$false,
[Switch]$RemoveTestPfx=$false,
[Switch]$Verbose=$false
)
$global:issues = 0
$remove_any = ($RemoveAll -eq $true) -or ($RemoveTestCert -eq $true) -or ($RemoveTestCert -eq $true)
if (($remove_any -eq $false) -And ($CheckTAEFService -eq $false) -And ($CheckTestCert -eq $false) -And ($CheckTestPfx -eq $false) -And ($CheckVisualStudio -eq $false))
{
$CheckAll = $true
}
function Write-Verbose
{
param(
[Parameter(Mandatory=$true)]
[string]$Message
)
if ($Verbose -eq $true)
{
Write-Host $Message
}
}
function Get-IsAdmin
{
return [Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544'
}
function Get-ProjectRoot
{
return $PSScriptRoot
}
function Get-TempPath
{
$root = Get-ProjectRoot
$temp = Join-Path $root 'temp'
if (-not(Test-Path -Path $temp -PathType Container))
{
Write-Host "Creating $temp..."
New-Item -Path $temp -ItemType Directory -Force
}
return $temp
}
function Get-UserPath
{
$root = Get-ProjectRoot
$user = Join-Path $root '.user'
if (-not(Test-Path -Path $user -PathType Container))
{
Write-Host "Creating $user..."
$null = New-Item -Path $user -ItemType Directory -Force
}
return $user
}
function Get-CpuArchitecture
{
$architecture = $(Get-WmiObject Win32_Processor).Architecture
if ($architecture -eq 0)
{
return "x86"
}
elseif ($architecture -eq 9)
{
return "x64"
}
elseif ($architecture -eq 12)
{
return "arm64"
}
else
{
throw "Unknown CPU Architecture $architecture"
}
}
# Home of vswhere.exe: https://github.com/microsoft/vswhere
$vswhere = ''
$vswhere_url = ''
function Get-VSWhere
{
if ([string]::IsNullOrEmpty($global:vswhere))
{
Write-Verbose "Detecting vswhere.exe..."
$vswhere = Join-Path $env:TEMP 'vswhere.exe'
if ($Clean -eq $true -And (Test-Path -Path $vswhere -PathType Leaf))
{
Write-Verbose "Found $vswhere. Deleting per -Clean..."
Remove-Item -Path $vswhere -Force
}
if (-not(Test-Path -Path $vswhere -PathType Leaf))
{
if ($Offline -eq $true)
{
Write-Host "vswhere.exe not detected"
$global:issues += 1
return
}
$vswhere_url = 'https://github.com/microsoft/vswhere/releases/download/2.8.4/vswhere.exe'
Write-Host "Downloading $vswhere from $vswhere_url..."
Write-Verbose "Executing: curl.exe --output $vswhere -L -# $vswhere_url"
$p = Start-Process curl.exe -ArgumentList "--output $vswhere -L -# $vswhere_url" -Wait -NoNewWindow -PassThru
}
Write-Verbose "Using $vswhere"
$global:vswhere = $vswhere
}
return $global:vswhere
}
function Run-Process([string]$exe, [string]$arguments, [Ref][string]$stderr)
{
$pi = New-Object System.Diagnostics.ProcessStartInfo
$pi.FileName = $exe
if (-not([string]::IsNullOrEmpty($arguments)))
{
$pi.Arguments = $arguments
}
$pi.CreateNoWindow = $true
$pi.RedirectStandardError = $true
$pi.RedirectStandardOutput = $true
$pi.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pi
$ok = $p.Start()
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
return $stdout
}
$vspath = ''
function Get-VisualStudio2022InstallPath
{
if ([string]::IsNullOrEmpty($global:vspath))
{
Write-Verbose "Detecting VisualStudio 2022..."
$vswhere = Get-VSWhere
$args = " -latest -products * -version [17.0,18.0) -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath"
Write-Verbose "Executing $vswhere $args"
$path = Run-Process $vswhere $args
$path = $path -replace [environment]::NewLine, ''
Write-Verbose "Visual Studio 2022 detected at $path"
$global:vspath = $path
}
return $global:vspath
}
function Test-VisualStudio2022Install
{
$path = Get-VisualStudio2022InstallPath
if ([string]::IsNullOrEmpty($path))
{
$global:issues++
}
Write-Host "VisualStudio 2022...$path"
}
function Test-DevTestPfx
{
if ($Clean -eq $true)
{
return $false
}
$user = Get-UserPath
$pfx_thumbprint = Join-Path $user 'winappsdk.certificate.test.thumbprint'
if (-not(Test-Path -Path $pfx_thumbprint -PathType Leaf))
{
Write-Host "Test certificate thumbprint $pfx_thumbprint...Not Found"
$global:issues += 1
return $false
}
$thumbprint = Get-Content -Path $pfx_thumbprint -Encoding utf8
$cert_path = "cert:\LocalMachine\TrustedPeople\$thumbprint"
if (-not(Test-Path -Path $cert_path))
{
Write-Host "Test certificate for $pfx_thumbprint...Not Found"
$global:issues += 1
return $false
}
$cert = Get-ChildItem $cert_path
$expiration = $cert.NotAfter
$now = Get-Date
if ($expiration -lt $now)
{
Write-Host "Test certificate for $pfx_thumbprint...Expired ($expiration)"
$global:issues += 1
return $false
}
elseif ($expiration -lt ($now + (New-TimeSpan -Days 14)))
{
Write-Host "Test certificate for $pfx_thumbprint...Expires soon ($expiration)"
$global:issues += 1
return $true
}
Write-Host "Test certificate for $pfx_thumbprint...OK"
return $true
}
function Repair-DevTestPfx
{
$isadmin = Get-IsAdmin
if ($isadmin -eq $false)
{
Write-Host "Test certificate .pfx...Access Denied. Run from an admin prompt"
$global:issues += 1
return $false
}
$user = Get-UserPath
$pwd_file = Join-Path $user 'winappsdk.certificate.test.pwd'
# -CertPassword <password> is a required parameter for this work
$password = ''
if (-not [string]::IsNullOrEmpty($CertPassword))
{
$password_plaintext = $CertPassword
}
elseif (-not [string]::IsNullOrEmpty($CertPasswordFile))
{
if (-not(Test-Path -Path $CertPasswordFile -PathType Leaf))
{
Write-Host "Test certificate file $CertPasswordFile...Not Found"
$global:issues += 1
return $false
}
$password = Get-Content -Path $CertPasswordFile -Encoding utf8
}
elseif (($CertPasswordUser -eq $true) -and (Test-Path -Path $pwd_file -PathType Leaf))
{
$password = Get-Content -Path $pwd_file -Encoding utf8
}
elseif ($NoInteractive -eq $false)
{
$password_plaintext = Read-Host -Prompt 'Creating test certificate. Please enter a password'
}
if ([string]::IsNullOrEmpty($password_plaintext))
{
Write-Host "Test certificate .pfx...password parameter (-CertPassword | -CertPasswordFile | -CertPasswordUser) or prompting required"
$global:issues += 1
return $false
}
$password = ConvertTo-SecureString -String $password_plaintext -Force -AsPlainText
Set-Content -Path $pwd_file -Value $password_plaintext -Force
# Prepare to record the pfx for the certificate
$user = Get-UserPath
$cert_thumbprint = Join-Path $user 'winappsdk.certificate.test.thumbprint'
# Create the certificate
$cert_path = "cert:\CurrentUser\My"
$now = Get-Date
$expiration = $now.AddMonths(12)
$subject = 'CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US'
$friendly_name = "Microsoft.WindowsAppSDK Test Certificate Create=$now"
$key_friendly_name = "Microsoft.WindowsAppSDK Test PrivateKey Create=$now"
$key_description = "Microsoft.WindowsAppSDK Test PrivateKey Create=$now"
$eku_oid = '2.5.29.37'
$eku_value = '1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13'
$eku = "$eku_oid={text}$eku_value"
$cert = New-SelfSignedCertificate -CertStoreLocation $cert_path -NotAfter $expiration -Subject $subject -FriendlyName $friendly_name -KeyFriendlyName $key_friendly_name -KeyDescription $key_description -TextExtension $eku
# Save the thumbprint
$thumbprint = $cert.Thumbprint
Set-Content -Path $cert_thumbprint -Value $thumbprint -Force
# Export the certificate
$cer = Join-Path $user 'winappsdk.certificate.test.cer'
$export_cer = Export-Certificate -Cert $cert -FilePath $cer -Force
$cert_personal = "cert:\CurrentUser\My\$thumbprint"
$pfx = Join-Path $user 'winappsdk.certificate.test.pfx'
$export_pfx = Export-PfxCertificate -Cert $cert_personal -FilePath $pfx -Password $password
# Delete the personal certiicate
Remove-Item -Path $cert_personal -DeleteKey
$ok = $true
foreach ($f in $cer,$pfx,$pwd_file,$cert_thumbprint)
{
if (Test-Path -Path $f -PathType Leaf)
{
Write-Host "Create $f...OK"
}
else
{
Write-Host "Create $f...Error"
$global:issues += 1
$ok = $false
}
}
return $ok
}
function Remove-DevTestPfx
{
$user = Get-UserPath
$cer = Join-Path $user 'winappsdk.certificate.test.cer'
$pfx = Join-Path $user 'winappsdk.certificate.test.pfx'
$pwd_file = Join-Path $user 'winappsdk.certificate.test.pwd'
$pfx_thumbprint = Join-Path $user 'winappsdk.certificate.test.thumbprint'
foreach ($f in $cer,$pfx,$pwd_file,$pfx_thumbprint)
{
if (Test-Path -Path $f -PathType Leaf)
{
Remove-Item -Path $f -Force
Write-Host "Remove test certificate $f...OK"
}
}
return $true
}
function Test-DevTestCert
{
$user = Get-UserPath
$pfx_thumbprint = Join-Path $user 'winappsdk.certificate.test.thumbprint'
$thumbprint = Get-Content -Path $pfx_thumbprint -Encoding utf8
$cert_path = "cert:\LocalMachine\TrustedPeople\$thumbprint"
if (-not(Test-Path -Path $cert_path))
{
Write-Host "Test certificate $pfx_thumbprint thumbprint $thumbprint...Not Found"
$global:issues += 1
return $false
}
$cert = Get-ChildItem -Path $cert_path
$expiration = $cert.NotAfter
$now = Get-Date
if ($expiration -lt $now)
{
Write-Host "Test certificate $thumbprint...Expired ($expiration)"
$global:issues += 1
return $false
}
elseif ($expiration -lt ($now + (New-TimeSpan -Days 14)))
{
Write-Host "Test certificate $thumbprint...Expires soon ($expiration)"
$global:issues += 1
return $false
}
Write-Host "Test certificate $thumbprint...OK"
return $true
}
function Repair-DevTestCert
{
$isadmin = Get-IsAdmin
if ($isadmin -eq $false)
{
Write-Host "Install test certificate...Access Denied. Run from an admin prompt"
$global:issues += 1
return
}
$user = Get-UserPath
$cer = Join-Path $user 'winappsdk.certificate.test.cer'
$pfx = Join-Path $user 'winappsdk.certificate.test.pfx'
$pfx_thumbprint = Join-Path $user 'winappsdk.certificate.test.thumbprint'
$thumbprint = Get-Content -Path $pfx_thumbprint -Encoding utf8
$cert_path = "cert:\LocalMachine\TrustedPeople"
$x509certificates = Import-Certificate -FilePath $cer -CertStoreLocation $cert_path
Write-Host "Install test certificate $cer...OK"
}
function Remove-DevTestCert
{
$isadmin = Get-IsAdmin
if ($isadmin -eq $false)
{
Write-Host "Remove test certificate...Access Denied. Run from an admin prompt"
$global:issues += 1
return $false
}
$user = Get-UserPath
$pfx_thumbprint = Join-Path $user 'winappsdk.certificate.test.thumbprint'
if (-not(Test-Path -Path [System.IO.FileInfo]$pfx_thumbprint -PathType Leaf))
{
Write-Host "Remove test certificate $pfx_thumbprint...OK (Not Found)"
return $true
}
$thumbprint = Get-Content -Path $pfx_thumbprint -Encoding utf8
$cert_path = "cert:\LocalMachine\TrustedPeople\$thumbprint"
if (-not(Test-Path -Path $cert_path))
{
Write-Host "Remove test certificate for $thumbprint...OK (Not Found)"
return $true
}
Remove-Item -Path $cert_path -DeleteKey
Write-Host "Remove test certificate for $thumbprint...OK"
return $true
}
function Test-TAEFService
{
$service = Get-Service |Where-Object {$_.Name -eq "TE.Service"}
if ([string]::IsNullOrEmpty($service))
{
Write-Host "TAEF service...Not Installed"
return 'NotFound'
}
elseif ($service.Status -ne "Running")
{
Write-Host "TAEF service...Not running ($service.Status)"
return 'NotRunning'
}
else
{
Write-Host "TAEF service...Running"
return 'Running'
}
}
function Repair-TAEFService
{
$isadmin = Get-IsAdmin
if ($isadmin -eq $false)
{
Write-Host "Install TAEF service...Access Denied. Run from an admin prompt"
$global:issues += 1
return
}
$root = Get-ProjectRoot
$cpu = Get-CpuArchitecture
$taef = 'Microsoft.Taef.10.58.210222006-develop'
$path = "$root\packages\$taef\build\Binaries\$cpu\Wex.Services.exe"
if (-not(Test-Path -Path $path -PathType Leaf))
{
Write-Host "Install TAEF service...Not Found ($path)"
$global:issues += 1
return 'TAEFNotFound'
}
$args = '/install:TE.Service'
$output = Run-Process $path $args
$service = Get-Service |Where-Object {$_.Name -eq "TE.Service"}
if ([string]::IsNullOrEmpty($service))
{
Write-Host "Install TAEF service...Failed"
$global:issues += 1
return 'InstallError'
}
else
{
Write-Host "Install TAEF service...OK"
return 'NotRunning'
}
}
function Start-TAEFService
{
$isadmin = Get-IsAdmin
if ($isadmin -eq $false)
{
Write-Host "Install TAEF service...Access Denied. Run from an admin prompt"
$global:issues += 1
return
}
$ok = Start-Service 'TE.Service'
$service = Get-Service |Where-Object {$_.Name -eq "TE.Service"}
if ($service.Status -ne "Running")
{
Write-Host "Start TAEF service...Failed"
$global:issues += 1
}
else
{
Write-Host "Start TAEF service...OK"
return $true
}
}
Write-Output "Checking developer environment..."
$cpu = Get-CpuArchitecture
Write-Verbose("Processor...$cpu")
$project_root = Get-ProjectRoot
Write-Output "Windows App SDK location...$project_root"
if (($CheckAll -ne $false) -Or ($CheckVisualStudio -ne $false))
{
Test-VisualStudio2022Install
}
if (($CheckAll -ne $false) -Or ($CheckTestPfx -ne $false))
{
$test = Test-DevTestPfx
if ($test -ne $true)
{
$test = Repair-DevTestPfx
if ($test -ne $true)
{
Write-Output "Fatal error. Aborting..."
Exit 1
}
}
}
if (($CheckAll -ne $false) -Or ($CheckTestCert -ne $false))
{
$test = Test-DevTestCert
if ($test -ne $true)
{
Repair-DevTestCert
}
}
if (($CheckAll -ne $false) -Or ($CheckTAEFService -ne $false))
{
$test = Test-TAEFService
if ($test -eq 'NotFound')
{
$test = Repair-TAEFService
}
if ($test -eq 'NotRunning')
{
$test = Start-TAEFService
}
}
if (($RemoveAll -ne $false) -Or ($RemoveTestCert -ne $false))
{
$test = Remove-DevTestCert
}
if (($RemoveAll -ne $false) -Or ($RemoveTestPfx -ne $false))
{
$test = Remove-DevTestPfx
}
if ($global:issues -eq 0)
{
Write-Output "Coding time!"
}
else
{
$n = $global:issues
Write-Output "$n issue(s) detected"
}