-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSDBuilder_Auto.ps1
More file actions
590 lines (513 loc) · 23 KB
/
OSDBuilder_Auto.ps1
File metadata and controls
590 lines (513 loc) · 23 KB
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
#Requires -Modules WindowsImageTools, OSD, OSDBuilder, OSDSUS, PSWriteHTML
#Requires -RunAsAdministrator
$ErrorArray = @()
$SuccessArray = @()
$Credential = (Get-Credential BuildAdmin)
Function Write-SuccessArray {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline)]$Item
)
[PSCustomObject]@{
Timestamp = (Get-Date -f "mm/dd HH:MM:ss zzz")
Message = $Item
}
}
$Paths = [PSCustomObject]@{
OSDBPath = ($Env:SystemDrive + "\" + "Build2")
OSDBWIM = ($Env:SystemDrive + "\" + "Windows\Temp\OSDB WIM")
OSDBModule = (Get-Module -Name OSDBuilder -ErrorAction SilentlyContinue).Path
WIMServicing = ($Env:SystemDrive + "\" + "Output")
ISOPath = ($Env:SystemDrive + "\" + "Intake")
}
$ExtrasContent = "$($Paths.ISOPath)\PSWindowsUpdate"
$CMTrace = "$($Paths.IsoPath)\CMTrace.exe"
$CheckPaths = @()
#$CheckPaths += $WIM2VHD = "$($Paths.WIMServicing)\WIM2VHD"
$CheckPaths += $Paths.WimServicing
$CheckPaths += $vhdPath = "$($Paths.WimServicing)\Virtual Disks"
$CheckPaths += $vmPath = "$($Paths.WimServicing)\Virtual Machines"
$CheckPaths += $CapturePath = "$($Paths.WimServicing)\Capture"
$CheckPaths += $vhdMount = "$($Paths.WimServicing)\VHDMount"
$CheckPaths | ForEach-Object {
$PathResult = Test-Path $_
if ($PathResult -eq $false) {
New-Item -Path $_ -ItemType Directory -Force | Out-Null
}
}
Function Get-EdgeEnterpriseMSI {
<#
.SYNOPSIS
Get-EdgeEnterpriseMSI
.DESCRIPTION
Imports all device configurations in a folder to a specified tenant
.PARAMETER Channel
Channel to download, Valid Options are: Dev, Beta, Stable, EdgeUpdate, Policy.
.PARAMETER Platform
Platform to download, Valid Options are: Windows or MacOS, if using channel "Policy" this should be set to "any"
Defaults to Windows if not set.
.PARAMETER Architecture
Architecture to download, Valid Options are: x86, x64, arm64, if using channel "Policy" this should be set to "any"
Defaults to x64 if not set.
.PARAMETER Version
If set the script will try and download a specific version. If not set it will download the latest.
.PARAMETER Folder
Specifies the Download folder
.PARAMETER Force
Overwrites the file without asking.
.NOTES
Version: 1.3
Author: Mattias Benninge
Modified by: DM
Creation Date: 2020-07-01
Update Date: 2022-05-14
Version history:
1.0 - Initial script development
1.1 - Fixes and improvements by @KarlGrindon
- Script now handles multiple files for e.g. MacOS Edge files
- Better error handling and formating
- URI Validation
1.2 - Better compability on servers (force TLS and remove dependency to IE)
1.3 - Added version check function
https://docs.microsoft.com/en-us/mem/configmgr/apps/deploy-use/deploy-edge
.EXAMPLE
Download the latest version for the Beta channel and overwrite any existing file
.\Get-EdgeEnterpriseMSI.ps1 -Channel Beta -Folder D:\SourceCode\PowerShell\Div -Force
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, HelpMessage = 'Channel to download, Valid Options are: Dev, Beta, Stable, EdgeUpdate, Policy')]
[ValidateSet('Dev', 'Beta', 'Stable', 'EdgeUpdate', 'Policy')]
[string]$Channel,
[Parameter(Mandatory = $false, HelpMessage = 'Folder where the file will be downloaded')]
[ValidateNotNullOrEmpty()]
[string]$Folder,
[Parameter(Mandatory = $false, HelpMessage = 'Platform to download, Valid Options are: Windows or MacOS')]
[ValidateSet('Windows', 'MacOS', 'any')]
[string]$Platform = "Windows",
[Parameter(Mandatory = $false, HelpMessage = "Architecture to download, Valid Options are: x86, x64, arm64, any")]
[ValidateSet('x86', 'x64', 'arm64', 'any')]
[string]$Architecture = "x64",
[parameter(Mandatory = $false, HelpMessage = "Specifies which version to download")]
[ValidateNotNullOrEmpty()]
[string]$ProductVersion,
[switch]$VersionCheck,
[parameter(Mandatory = $false, HelpMessage = "Overwrites the file without asking")]
[Switch]$Force
)
$ErrorActionPreference = "Stop"
$edgeEnterpriseMSIUri = 'https://edgeupdates.microsoft.com/api/products?view=enterprise'
# Validating parameters to reduce user errors
if ($Channel -eq "Policy" -and ($Architecture -ne "Any" -or $Platform -ne "Any")) {
Write-Warning ("Channel 'Policy' requested, but either 'Architecture' and/or 'Platform' is not set to 'Any'.
Setting Architecture and Platform to 'Any'")
$Architecture = "Any"
$Platform = "Any"
}
elseif ($Channel -ne "Policy" -and ($Architecture -eq "Any" -or $Platform -eq "Any")) {
throw "If Channel isn't set to policy, architecture and/or platform can't be set to 'Any'"
}
elseif ($Channel -eq "EdgeUpdate" -and ($Architecture -ne "x86" -or $Platform -eq "Windows")) {
Write-Warning ("Channel 'EdgeUpdate' requested, but either 'Architecture' is not set to x86 and/or 'Platform'
is not set to 'Windows'. Setting Architecture to 'x86' and Platform to 'Windows'")
$Architecture = "x86"
$Platform = "Windows"
}
Write-Host "Enabling connection over TLS for better compability on servers" -ForegroundColor Green
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
# Test if HTTP status code 200 is returned from URI
try {
Invoke-WebRequest $edgeEnterpriseMSIUri -UseBasicParsing | Where-Object StatusCode -match 200 | Out-Null
}
catch {
throw "Unable to get HTTP status code 200 from $edgeEnterpriseMSIUri. Does the URL still exist?"
}
Write-Host "Getting available files from $edgeEnterpriseMSIUri" -ForegroundColor Green
# Try to get JSON data from Microsoft
try {
$response = Invoke-WebRequest -Uri $edgeEnterpriseMSIUri -Method Get -ContentType "application/json" -UseBasicParsing -ErrorVariable InvokeWebRequestError
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))
Write-Host "Succefully retrived data" -ForegroundColor Green
}
catch {
throw "Could not get MSI data: $InvokeWebRequestError"
}
# Alternative is to use Invoke-RestMethod to get a Json object directly
# $jsonObj = Invoke-RestMethod -Uri "https://edgeupdates.microsoft.com/api/products?view=enterprise" -UseBasicParsing
$selectedIndex = [array]::indexof($jsonObj.Product, "$Channel")
if (-not $ProductVersion) {
try {
Write-host "No version specified, getting the latest for $Channel" -ForegroundColor Green
$script:selectedVersion = (([Version[]](($jsonObj[$selectedIndex].Releases |
Where-Object { $_.Architecture -eq $Architecture -and $_.Platform -eq $Platform }).ProductVersion) |
Sort-Object -Descending)[0]).ToString(4)
Write-Host "Latest Version for channel $Channel is $selectedVersion`n" -ForegroundColor Green
if ($VersionCheck) {
#$selectedVersion = $script:selectedVersion
return $SelectedVersion
break
}
$selectedObject = $jsonObj[$selectedIndex].Releases |
Where-Object { $_.Architecture -eq $Architecture -and $_.Platform -eq $Platform -and $_.ProductVersion -eq $selectedVersion }
}
catch {
throw "Unable to get object from Microsoft. Check your parameters and refer to script help."
}
}
else {
Write-Host "Matching $ProductVersion on channel $Channel" -ForegroundColor Green
$selectedObject = ($jsonObj[$selectedIndex].Releases |
Where-Object { $_.Architecture -eq $Architecture -and $_.Platform -eq $Platform -and $_.ProductVersion -eq $ProductVersion })
if (-not $selectedObject) {
throw "No version matching $ProductVersion found in $channel channel for $Architecture architecture."
}
else {
Write-Host "Found matching version`n" -ForegroundColor Green
}
}
if (Test-Path $Folder) {
foreach ($artifacts in $selectedObject.Artifacts) {
# Not showing the progress bar in Invoke-WebRequest is quite a bit faster than default
$ProgressPreference = 'SilentlyContinue'
Write-host "Starting download of: $($artifacts.Location)" -ForegroundColor Green
# Work out file name
$fileName = Split-Path $artifacts.Location -Leaf
if (Test-Path "$Folder\$fileName" -ErrorAction SilentlyContinue) {
if ($Force) {
Write-Host "Force specified. Will attempt to download and overwrite existing file." -ForegroundColor Green
try {
Invoke-WebRequest -Uri $artifacts.Location -OutFile "$Folder\$fileName" -UseBasicParsing
}
catch {
throw "Attempted to download file, but failed: $error[0]"
}
}
else {
# CR-someday: There should be an evaluation of the file version, if possible. Currently the function only
# checks if a file of the same name exists, not if the versions differ
Write-Host "$Folder\$fileName already exists!" -ForegroundColor Yellow
do {
$overWrite = Read-Host -Prompt "Press Y to overwrite or N to quit."
}
# -notmatch is case insensitive
while ($overWrite -notmatch '^y$|^n$')
if ($overWrite -match '^y$') {
Write-Host "Starting Download" -ForegroundColor Green
try {
Invoke-WebRequest -Uri $artifacts.Location -OutFile "$Folder\$fileName" -UseBasicParsing
}
catch {
throw "Attempted to download file, but failed: $error[0]"
}
}
else {
Write-Host "File already exists and user chose not to overwrite, exiting script." -ForegroundColor Red
exit
}
}
}
else {
Write-Host "Starting Download" -ForegroundColor Green
try {
Invoke-WebRequest -Uri $artifacts.Location -OutFile "$Folder\$fileName" -UseBasicParsing
}
catch {
throw "Attempted to download file, but failed: $error[0]"
}
}
if (((Get-FileHash -Algorithm $artifacts.HashAlgorithm -Path "$Folder\$fileName").Hash) -eq $artifacts.Hash) {
Write-Host "Calculated checksum matches known checksum`n" -ForegroundColor Green
}
else {
Write-Warning "Checksum mismatch!"
Write-Warning "Expected Hash: $($artifacts.Hash)"
Write-Warning "Downloaded file Hash: $((Get-FileHash -Algorithm $($artifacts.HashAlgorithm) -Path "$Folder\$fileName").Hash)`n"
}
}
}
else {
throw "Folder $Folder does not exist"
}
Write-Host "-- Script Completed: File Downloaded -- " -ForegroundColor Green
}
if (Test-Path $Paths.OSDBPath) {
try {
Write-Host "Removing existing OSDBuilder folder structure..." -ForegroundColor DarkMagenta
Remove-Item -Path $Paths.OSDBPath -Force -Recurse -ErrorAction Stop -Exclude ($Paths.OSDBPath + "\" + "Updates") | Out-Null
$SuccessArray += (Write-Output "$($Paths.OSDBPATH) destroyed" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
}
### Update OSDSUS
try {
Update-OSDSUS
$VersionGet = (Get-Module -Name OSDBuilder).Version
$SuccessArray += (Write-Output "OSDBuilder module version $VersionGet installed" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Import OSDBuilder
Write-Host "Importing OSDBuilder Module..." -ForegroundColor Green
try {
Import-Module -Name OSDBuilder -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "OSDBuilder module version $VersionGet imported" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Initialize OSDBuilder
Write-Host "Initializing OSDBuilder (Create Paths)..." -ForegroundColor Green
try {
#Initialize-OSDBuilder -SetHome $Paths.OSDBPath-ErrorAction Stop | Out-Null
Get-OSDBuilder -SetPath $($Paths.OSDBPath) -CreatePaths -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "OSDBuilder initialized" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Input ISO
try {
$ISORelPath = $Paths.ISOPath + "\" + "*.ISO"
$ISO = Get-Item $ISORelPath -ErrorAction Stop
$ISOFullPath = ($($Paths.ISOPath) + "\" + $($ISO.Name))
if ($ISO) {
Mount-DiskImage -ImagePath $ISOFullPath | Out-Null
$SuccessArray += (Write-Output "Mounted $($ISO.Name)" | Write-SuccessArray)
}
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Import OSMedia
try {
Write-Host "Import OS Media: $($ISO.Name) and Update/Install .Net 3.5..." -ForegroundColor Green
Import-OSMedia -EditionId Enterprise -SkipGridView -Update -BuildNetFx -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "Imported OSMedia into $($ISO.Name)" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Dismount Image
try {
Write-Host "Dismounting OS Media: $($ISO.Name)..." -ForegroundColor DarkMagenta
Dismount-DiskImage -ImagePath $ISOFullPath -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "Dismounted $($ISO.Name)" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Extra Directories
try {
Write-Host "Creating ExtraFiles Directories..." -ForegroundColor Green
New-Item -Path ($Paths.OSDBPath + "\Content\ExtraFiles\CMTrace\Windows\System32") -ItemType Directory -Force | Out-Null
$SuccessArray += (Write-Output "Created Path..." | Write-SuccessArray)
New-Item -Path ($Paths.OSDBPath + "\Content\ExtraFiles\scripts\Windows\Installer") -ItemType Directory -Force | Out-Null
$SuccessArray += (Write-Output "Created Path..." | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
try {
Write-Host "Populating ExtraFiles Directories and Unattend.xml..." -ForegroundColor Green
Copy-Item -Path $CMTrace -Destination ($Paths.OSDBPath + "\Content\ExtraFiles\CMTrace\Windows\System32") -Force -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "Copied file to path..." | Write-SuccessArray)
Copy-Item -Path $ExtrasContent -Destination ($Paths.OSDBPath + "\Content\ExtraFiles\scripts\Windows\Installer") -Recurse -Force -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "Copied file to path..." | Write-SuccessArray)
Copy-Item -Path ($Paths.ISOPath + "\" + "Unattend.xml") -Destination ($Paths.OSDBPath + "\Content\Unattend") -Force -ErrorAction Stop | Out-Null
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Update OS Media
try {
$StartDTM = (Get-Date)
$OSMediaSource = Get-ChildItem -Path ($Paths.OSDBPath + "\OSImport") | Sort-Object LastAccessTime -Descending | Select-Object -First 1
Write-Host "Updating OS Media : $($OSMediaSource.Name), starting at $StartDTM..." -ForegroundColor Green
Update-OSMedia -Name $($OSMediaSource.Name) -Download -Execute -SkipComponentCleanup -ErrorAction Stop | Out-Null
$EndDTM = (Get-Date)
Write-Host "Updated OS Media : $($OSMediaSource.Name), ending at $EndDTM. Elapsed Time: $(($EndDTM-$StartDTM).TotalMinutes) Minutes" -ForegroundColor Green
$SuccessArray += (Write-Output "Update-OSMedia completed succesfully, Elapsed Time: $(($EndDTM-$StartDTM).TotalMinutes) Minutes" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Task for OS Media
$Date = Get-Date -Format MMddyy
$TaskName = "Build-$Date"
try {
$Pull = (Get-OSMedia)[-1]
Write-Host "Creating JSON Taskfile for $Pull..." -ForegroundColor Green
New-OSBuildTask -TaskName $TaskName <#-EnableNetFX3#> <#-ContentExtraFiles#> <#-ContentUnattend#> -OSMedia $Pull -ErrorAction Stop | Out-Null
$SuccessArray += (Write-Output "Created JSON Taskfile for $Pull" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
}
### Inject ExtraFiles Data
$Pattern = @"
"EnableNetFX3": "False",
"@
$Inject = @"
"EnableNetFX3": "True",
"@
$Pattern1 = @"
"ExtraFiles": null,
"@
$Inject1 = @"
"ExtraFiles": [
"ExtraFiles\\CMTrace",
"ExtraFiles\\scripts"
],
"@
$Pattern2 = @"
"UnattendXML": "",
"@
$Inject2 = @"
"UnattendXML": "Unattend\\Unattend.xml",
"@
$JSONPath = $Paths.OSDBPath + "\Tasks\OSBuild " + $TaskName + ".json"
$TaskJSON = Get-Content $JSONPath
$TaskJSON -Replace $Pattern,$Inject | Set-Content -Path $JSONPath
$TaskJSON = Get-Content $JSONPath
$TaskJSON -Replace $Pattern1,$Inject1 | Set-Content -Path $JSONPath
$TaskJSON = Get-Content $JSONPath
$TaskJSON -Replace $Pattern2,$Inject2 | Set-Content -Path $JSONPath
Write-Host "Injected ExtraFiles and Unattend pattern into JSON Taskfile." -ForegroundColor Green
### Create New OS Build
try {
$StartDTM = (Get-Date)
Write-Host "Creating OS Build : $TaskName, starting at $StartDTM..." -ForegroundColor Green
New-OSBuild -ByTaskName $TaskName -Download -Execute | Out-Null
$EndDTM = (Get-Date)
Write-Host "Created OS Build : $TaskName, ending at $EndDTM. Elapsed Time: $(($EndDTM-$StartDTM).TotalMinutes) Minutes" -ForegroundColor Green
$SuccessArray += (Write-Output "OS-Media Creation for Task $TaskName completed succesfully, Elapsed time: $(($EndDTM-$StartDTM).TotalMinutes) Minutes" | Write-SuccessArray)
}
catch {
$ErrorArray += (Write-Output $_.Exception)
$ErrorCounter++
}
$LatestBuild = ((Get-ChildItem ($Paths.OSDBPath + "\OSBuilds") | Sort-Object -Descending)[0])
[string]$LatestBuildFN = ((Get-ChildItem ($Paths.OSDBPath + "\OSBuilds") | Sort-Object -Descending)[0])
$WIMSource = $LatestBuild.FullName + "\OS\sources" #\install.wim"
$OSDBCheck = Test-Path $Paths.OSDBWIM
if (!($OSDBCheck)) {
New-Item -ItemType Directory -Path $Paths.OSDBWIM
}
Write-Host "Copying $WIMSource to $($Paths.OSDBWIM) ..." -ForegroundColor Green
#Copy-Item -Path $WIMSource -Destination $WIMDest -Recurse -ErrorAction Stop | Out-Null
robocopy.exe $WIMSource $Paths.OSDBWIM /zb *install.wim | Out-Null
if (Get-Item -Path ($Paths.OSDBWIM + "\" + $LatestBuildFN + ".wim") -ErrorAction SilentlyContinue) {
Remove-Item -Path ($Paths.OSDBWIM + "\" + $LatestBuildFN + ".wim")
}
Rename-Item -Path ($Paths.OSDBWIM + "\install.wim") -NewName ($LatestBuildFN + ".wim")
### Clear Stale Mounts
$Mounts = Get-WindowsImage -Mounted -ErrorAction SilentlyContinue
if ($Mounts) {
try {
Write-Host "Clearing stuck/unused DISM mounts..." -ForegroundColor DarkMagenta
Get-WindowsImage -Mounted | Dismount-WindowsImage -Discard #| Out-Null
$SuccessArray += (Write-Output "Stale mounts cleared" | Write-SuccessArray)
}
catch {
$ErrorArray += ($_.Exception)
Clear-WindowsCorruptMountPoint
}
}
#### Convert to VHD, Create Hyper-V VM with new VHD ####
#$WIM2VHD = $Paths.WIMServicing + "\WIM2VHD"
#Import-Module ($WIM2VHD + "\module\WindowsImageTools\1.9.30\WindowsImageTools.psd1")
$GUID = (New-GUID).Guid
$date = Get-Date -Format yyyyMMdd
$vhd = $GUID
$WIM = (Get-ChildItem -Path ($Paths.OSDBWim) -Filter *.wim | Sort -Descending | Select -First 1).VersionInfo.FileName
Convert-Wim2VHD -Path "$vhdPath\$vhd.vhdx" -SourcePath $WIM -Index 1 -Size 40GB -DiskLayout UEFI -Dynamic -ErrorAction Stop
Remove-Item $WIM #Clean item from Temp after conversion
New-VM -Name $vhd -MemoryStartupBytes 8GB -VHDPath "$vhdPath\$vhd.vhdx" -Generation 2 -SwitchName 'Default Switch' -Path "$vmPath"
#Add-VMDvdDrive -VMName $vhd -Path "$WIM2VHD\images\WinPE_amd64.iso"
Set-VM -Name $vhd -CheckpointType Disabled
Start-VM -Name $vhd
Start-Process -FilePath "C:\Windows\System32\vmconnect.exe" -ArgumentList "localhost $vhd"
Write-Host -ForegroundColor Green "Waiting for PowerShell Direct session..."
Function Get-BuildSession {
Get-PSSession -Name "Build" -ErrorAction SilentlyContinue | ? {$_.State -eq "Opened"}
}
$FileSB = {
Test-Path C:\Windows\Temp\TokenFile.txt
}
Get-BuildSession | Remove-PSSession
do {
Start-Sleep -Seconds 15
$Result = $Null
$VM = Get-VM -Name $vhd
New-PSSession -VMName $VM.Name -Name "Build" -Credential $Credential -ErrorAction SilentlyContinue | Out-Null
if (Get-PSSession -Name "Build" -ErrorAction SilentlyContinue | ? {$_.State -eq "Opened"} ) {
$Result = Invoke-Command -Session (Get-PSSession -Name "Build" -ErrorAction SilentlyContinue | ? {$_.State -eq "Opened"} ) -ScriptBlock $FileSB
if ($Result -eq $false) {
Get-PSSession -Name "Build" | Remove-PSSession
}
}
} until (
( [bool](Get-PSSession -Name "Build" -ErrorAction SilentlyContinue | ? {$_.State -eq "Opened"} ) -and ($Result -eq $true) ) -eq $true
)
$SB = {
$UpdateSession = New-Object -ComObject 'Microsoft.Update.Session'
$SearchResult = New-Object -ComObject 'Microsoft.Update.UpdateColl'
$UpdateSession.ClientApplicationID = 'Build Windows Update Installer'
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$SearchResult = $UpdateSearcher.Search("IsInstalled=1 and Type='Software' and IsHidden=0")
$UpdateResults = $SearchResult.Updates | Select Title, SupportUrl
$Version = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
$EdgeVersion = (Get-Item -Path ${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe).VersionInfo.ProductVersion
$OutputObj = [PSCustomObject]@{
Build = $Version.CurrentBuildNumber + "." + $Version.UBR
MSEdge = $EdgeVersion
Updates = $UpdateResults
}
$OutputObj
}
$Report = Invoke-Command -ScriptBlock $SB -Session (Get-PSSession -Name "Build"| ? {$_.State -eq "Opened"} ) #| Select Title, SupportURL
Import-Module PSWriteHTML
$BuildVer = $Report.Build
$Updates = $Report.Updates
$MSEdge = $Report.MSEdge
$NewestMSEdge = Get-EdgeEnterpriseMSI -VersionCheck -Channel Stable
#### Build Report ####
New-HTML {
New-HTMLList {
New-HTMLListItem -Text "Build Version: $BuildVer" -BackGroundColor SkyBlue -Color White
New-HTMLListItem -Text "MS Edge Version: $MSEdge - Newest Reported Version: $NewestMSEdge"
}
New-HTMLSection -HeaderText 'Updates' -BackgroundColor SkyBlue {
New-HTMLTable -Title Updates -DataTable $Updates
}
} -ShowHTML -TitleText "Build Report"
Start-Sleep -s 10
Get-BuildSession | Remove-PSSession
Write-Host -ForegroundColor Green "Waiting for VM to be turned off..."
do {
Start-Sleep -s 60
}
until ((Get-VM -Name $vhd).State -eq "Off")
#### ISO Capture ####
Write-Host -ForegroundColor Green "Beginning Capture..."
# Script Log Path
$scriptlog = "$($CapturePath)\capture_$($vhd).log"
# Start Logging
Start-Transcript $scriptlog -Append
# Define Image File for Capture
$Image = $vhdPath + "\" + $vhd + ".vhdx"
# Mount Image
Write-Host -ForegroundColor Green "Mount Image for Capture..."
Mount-WindowsImage -ImagePath $Image -Index 1 -Path $VHDMount
# Capture Image
Write-Host -ForegroundColor Green "Capture new image..."
New-WindowsImage -CapturePath $VHDMount -Name "Windows 10 Enterprise" -ImagePath "$($CapturePath)\$($vhd).wim" -Description "Windows 10 Enterprise" -Verify -Verbose
Start-Sleep -s 5
# Dismount Image
Write-Host -ForegroundColor Green "Dismount image."
Dismount-WindowsImage -Path $VHDMount -Discard
Stop-Transcript
Write-Host -ForegroundColor Green "Capture completed."