-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateISO.ps1
841 lines (717 loc) · 32 KB
/
CreateISO.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
<#
.NOTES
===========================================================================
Author: Richard Tracy
Filename: CreateISO.ps1
Last Updated: 03/22/2019
Thanks to:
===========================================================================
.PARAMETER ImageName
.PARAMETER WIMSourceRoot
.PARAMETER StagingPath
.PARAMETER ISOPath
.PARAMETER OscdimgPath
.PARAMETER Bootable
.PARAMETER BootFilePath
.PARAMETER EFIBootFilePath
.PARAMETER SplitISO
.EXAMPLE
Sample Using MDT parameters
.\createISO.ps1 -ImageName %TaskSequenceName% -WIMSourceRoot %BackupFile% -StagingPath .\SourceFiles\%TaskSequenceName% -ISOPath "\\filer.s3i.org\s3isoftware\Software\Microsoft\ISOs\PATCHED_ISO" -Bootable
Sample Using normal parameters
.\createISO.ps1 -ImageName WIN101809X64 -WIMSourceRoot "D:\DeploymentShare\Captures" -StagingPath "D:\DeploymentShare\Scripts\Custom\OS-ISOBuild\SourceFiles\WIN101809X64" -ISOPath "\\filer.s3i.org\s3isoftware\Software\Microsoft\ISOs\PATCHED_ISO" -Bootable -Comparedby Hash
.LINKS
https://deploymentresearch.com/Research/Post/399/How-to-REALLY-create-a-Windows-10-ISO-no-3rd-party-tools-needed
#>
[CmdLetBinding()]
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("WIMFile")]
[string]$WIMSourceRoot,
[parameter(Mandatory=$false)]
[Alias("StagingDir")]
[string]$StagingPath,
[parameter(Mandatory=$false)]
[Alias("SetupFiles","SourcePath")]
[string]$SetupSource,
[parameter(Mandatory=$false)]
[string]$OscdimgPath,
[parameter(Mandatory=$false)]
[string]$ImageName,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Alias("ISOPath")]
[string]$ISODestPath,
[parameter(Mandatory=$false)]
[switch]$SplitISO,
[parameter(Mandatory=$false)]
[switch]$Bootable,
[parameter(Mandatory=$false)]
[string]$BootFilePath,
[parameter(Mandatory=$false)]
[string]$EFIBootFilePath,
[parameter(Mandatory=$false)]
[boolean]$CleanupTemp = $true
)
##*===========================================================================
##* FUNCTIONS
##*===========================================================================
Function Test-IsISE {
# try...catch accounts for:
# Set-StrictMode -Version latest
try {
return $psISE -ne $null;
}
catch {
return $false;
}
}
Function Get-ScriptPath {
If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation } Else { $InvocationInfo = $MyInvocation }
# Makes debugging from ISE easier.
if ($PSScriptRoot -eq "")
{
if (Test-IsISE)
{
$psISE.CurrentFile.FullPath
#$root = Split-Path -Parent $psISE.CurrentFile.FullPath
}
else
{
$context = $psEditor.GetEditorContext()
$context.CurrentFile.Path
#$root = Split-Path -Parent $context.CurrentFile.Path
}
}
else
{
#$PSScriptRoot
$PSCommandPath
#$MyInvocation.MyCommand.Path
}
}
Function Get-SMSTSENV{
param(
[switch]$ReturnLogPath,
[switch]$NoWarning
)
Begin{
## Get the name of this function
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
}
Process{
try{
# Create an object to access the task sequence environment
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
}
catch{
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
If(!$NoWarning){Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)}
#set variable to null
$Script:tsenv = $null
}
Finally{
#set global Logpath
if ($Script:tsenv){
#grab the progress UI
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
# Convert all of the variables currently in the environment to PowerShell variables
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
# Query the environment to get an existing variable
# Set a variable for the task sequence log path
#Something like: C:\MININT\SMSOSD\OSDLOGS
#[string]$LogPath = $tsenv.Value("LogPath")
#Somthing like C:\WINDOWS\CCM\Logs\SMSTSLog
[string]$LogPath = $tsenv.Value("_SMSTSLogPath")
}
Else{
[string]$LogPath = $env:Temp
}
}
}
End{
If($ReturnLogPath){return $LogPath}
}
}
Function Format-ElapsedTime($ts) {
$elapsedTime = ""
if ( $ts.Minutes -gt 0 ){$elapsedTime = [string]::Format( "{0:00} min. {1:00}.{2:00} sec.", $ts.Minutes, $ts.Seconds, $ts.Milliseconds / 10 );}
else{$elapsedTime = [string]::Format( "{0:00}.{1:00} sec.", $ts.Seconds, $ts.Milliseconds / 10 );}
if ($ts.Hours -eq 0 -and $ts.Minutes -eq 0 -and $ts.Seconds -eq 0){$elapsedTime = [string]::Format("{0:00} ms.", $ts.Milliseconds);}
if ($ts.Milliseconds -eq 0){$elapsedTime = [string]::Format("{0} ms", $ts.TotalMilliseconds);}
return $elapsedTime
}
Function Format-DatePrefix{
[string]$LogTime = (Get-Date -Format 'HH:mm:ss.fff').ToString()
[string]$LogDate = (Get-Date -Format 'MM-dd-yyyy').ToString()
$CombinedDateTime = "$LogDate $LogTime"
return ($LogDate + " " + $LogTime)
}
Function Write-LogEntry{
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory=$false,Position=2)]
[string]$Source = '',
[parameter(Mandatory=$false)]
[ValidateSet(0,1,2,3,4)]
[int16]$Severity,
[parameter(Mandatory=$false, HelpMessage="Name of the log file that the entry will written to.")]
[ValidateNotNullOrEmpty()]
[string]$OutputLogFile = $Global:LogFilePath,
[parameter(Mandatory=$false)]
[switch]$Outhost
)
## Get the name of this function
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
[string]$LogTime = (Get-Date -Format 'HH:mm:ss.fff').ToString()
[string]$LogDate = (Get-Date -Format 'MM-dd-yyyy').ToString()
[int32]$script:LogTimeZoneBias = [timezone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
[string]$LogTimePlusBias = $LogTime + $script:LogTimeZoneBias
# Get the file name of the source script
Try {
If ($script:MyInvocation.Value.ScriptName) {
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.Value.ScriptName -Leaf -ErrorAction 'Stop'
}
Else {
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.MyCommand.Definition -Leaf -ErrorAction 'Stop'
}
}
Catch {
$ScriptSource = ''
}
If(!$Severity){$Severity = 1}
$LogFormat = "<![LOG[$Message]LOG]!>" + "<time=`"$LogTimePlusBias`" " + "date=`"$LogDate`" " + "component=`"$ScriptSource`" " + "context=`"$([Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + "type=`"$Severity`" " + "thread=`"$PID`" " + "file=`"$ScriptSource`">"
# Add value to log file
try {
Out-File -InputObject $LogFormat -Append -NoClobber -Encoding Default -FilePath $OutputLogFile -ErrorAction Stop
}
catch {
Write-Host ("[{0}] [{1}] :: Unable to append log entry to [{1}], error: {2}" -f $LogTimePlusBias,$ScriptSource,$OutputLogFile,$_.Exception.ErrorMessage) -ForegroundColor Red
}
If($Outhost){
If($Source){
$OutputMsg = ("[{0}] [{1}] :: {2}" -f $LogTimePlusBias,$Source,$Message)
}
Else{
$OutputMsg = ("[{0}] [{1}] :: {2}" -f $LogTimePlusBias,$ScriptSource,$Message)
}
Switch($Severity){
0 {Write-Host $OutputMsg -ForegroundColor Green}
1 {Write-Host $OutputMsg -ForegroundColor Gray}
2 {Write-Warning $OutputMsg}
3 {Write-Host $OutputMsg -ForegroundColor Red}
4 {If($Global:Verbose){Write-Verbose $OutputMsg}}
default {Write-Host $OutputMsg}
}
}
}
function Show-ProgressStatus
{
<#
.SYNOPSIS
Shows task sequence secondary progress of a specific step
.DESCRIPTION
Adds a second progress bar to the existing Task Sequence Progress UI.
This progress bar can be updated to allow for a real-time progress of
a specific task sequence sub-step.
The Step and Max Step parameters are calculated when passed. This allows
you to have a "max steps" of 400, and update the step parameter. 100%
would be achieved when step is 400 and max step is 400. The percentages
are calculated behind the scenes by the Com Object.
.PARAMETER Message
The message to display the progress
.PARAMETER Step
Integer indicating current step
.PARAMETER MaxStep
Integer indicating 100%. A number other than 100 can be used.
.INPUTS
- Message: String
- Step: Long
- MaxStep: Long
.OUTPUTS
None
.EXAMPLE
Set's "Custom Step 1" at 30 percent complete
Show-ProgressStatus -Message "Running Custom Step 1" -Step 100 -MaxStep 300
.EXAMPLE
Set's "Custom Step 1" at 50 percent complete
Show-ProgressStatus -Message "Running Custom Step 1" -Step 150 -MaxStep 300
.EXAMPLE
Set's "Custom Step 1" at 100 percent complete
Show-ProgressStatus -Message "Running Custom Step 1" -Step 300 -MaxStep 300
#>
param(
[Parameter(Mandatory=$true)]
[string] $Message,
[Parameter(Mandatory=$true)]
[int]$Step,
[Parameter(Mandatory=$true)]
[int]$MaxStep,
[string]$SubMessage,
[int]$IncrementSteps,
[switch]$Outhost
)
Begin{
If($SubMessage){
$StatusMessage = ("{0} [{1}]" -f $Message,$SubMessage)
}
Else{
$StatusMessage = $Message
}
}
Process
{
If($Script:tsenv){
$Script:TSProgressUi.ShowActionProgress(`
$Script:tsenv.Value("_SMSTSOrgName"),`
$Script:tsenv.Value("_SMSTSPackageName"),`
$Script:tsenv.Value("_SMSTSCustomProgressDialogMessage"),`
$Script:tsenv.Value("_SMSTSCurrentActionName"),`
[Convert]::ToUInt32($Script:tsenv.Value("_SMSTSNextInstructionPointer")),`
[Convert]::ToUInt32($Script:tsenv.Value("_SMSTSInstructionTableSize")),`
$StatusMessage,`
$Step,`
$Maxstep)
}
Else{
Write-Progress -Activity "$Message ($Step of $Maxstep)" -Status $StatusMessage -PercentComplete (($Step / $Maxstep) * 100) -id 1
}
}
End{
Write-LogEntry $Message -Severity 1 -Outhost:$Outhost
}
}
function Copy-LatestFile{
[CmdletBinding(SupportsShouldProcess = $True)]
Param(
[Parameter(Mandatory=$true)]
[Alias("Source","Path")]
$SourceFile,
[Parameter(Mandatory=$true)]
[Alias("Destination","Dest")]
$DestFile,
[Parameter(Mandatory=$false)]
[ValidateSet("Hash","Date","Size")]
[string]$Compareby = "Size"
)
Begin
{
## Get the name of this function
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
if (-not $PSBoundParameters.ContainsKey('Verbose')) {
$VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
}
if (-not $PSBoundParameters.ContainsKey('Confirm')) {
$ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
}
if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
$WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
}
}
Process
{
#if Source path doesn't exist unable to do anything
If(-not(Test-Path $SourceFile)){
Write-LogEntry ("Source files doesn't exist; Unable to compare or copy anything to") -Severity 3 -Source ${CmdletName} -Outhost
Return
}
#only compare source and destination if they already exists
ElseIf( (Test-Path $SourceFile) -and (Test-Path $DestFile) ){
Write-LogEntry ("Preparing to copy [{0}] to [{1}]..." -f $SourceFile,$DestFile) -Severity 1 -Source ${CmdletName} -Outhost
#grab the file name form the full path
$SrcFileName = Split-Path $SourceFile -Leaf
$DestFileName = Split-Path $DestFile -Leaf
switch($Compareby){
"Date" {
Write-LogEntry ("Checking Date for [{0}]." -f $SourceFile) -Severity 1 -Source ${CmdletName} -Outhost
$SourceCompare = (Get-Item $SourceFile).LastWriteTimeUTC
Write-LogEntry ("Comparing Date with [{0}]." -f $DestFile) -Severity 1 -Source ${CmdletName} -Outhost
$DestCompare = (Get-Item $DestFile).LastWriteTimeUTC
}
"Size" {
Write-LogEntry ("Checking Size for [{0}]." -f $SourceFile) -Severity 1 -Source ${CmdletName} -Outhost
$SourceCompare = (Get-Item $SourceFile).Length
Write-LogEntry ("Comparing Size with [{0}]." -f $DestFile) -Severity 1 -Source ${CmdletName} -Outhost
$DestCompare = (Get-Item $DestFile).Length
}
"Hash" {
Write-LogEntry ("Checking Hash value for [{0}]. This can take a while..." -f $SourceFile) -Severity 1 -Source ${CmdletName} -Outhost
$SourceCompare = Get-FileHash -Path $SourceFile -Algorithm MD5
Write-LogEntry ("Comparing Hash value with [{0}]. This can take a while..." -f $DestFile) -Severity 1 -Source ${CmdletName} -Outhost
$DestCompare = Get-FileHash -Path $DestFile -Algorithm MD5
}
}
if( $SourceCompare -ne $DestCompare )
{
Write-LogEntry ("{2} Comparison [{0}] is different than [{1}]. Overwriting destination file with source file..." -f $SrcFileName,$DestFileName,$Compareby) -Severity 1 -Source ${CmdletName} -Outhost
Copy-File -Path $SourceFile -Destination $DestFile -Force -whatif:$WhatIfPreference
#Copy-ItemWithProgress -Source $SourceFile -Destination $DestFile -Force
}
else
{
#Hash or date match, not overrighting file
Write-LogEntry ("{2} Comparison [{0}] matches destination file [{1}]" -f $SrcFileName,$DestFileName,$Compareby) -Severity 1 -Source ${CmdletName} -Outhost
}
}
#otherwise just copy the file
Else{
Write-LogEntry ("No destination file to compare; Copying [{0}] to [{1}]..." -f $SourceFile,(Split-Path $DestFile -Parent)) -Severity 1 -Source ${CmdletName} -Outhost
Copy-File -Path $SourceFile -Destination $DestFile -Force -whatif:$WhatIfPreference
#Copy-ItemWithProgress -Source $SourceFile -Destination $DestFile -Force
#Import-Module BitsTransfer
#Start-BitsTransfer -Source $Source -Destination $Destination -Description "Backup" -DisplayName "Backup"
}
}
End {
#Write-Host $Output
}
}
Function Copy-ItemWithProgress
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[string]$Source,
[Parameter(Mandatory=$true,Position=1)]
[string]$Destination,
[Parameter(Mandatory=$false,Position=2)]
[string]$Filter,
[Parameter(Mandatory=$false,Position=3)]
[switch]$Force
)
Begin{
$Source = $Source
#get the entire folder structure
$Filelist = Get-Childitem $Source -Recurse -Exclude $filter
#get the count of all the objects
$Total = $Filelist.count
#establish a counter
$Position = 0
}
Process{
#Stepping through the list of files is quite simple in PowerShell by using a For loop
foreach ($File in $Filelist)
{
#On each file, grab only the part that does not include the original source folder using replace
$Filename = ($File.Fullname).replace($Source,'')
#rebuild the path for the destination:
$DestinationFile = ($Destination+$Filename)
#get just the folder path
$DestinationPath = Split-Path $DestinationFile -Parent
#show progress
Show-ProgressStatus -Message "Copying data from $source to $Destination" -Step (($Position/$total)*100) -MaxStep $total
#create destination directories
If (-not (Test-Path $DestinationPath) ) {
New-Item $DestinationPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
}
#do copy (enforce)
Try{
Copy-Item $File.FullName -Destination $DestinationFile -Force:$Force -ErrorAction:$VerbosePreference -Verbose:($PSBoundParameters['Verbose'] -eq $true) | Out-Null
Write-Verbose ("Copied file [{0}] to [{1}]" -f $File.FullName,$DestinationFile)
}
Catch{
Write-Host ("Unable to copy file in {0} to {1}; Error: {2}" -f $File.FullName,$DestinationFile ,$_.Exception.Message) -ForegroundColor Red
break
}
#bump up the counter
$Position++
}
}
End{
Show-ProgressStatus -Message "Copy completed" -Step $total -MaxStep $total
}
}
function Copy-File {
[CmdletBinding(SupportsShouldProcess = $True)]
param(
[Alias("Source","Path")]
[string]$from,
[Alias("Destination","Dest")]
[string]$to,
[switch]$force
)
Begin
{
## Get the name of this function
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
if (-not $PSBoundParameters.ContainsKey('Verbose')) {
$VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
}
if (-not $PSBoundParameters.ContainsKey('Confirm')) {
$ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
}
if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
$WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
}
}
Process
{
If($force){Remove-Item $to -Force -ErrorAction SilentlyContinue -WhatIf:$WhatIfPreference | Out-Null}
If(!$WhatIfPreference){
$ffile = [io.file]::OpenRead($from)
$tofile = [io.file]::OpenWrite($to)
$srcFile = Split-Path $from -Leaf
#Create progress bar
Write-Progress -Activity "Copying file" -status "Copying $srcFile" -PercentComplete 0
#Begine file transfer with progress by bytes
try {
$sw = [System.Diagnostics.Stopwatch]::StartNew();
[byte[]]$buff = new-object byte[] (4096*1024)
[long]$total = [long]$count = 0
do {
$count = $ffile.Read($buff, 0, $buff.Length)
$tofile.Write($buff, 0, $count)
$total += $count
[int]$pctcomp = ([int]($total/$ffile.Length* 100));
[int]$secselapsed = [int]($sw.elapsedmilliseconds.ToString())/1000;
if ( $secselapsed -ne 0 ) {
[single]$xferrate = (($total/$secselapsed)/1mb);
} else {
[single]$xferrate = 0.0
}
if ($total % 1mb -eq 0) {
if($pctcomp -gt 0)`
{[int]$secsleft = ((($secselapsed/$pctcomp)* 100)-$secselapsed);
} else {
[int]$secsleft = 0};
Write-Progress -Activity ($pctcomp.ToString() + "% Copying file @ " + "{0:n2}" -f $xferrate + " MB/s") -status "Copying $srcFile" -PercentComplete $pctcomp -SecondsRemaining $secsleft;
}
} while ($count -gt 0)
$sw.Stop();
$sw.Reset();
}
finally {
Write-LogEntry ($srcFile + `
" copied in " + $secselapsed + " seconds at " + `
"{0:n2}" -f [int](($ffile.length/$secselapsed)/1mb) + " MB/s.") -Severity 1 -Source ${CmdletName} -Outhost
$ffile.Close();
$tofile.Close();
}
}
Else{
Write-host ("What if: Performing the operation `"Copy File`" on target `"Item: {0} Destination: {1}." -f $from.$to)
}
}
End{
Write-Progress -Activity "Copying files" -status "Done copying files" -PercentComplete 100
}
}
##*===========================================================================
##* VARIABLES
##*===========================================================================
# Use function to get paths because Powershell ISE and other editors have differnt results
$scriptPath = Get-ScriptPath
[string]$scriptDirectory = Split-Path $scriptPath -Parent
[string]$scriptName = Split-Path $scriptPath -Leaf
[string]$scriptBaseName = [System.IO.Path]::GetFileNameWithoutExtension($scriptName)
[int]$OSBuildNumber = (Get-WmiObject -Class Win32_OperatingSystem).BuildNumber
#build log name
[string]$FileName = $scriptBaseName +'.log'
#build global log fullpath
[string]$Global:LogFilePath = Join-Path (Get-SMSTSENV -ReturnLogPath -NoWarning) -ChildPath $FileName
Write-Host "logging to file: $LogFilePath" -ForegroundColor Cyan
If(Get-SMSTSENV){
$FILESHARE_USER = ($tsenv.Value("DomainAdminDomain") + "\" + $tsenv.Value("DomainAdmin"))
$FILESHARE_PASSWORD = $tsenv.Value("DomainAdminPassword")
}
##*===========================================================================
##* PREREQS
##*===========================================================================
$PrereqsReady = $true
#grab latest wim and its path
If($tsenv -and !$WIMSourceRoot){
$FindWIM = Get-ChildItem -Path "$($tsenv.Value("DeployRoot"))\Captures" -Filter $ImageName*.wim -Recurse | sort LastWriteTime | Select -First 1
}
Elseif($WIMSourceRoot){
$FindWIM = Get-ChildItem -Path "$WIMSourceRoot" -Filter $ImageName*.wim -Recurse | sort LastWriteTime | Select -First 1
}
Else{
$FindWIM = Get-ChildItem -Path "$scriptDirectory\WIM" -Filter $ImageName*.wim -Recurse | sort LastWriteTime | Select -First 1
}
#does captured wim exist
If($FindWIM){
$WIMPath = $FindWIM.FullName
$WIMName = ($FindWIM.BaseName -split "_|-|\s+")[0]
$WIMDate = $FindWIM.LastWriteTime.ToString("yyyy-MM-dd")
Write-LogEntry ("WIM file being used [{0}]" -f $WIMPath) -Severity 1 -Outhost
}
Else{
Write-LogEntry ("WIM file not found [{0}]" -f $WIMSourceRoot) -Severity 3 -Outhost
$PrereqsReady = $false
}
#Build path for oscdimg.exe
If(!$OscdimgPath){$OscdimgPath = Join-Path $scriptDirectory -ChildPath "Files"}
#Checking for oscdimg.exe. required to create ISO
Write-LogEntry ("Validating oscdimg path [{0}]..." -f $OscdimgPath) -Severity 1 -Outhost
If(-not(Test-Path "$OscdimgPath\oscdimg.exe") ){
Write-LogEntry ("Oscdimg.exe binary not found or not accessible") -Severity 3 -Outhost
$PrereqsReady = $false
}
Else{
$OscdimgPath = (Resolve-Path $OscdimgPath).Path
Write-LogEntry ("Resolved Oscdimg absolute path [{0}]..." -f $OscdimgPath) -Severity 0 -Outhost
}
#If SetupSource is specified, validate it
#if not use temp directory
If($SetupSource){
Write-LogEntry ("Validating setup source path [{0}]..." -f $SetupSource) -Severity 1 -Outhost
if(-not(Test-Path $SetupSource)){
Write-LogEntry ("Source path does not exist [{0}]" -f $SetupSource) -Severity 3 -Outhost
$PrereqsReady = $false
}
Else{
#resolve to absolute path always
$SetupSource = (Resolve-Path $SetupSource).Path
Write-LogEntry ("Resolved source absolute path [{0}]..." -f $SetupSource) -Severity 1 -Outhost
}
}
Else{
$SetupSource = "$env:Temp\$ImageName"
Write-LogEntry ("Source path set to [{0}]" -f $SetupSource) -Severity 0 -Outhost
}
#check if staging path is specified, if not use temp directory
If($StagingPath){
Write-LogEntry ("Validating staging path [{0}]..." -f $StagingPath) -Severity 1 -Outhost
if(-not(Test-Path $StagingPath)){
Write-LogEntry ("Staging path not found or not accessible") -Severity 3 -Outhost
$PrereqsReady = $false
}
Else{
#resolve to absolute path always
$StagingPath = (Resolve-Path $StagingPath).Path
Write-LogEntry ("Resolved staging absolute path [{0}]..." -f $StagingPath) -Severity 1 -Outhost
}
}
Else{
$StagingPath = "$env:Temp\$ImageName"
Write-LogEntry ("Staging path set to [{0}]" -f $StagingPath) -Severity 0 -Outhost
}
#check to see if either setup or staging has bootable files
If($Bootable){
Write-LogEntry ("Validating bootable files...") -Severity 1 -Outhost
#check if staging/source path is bootable and files exist: setup.exe and boot.wim, if not copy
If( ( (Test-Path "$SetupSource\setup.exe") -and (Test-Path "$SetupSource\sources\boot.wim") ) -and ( (-not(Test-Path "$StagingPath\setup.exe")) -or (-not(Test-Path "$StagingPath\sources\boot.wim")) ) ){
Write-LogEntry ("Copying source setup files from [{0}] to staging path [{1}]..." -f $SetupSource,$StagingPath) -Severity 1 -Outhost
Copy-ItemWithProgress -Source $SetupSource -Destination $StagingPath -Force -Filter install.wim
}
If( ( (-not(Test-Path "$SetupSource\setup.exe")) -and (-not(Test-Path "$SetupSource\sources\boot.wim")) ) -and ( (-not(Test-Path "$StagingPath\setup.exe")) -or (-not(Test-Path "$StagingPath\sources\boot.wim")) ) ){
Write-LogEntry ("Nether SetupSource [{0}] nor Staging path [{1}] have bootable files, Please extract a bootable ISO" -f $SetupSource,$StagingPath) -Severity 3 -Outhost
$PrereqsReady = $false
}
Else{
Write-LogEntry ("Bootable files are found...") -Severity 0 -Outhost
}
}
#Determine if ISOPath found and accessible
If($ISODestPath){
Try{
Write-LogEntry ("Validating ISO destination path [{0}]..." -f $ISODestPath) -Severity 1 -Outhost
if(-not(Test-Path $ISODestPath)){
Write-LogEntry ("ISO destination path not found or not accessible") -Severity 3 -Outhost
break
}
Else{
#resolve to absolute path always
$ISODestPath = (Resolve-Path $ISODestPath).Path
}
#Determine if ISOPath is pointing to a directory
If( (Get-Item $ISODestPath) -is [System.IO.DirectoryInfo]){
#build ISO
$ISODestPath = Join-Path $ISODestPath -ChildPath ($WIMName + "_Patched_"+ $WIMDate +".ISO")
Write-LogEntry ("Resolved ISO destination path [{0}]..." -f $ISODestPath) -Severity 1 -Outhost
}
}
Catch [System.UnauthorizedAccessException] {
Write-LogEntry ("ISO destination path is unaccessible") -Severity 3 -Outhost
If( ($FILESHARE_USER -ne $null) -and ($FILESHARE_PASSWORD -ne $null) ){
$SecurePassword = ConvertTo-SecureString $FILESHARE_PASSWORD -AsPlainText -Force
$FileShareCredential = New-Object System.Management.Automation.PSCredential ($FILESHARE_USER, $SecurePassword)
Try{
New-PSDrive -Name $FILESHARE_PS_DRIVE -PSProvider FileSystem -Root $ISODestPath -Persist -Credential $FileShareCredential
}
Catch{
Write-Output "Unable to map drive $FILESHARE_PS_DRIVE to [$ISODestPath]; credentials may be invalid"
Write-Output $_.Exception
$PrereqsReady = $false
}
#Determine if ISOPath is pointing to a directory
If( (Get-Item $ISODestPath) -is [System.IO.DirectoryInfo]){
#build ISO
$ISODestPath = Join-Path $ISODestPath -ChildPath ($WIMName + "_Patched_"+ $WIMDate +".ISO")
}
}
Else{
$PrereqsReady = $false
}
}
Catch [System.Management.Automation.ItemNotFoundException] {
Write-LogEntry ("ISO destination path not found") -Severity 3 -Outhost
$PrereqsReady = $false
}
Catch {
# An unexpected error
Write-LogEntry $_.Exception -Severity 3 -Outhost
$Error[0]
$PrereqsReady = $false
}
}
Else{
$PrereqsReady = $false
}
##*===========================================================================
##* MAIN
##*===========================================================================
If($PrereqsReady){
#Copy wim file is hash is different
Copy-LatestFile -SourceFile $WIMPath -DestFile "$StagingPath\Sources\install.wim" -Compareby Hash
#Use specified Boot files, otherwise default to default Staging paths
If($BootFilePath -and $EFIBootFilePath){
$BootFile = "$BootFilePath\etfsboot.com"
$EFIBootFile = "$EFIBootFilePath\efisys.bin"
}
Else{
$BootFile = "$StagingPath\boot\etfsboot.com"
$EFIBootFile = "$StagingPath\efi\Microsoft\boot\efisys.bin"
}
#if Set to Bootable and required boot files are valid, build boot image
If($Bootable -and (Test-Path $BootFile) -and (Test-Path $EFIBootFile)){
#Build bootdata params
$BootData=('2#p0,e,b"{0}"#pEF,e,b"{1}"' -f $BootFile,$EFIBootFile)
Write-LogEntry ("Building bootable ISO [{0}]" -f $ISODestPath) -Severity 2 -Outhost
Write-Host "OSCDIMG COMMAND: $OscdimgPath\oscdimg.exe -m -o -h -bootdata:$BootData -u2 -udfver102 '$StagingPath' '$ISODestPath'" -Verbose
$Proc = Start-Process -FilePath "$OscdimgPath\oscdimg.exe" -ArgumentList @('-m','-o','-h',"-bootdata:$BootData",'-u2','-udfver102',"-l$ImageName","$StagingPath","$ISODestPath") -PassThru -NoNewWindow -RedirectStandardOutput "$env:temp\ocsdimg_stdout.txt" -RedirectStandardError "$env:temp\ocsdimg_stderr.txt"
}
Else{
Write-LogEntry ("Building non-bootable ISO [{0}]" -f $ISODestPath) -Severity 2 -Outhost
Write-Host "OSCDIMG COMMAND: $OscdimgPath\oscdimg.exe -m -o -h -u2 -u2 -udfver102 '$StagingPath' '$ISODestPath'" -Verbose
$Proc = Start-Process -FilePath "$OscdimgPath\oscdimg.exe" -ArgumentList @('-m','-o','-h','-u2','-udfver102',"-l$ImageName","$StagingPath","$ISODestPath") -PassThru -NoNewWindow -RedirectStandardOutput "$env:temp\ocsdimg_stdout.txt" -RedirectStandardError "$env:temp\ocsdimg_stderr.txt"
}
#As the ocsdimg is running (in the brackground)
#Read the output of the oscdimg process to pull the percentage
While (!($Proc.HasExited)){
Start-Sleep -Seconds 3
$content = Get-Content -Path "$env:temp\ocsdimg_stderr.txt" -Tail 1
If($content -like "*% Complete"){
$status = (($content -replace "% Complete","") | Out-String).Trim()
[int]$status = $status
}
Show-ProgressStatus -Message ("Creating ISO [{0}]..." -f $ISODestPath) -Step $status -MaxStep 100
}
$Proc.ExitCode
#if process doesn't exist correctly
#if($Proc.ExitCode -ne 0){Write-LogEntry ("Failed to generate ISO with exitcode: {0}" -f $Proc.ExitCode) -Severity 3 -Outhost}
#cleanup temp location
If($CleanupTemp){
If(Test-Path "$env:Temp\$ImageName"){
Remove-Item "$env:Temp\$ImageName" -Recurse -Force | Out-Null
Write-LogEntry ("Remove temp directories" -f "$env:Temp\$ImageName") -Severity 2 -Outhost
}
}
}
Else{
Write-LogEntry ("Prereqs failed, unable to build ISO. Check log for more details") -Severity 3 -Outhost
}