forked from dsx42/UnattendTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnattendTool.ps1
1724 lines (1572 loc) · 69.5 KB
/
UnattendTool.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
param(
$Language = 'zh-CN',
$OsVersion = 11,
$WindowsProductName = 'Enterprise',
$Architecture = 'x64',
$DiskId = -1,
$PartitionId = -1,
$PartitionStyle = 'GPT',
$FullName = 'MyPC',
$Password = '',
$DriverLetter = '',
$ISOPath = '',
[switch]$ByPassCheck,
[switch]$Interactive,
[switch]$NotFormat,
[switch]$Version
)
function GetDefaultFolderPath {
return [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
}
function GetPartitionTypeName {
param($GptType, $MbrType, $FileSystem)
if ($GptType) {
if ($GptType -ieq '{de94bba4-06d1-4d40-a16a-bfd50179d6ac}') {
return '微软恢复分区'
}
elseif ($GptType -ieq '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}') {
return 'EFI 系统分区'
}
elseif ($GptType -ieq '{e3c9e316-0b5c-4db8-817d-f92df00215ae}') {
return '微软保留分区'
}
elseif ($GptType -ieq '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}') {
return '基本数据分区'
}
elseif ($GptType -ieq '{af9b60a0-1431-4f62-bc68-3311714a69ad}') {
return '逻辑磁盘管理器元数据分区'
}
elseif ($GptType -ieq '{af9b60a0-1431-4f62-bc68-3311714a69ad}') {
return '逻辑磁盘管理器数据分区'
}
}
if ($MbrType) {
if ($MbrType -ieq '1') {
return 'FAT12 分区'
}
elseif ($MbrType -ieq '4') {
return 'FAT16 分区'
}
elseif ($MbrType -ieq '5') {
return '扩展分区'
}
elseif ($MbrType -ieq '6') {
return '逻辑分区'
}
elseif ($MbrType -ieq '7') {
return "$FileSystem 分区"
}
elseif ($MbrType -ieq '12') {
return 'FAT32 分区'
}
}
return '未知分区'
}
function GetCurrentDisk {
$CurrentDisks = [ordered]@{}
Get-Disk | ForEach-Object {
$Partitions = [ordered]@{}
$PhydicalDisk = Get-PhysicalDisk -FriendlyName $_.FriendlyName
Get-Partition -DiskNumber $_.DiskNumber | ForEach-Object {
$Volume = Get-Volume -Partition $_
$PartitionTypeName = GetPartitionTypeName -GptType $_.GptType -MbrType $_.MbrType `
-FileSystem $Volume.FileSystem
$Partition = @{
'Guid' = $_.Guid; # 分区 ID
'PartitionNumber' = $_.PartitionNumber; # 分区编号,值从 1 开始
'DiskNumber' = $_.DiskNumber; # 所属硬盘编号,值从 0 开始
'GptType' = $_.GptType; # 分区类型的 ID
'DriveLetter' = $_.DriveLetter; # 驱动器号,如 C
'FileSystem' = $Volume.FileSystem; # 分区文件系统类型
'FileSystemType' = $Volume.FileSystemType; # 分区文件系统类型
'SizeRemaining' = $Volume.SizeRemaining; # 分区可用空间大小,单位为 Byte
'Size' = $_.Size; # 分区容量,单位为 Byte
'Type' = $_.Type; # GPT 分区类型,System 表示 EFI 分区,Basic 表示基本数据分区,Reserved 表示 MSR 保留分区
'TypeName' = $PartitionTypeName;
'MbrType' = $_.MbrType; # MBR 分区类型的 ID
'IsHidden' = $_.IsHidden; # 是否隐藏分区
'IsBoot' = $_.IsBoot; # 是否启动分区
'IsSystem' = $_.IsSystem; # 是否 EFI 系统分区
'IsActive' = $_.IsActive # 是否活动分区,MBR 才有意义,GPT 无意义
}
$Partitions.Add($_.PartitionNumber, $Partition)
}
$Disk = @{
'DiskNumber' = $_.DiskNumber; # 硬盘编号,值从 0 开始
'PartitionStyle' = $_.PartitionStyle; # 分区类型,如 GPT 或 MBR
'MediaType' = $PhydicalDisk.MediaType; # 硬盘类型,如 SSD 或 HDD
'OperationalStatus' = $_.OperationalStatus; # 硬盘状态,如 Online
'HealthStatus' = $_.HealthStatus; # 硬盘健康状态,如 Healthy
'BusType' = $_.BusType; # 硬盘接口类型,如 RAID 或 USB
'BootFromDisk' = $_.BootFromDisk; # 是否从该硬盘启动
'FirmwareVersion' = $_.FirmwareVersion; # 硬盘固件版本
'FriendlyName' = $_.FriendlyName; # 硬盘名称
'IsBoot' = $_.IsBoot; # 是否从该硬盘启动
'IsSystem' = $_.IsSystem; # 是否系统盘
'Manufacturer' = $_.Manufacturer; # 硬盘制造商
'Model' = $_.Model; # 硬盘型号
'NumberOfPartitions' = $_.NumberOfPartitions; # 硬盘分区数量
'Size' = $_.Size; # 硬盘容量,单位为 Byte
'AllocatedSize' = $PhydicalDisk.AllocatedSize; # 已分配容量,单位为 Byte
'Partitions' = $Partitions
}
$CurrentDisks.Add($_.DiskNumber, $Disk)
}
return $CurrentDisks
}
function GetSystemDiskId {
$CurrentDisks = GetCurrentDisk
foreach ($_ in $CurrentDisks.GetEnumerator()) {
if ($_.Value['IsBoot']) {
return $_.Value['DiskNumber']
}
}
return 0
}
function GetSystemPartitionId {
$CurrentDisks = GetCurrentDisk
foreach ($_ in $CurrentDisks.GetEnumerator()) {
foreach ($__ in $_.Value['Partitions'].GetEnumerator()) {
if ($__.Value['IsBoot']) {
return $__.Value['PartitionNumber']
}
}
}
return 1
}
function FormatSize {
param($Size)
$Tb = [Math]::Round($Size * 1.0 / 1024 / 1024 / 1024 / 1024, 2)
if ($Tb -gt 1) {
return "$Tb" + ' TB'
}
$Gb = [Math]::Round($Size * 1.0 / 1024 / 1024 / 1024, 2)
if ($Gb -gt 1) {
return "$Gb" + ' GB'
}
$Mb = [Math]::Round($Size * 1.0 / 1024 / 1024, 2)
if ($Mb -gt 1) {
return "$Mb" + ' MB'
}
$Kb = [Math]::Round($Size * 1.0 / 1024, 2)
if ($Kb -gt 1) {
return "$Kb" + ' KB'
}
if ($Size -le 0) {
return '0 Byte'
}
return "$Size" + ' Byte'
}
function ShowLanguageSelect {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '============================'
Write-Host -Object '选择要安装系统的语言,推荐 1' -ForegroundColor Green
Write-Host -Object '============================'
Write-Host -Object ''
Write-Host -Object '1: 简体中文 zh-CN' -ForegroundColor Green
Write-Host -Object ''
Write-Host -Object '2: 英文 en-US'
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入选择的序号(默认为 1),按回车键确认'
if ($InputOption -ieq '' -or $InputOption -ieq '1') {
Write-Host -Object ''
return 'zh-CN'
}
elseif ($InputOption -ieq '2') {
Write-Host -Object ''
return 'en-US'
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowOsVersionSelect {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '============================'
Write-Host -Object '选择要安装系统的版本,推荐 1' -ForegroundColor Green
Write-Host -Object '============================'
Write-Host -Object ''
Write-Host -Object '1: Windows 11' -ForegroundColor Green
Write-Host -Object ''
Write-Host -Object '2: Windows 10'
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入选择的序号(默认为 1),按回车键确认'
if ($InputOption -ieq '' -or $InputOption -ieq '1') {
Write-Host -Object ''
return 11
}
elseif ($InputOption -ieq '2') {
Write-Host -Object ''
return 10
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowWindowsProductNameSelect {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '============================'
Write-Host -Object '选择要安装系统的产品,推荐 1' -ForegroundColor Green
Write-Host -Object '============================'
Write-Host -Object ''
Write-Host -Object '1: 专业版 Pro' -ForegroundColor Green
Write-Host -Object ''
Write-Host -Object '2: 教育版 Education'
Write-Host -Object ''
Write-Host -Object '3: 企业版 Enterprise'
Write-Host -Object ''
Write-Host -Object '4: 专业教育版 Pro Education'
Write-Host -Object ''
Write-Host -Object '5: 专业工作站版 Pro For Workstations'
Write-Host -Object ''
Write-Host -Object '6: 其他'
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入选择的序号(默认为 1),按回车键确认'
if ($InputOption -ieq '' -or $InputOption -ieq '1') {
Write-Host -Object ''
return 'Pro'
}
elseif ($InputOption -ieq '2') {
Write-Host -Object ''
return 'Education'
}
elseif ($InputOption -ieq '3') {
Write-Host -Object ''
return 'Enterprise'
}
elseif ($InputOption -ieq '4') {
Write-Host -Object ''
return 'Pro Education'
}
elseif ($InputOption -ieq '5') {
Write-Host -Object ''
return 'Pro For Workstations'
}
elseif ($InputOption -ieq '6') {
Write-Host -Object ''
return ''
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowByPassSelect {
param($OsVersion)
if ($OsVersion -eq 10) {
return $false
}
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '======================================'
Write-Host -Object '选择系统安装时是否跳过硬件检测,推荐 0' -ForegroundColor Green
Write-Host -Object '======================================'
Write-Host -Object ''
Write-Host -Object '0: 否' -ForegroundColor Green
Write-Host -Object ''
Write-Host -Object '1: 是'
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入选择的序号(默认为 0),按回车键确认'
if ($InputOption -ieq '' -or $InputOption -ieq '0') {
Write-Host -Object ''
return $false
}
elseif ($InputOption -ieq '1') {
Write-Host -Object ''
return $true
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowArchitectureSelect {
param($OsVersion)
if ($OsVersion -eq 11) {
return 'x64'
}
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '============================'
Write-Host -Object '选择要安装系统的架构,推荐 1' -ForegroundColor Green
Write-Host -Object '============================'
Write-Host -Object ''
Write-Host -Object '1: 64 位系统 x64' -ForegroundColor Green
Write-Host -Object ''
Write-Host -Object '2: 32 位系统 x86'
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入选择的序号(默认为 1),按回车键确认'
if ($InputOption -ieq '' -or $InputOption -ieq '1') {
Write-Host -Object ''
return 'x64'
}
elseif ($InputOption -ieq '2') {
Write-Host -Object ''
return 'x86'
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowDiskIdSelect {
$SystemDiskId = GetSystemDiskId
$CurrentDisks = GetCurrentDisk
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '================================================'
Write-Host -Object "选择要安装系统的硬盘编号,推荐当前系统所在硬盘 $SystemDiskId" -ForegroundColor Green
Write-Host -Object '================================================'
Write-Host -Object ''
Write-Host -Object '当前系统识别到的硬盘如下:绿色字体的硬盘为当前系统所在硬盘,绿色字体的分区为当前系统所在分区'
$CurrentDisks.GetEnumerator() | ForEach-Object {
$msg = '硬盘编号: ' + $_.Value['DiskNumber'] + ', 硬盘类型: ' + $_.Value['MediaType'] + ', 接口类型: ' `
+ $_.Value['BusType'] + ', 分区类型: ' + $_.Value['PartitionStyle'] + ', 硬盘容量: ' `
+ (FormatSize -Size $_.Value['Size']) + ', 硬盘名称: ' + $_.Value['FriendlyName']
Write-Host -Object ''
if ($_.Value['IsBoot']) {
Write-Host -Object $msg -ForegroundColor Green
}
else {
Write-Host -Object $msg
}
$_.Value['Partitions'].GetEnumerator() | ForEach-Object {
$msg = ' |- 分区编号: ' + $_.Value['PartitionNumber'] + ', 驱动器: ' + $_.Value['DriveLetter'] `
+ ', 是否隐藏: ' + $_.Value['IsHidden'] + ', 文件系统: ' + $_.Value['FileSystem'] + ', 类型: ' `
+ $_.Value['TypeName'] + ', 可用空间: ' + (FormatSize -Size $_.Value['SizeRemaining']) + ', 容量: ' `
+ ( FormatSize -Size $_.Value['Size'])
Write-Host -Object ' |'
if ($_.Value['IsBoot']) {
Write-Host -Object $msg -ForegroundColor Green
}
else {
Write-Host -Object $msg
}
}
}
while ($true) {
Write-Host -Object ''
try {
[System.Int32]$InputOption = Read-Host `
-Prompt "请输入选择的硬盘编号(硬盘编号从 0 开始,默认为 $SystemDiskId),按回车键确认"
if ($InputOption -ge 0) {
Write-Host -Object ''
return $InputOption
}
else {
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
}
}
catch {
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
}
}
}
function ShowWipeDiskSelect {
param($DiskId)
$CurrentDisks = GetCurrentDisk
$SelectDisk = $CurrentDisks[$DiskId]
$DefalultSelect = 0
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
if ($SelectDisk -and $SelectDisk['PartitionStyle'] -ine 'GPT') {
Write-Host -Object '=================================='
Write-Host -Object '选择是否对所选硬盘进行分区,推荐 1' -ForegroundColor Green
Write-Host -Object '=================================='
$DefalultSelect = 1
}
else {
Write-Host -Object '=================================='
Write-Host -Object '选择是否对所选硬盘进行分区,推荐 0' -ForegroundColor Green
Write-Host -Object '=================================='
$DefalultSelect = 0
}
Write-Host -Object ''
Write-Host -Object '0: 否' -ForegroundColor Green
Write-Host -Object ''
Write-Host -Object '1: GPT 分区,注意:安装系统时会清除所选硬盘的数据,请及时备份所选硬盘的数据' -ForegroundColor Red
Write-Host -Object ''
Write-Host -Object '2: MBR 分区,注意:安装系统时会清除所选硬盘的数据,请及时备份所选硬盘的数据' -ForegroundColor Red
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt "请输入选择的序号(默认为 $DefalultSelect),按回车键确认"
if ($InputOption -ieq '') {
Write-Host -Object ''
return $DefalultSelect
}
elseif ('0' -ieq $InputOption) {
Write-Host -Object ''
return 0
}
elseif ($InputOption -ieq '1') {
Write-Host -Object ''
return 1
}
elseif ($InputOption -ieq '2') {
Write-Host -Object ''
return 2
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowNewPartition {
param($CreatePartitionInfo)
Write-Host -Object '所选硬盘重新分区如下:绿色字体的分区为系统安装分区'
Write-Host -Object ''
$CreatePartitionInfo.GetEnumerator() | ForEach-Object {
$VolumeSize = '硬盘剩余所有空间'
if (!$_.Value['Extend']) {
$VolumeSize = FormatSize -Size $($_.Value['Size'] * 1024 * 1024)
}
$msg = '分区编号: ' + $_.Value['Order'] + ', 是否隐藏: ' + $_.Value['IsHidden'] + ', 文件系统: ' `
+ $_.Value['FileSystem'] + ', 类型: ' + $_.Value['TypeName'] + ', 容量: ' + $VolumeSize
if ($_.Value['IsBoot']) {
Write-Host -Object $msg -ForegroundColor Green
}
else {
Write-Host -Object $msg
}
Write-Host -Object ''
}
}
function ShowAddNewPartition {
while ($true) {
$InputOption = Read-Host -Prompt '是否增加新分区(0: 否, 1: 是),按回车键确认'
Write-Host -Object ''
if ($InputOption -ieq '0') {
return $false
}
elseif ($InputOption -ieq '1') {
return $true
}
else {
Write-Warning -Message '输入无效,请重新输入'
Write-Host -Object ''
}
}
}
function ShowIsBoot {
param($CreatePartitionInfo)
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '================================================'
Write-Host -Object '选择要安装系统的分区编号,类型必须为基本数据分区' -ForegroundColor Green
Write-Host -Object '================================================'
Write-Host -Object ''
ShowNewPartition -CreatePartitionInfo $CreatePartitionInfo
while ($true) {
$InputOption = Read-Host -Prompt '请输入选择的分区编号(分区编号从 1 开始,类型必须为基本数据分区),按回车键确认'
$SelectPartition = $CreatePartitionInfo[$InputOption]
if ($SelectPartition) {
if ($SelectPartition['Type'] -ieq 'Primary') {
Write-Host -Object ''
$SelectPartition['IsBoot'] = $true
$Script:PartitionId = $SelectPartition['Order']
return
}
else {
Write-Host -Object ''
Write-Warning -Message '所选分区非基本数据分区,只能选择基本数据分区,请重新输入'
Write-Host -Object ''
}
}
else {
Write-Host -Object ''
Write-Warning -Message '所选分区不存在,请重新输入'
Write-Host -Object ''
}
}
}
function ShowCreatePartition {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
if (1 -eq $script:WipeDisk) {
$CreatePartitionInfo = [ordered]@{
'1' = @{
'Order' = 1;
'Size' = 100;
'Type' = 'EFI';
'TypeName' = 'EFI 分区';
'FileSystem' = 'FAT32';
'Extend' = $false;
'IsHidden' = $true;
'IsBoot' = $false
}
#'2' = @{
# 'Order' = 2;
# 'Size' = 128;
# 'Type' = 'MSR';
# 'TypeName' = '微软保留分区';
# 'FileSystem' = '';
# 'Extend' = $false;
# 'IsHidden' = $true;
# 'IsBoot' = $false
#}
}
$PartitionNumber = 1
Write-Host -Object '==============='
Write-Host -Object '创建新 GPT 分区' -ForegroundColor Green
Write-Host -Object '==============='
Write-Host -Object ''
}
else {
$CreatePartitionInfo = [ordered]@{}
$PartitionNumber = 0
Write-Host -Object '======================================'
Write-Host -Object '创建新 MBR 分区,最多支持创建 4 个分区' -ForegroundColor Green
Write-Host -Object '======================================'
Write-Host -Object ''
}
ShowNewPartition -CreatePartitionInfo $CreatePartitionInfo
while ($true) {
try {
$PartitionNumber = $PartitionNumber + 1
[System.Int32]$InputOption = Read-Host `
-Prompt "输入第 $PartitionNumber 个分区的大小,0 表示占用硬盘所有剩余空间(默认为 0),单位为 MB,按回车键确认"
if ($InputOption -eq 0) {
Write-Host -Object ''
$CreatePartitionInfo.Add([System.String]$PartitionNumber, @{
'Order' = $PartitionNumber;
'Size' = 0;
'Type' = 'Primary';
'TypeName' = '基本数据分区';
'FileSystem' = 'NTFS';
'Extend' = $true;
'IsHidden' = $false;
'IsBoot' = $false
})
ShowNewPartition -CreatePartitionInfo $CreatePartitionInfo
break
}
elseif ($InputOption -gt 0) {
Write-Host -Object ''
$CreatePartitionInfo.Add([System.String]$PartitionNumber, @{
'Order' = $PartitionNumber;
'Size' = $InputOption;
'Type' = 'Primary';
'TypeName' = '基本数据分区';
'FileSystem' = 'NTFS';
'Extend' = $false;
'IsHidden' = $false;
'IsBoot' = $false
})
ShowNewPartition -CreatePartitionInfo $CreatePartitionInfo
}
else {
$PartitionNumber = $PartitionNumber - 1
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
Write-Host -Object ''
continue
}
if (2 -eq $script:WipeDisk -and $PartitionNumber -ge 4) {
$ShowAddNewPartition = $false
}
else {
$ShowAddNewPartition = ShowAddNewPartition
}
if (!$ShowAddNewPartition) {
$CreatePartitionInfo["$PartitionNumber"]['Extend'] = $true
break
}
}
catch {
$PartitionNumber = $PartitionNumber - 1
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
Write-Host -Object ''
}
}
ShowIsBoot -CreatePartitionInfo $CreatePartitionInfo
ShowNewPartition -CreatePartitionInfo $CreatePartitionInfo
return $CreatePartitionInfo
}
function ShowPartitionIdSelect {
param($DiskId)
$DefalultSelect = GetSystemPartitionId
$CurrentDisks = GetCurrentDisk
$SelectDisk = $CurrentDisks[$DiskId]
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
if ($SelectDisk) {
Write-Host -Object '================================================'
Write-Host -Object "选择要安装系统的分区编号,推荐当前系统所在分区 $DefalultSelect" -ForegroundColor Green
Write-Host -Object '================================================'
Write-Host -Object ''
Write-Host -Object '所选硬盘的分区如下:绿色字体的分区为当前系统所在分区'
$SelectDisk['Partitions'].GetEnumerator() | ForEach-Object {
$msg = '分区编号: ' + $_.Value['PartitionNumber'] + ', 驱动器: ' + $_.Value['DriveLetter'] + ', 是否隐藏: ' `
+ $_.Value['IsHidden'] + ', 文件系统: ' + $_.Value['FileSystem'] + ', 类型: ' + $_.Value['TypeName'] `
+ ', 可用空间: ' + (FormatSize -Size $_.Value['SizeRemaining']) + ', 容量: ' `
+ (FormatSize -Size $_.Value['Size'])
Write-Host -Object ''
if ($_.Value['IsBoot']) {
Write-Host -Object $msg -ForegroundColor Green
}
else {
Write-Host -Object $msg
}
}
}
else {
Write-Host -Object '================================'
Write-Host -Object "选择要安装系统的分区编号,推荐 $DefalultSelect" -ForegroundColor Green
Write-Host -Object '================================'
}
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt "请输入选择的分区编号(分区编号从 1 开始,默认为 $DefalultSelect),按回车键确认"
if ($InputOption -ieq '') {
Write-Host -Object ''
return $DefalultSelect
}
try {
[System.Int32]$InputOption1 = [System.Int32]$InputOption
if ($InputOption1 -eq 0) {
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
continue
}
if ($InputOption1 -ge 1) {
Write-Host -Object ''
return $InputOption1
}
else {
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
}
}
catch {
Write-Host -Object ''
Write-Warning -Message '输入无效,请重新输入'
}
}
}
function ShowFomatSelect {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '================================'
Write-Host -Object '选择是否对所选分区格式化,推荐 1' -ForegroundColor Green
Write-Host -Object '================================'
Write-Host -Object ''
Write-Host -Object '0: 否'
Write-Host -Object ''
Write-Host -Object '1: 是,注意:安装系统时会清除所选分区的数据,请及时备份所选分区的数据' -ForegroundColor Red
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入选择的序号(默认为 1),按回车键确认'
if ($InputOption -ieq '' -or $InputOption -ieq '1') {
Write-Host -Object ''
return $true
}
elseif ($InputOption -ieq '0') {
Write-Host -Object ''
return $false
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowPartitionStyleSelect {
param($DiskId)
$CurrentDisks = GetCurrentDisk
$SelectDisk = $CurrentDisks[$DiskId]
$DefalultSelect = 1
$DefaultPartitionStyle = 'GPT'
if ($SelectDisk -and $SelectDisk['PartitionStyle'] -ine 'GPT') {
$DefalultSelect = 2
$DefaultPartitionStyle = 'MBR'
}
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '===================================='
Write-Host -Object "请确认所选硬盘分区的分区类型,推荐 $DefalultSelect" -ForegroundColor Green
Write-Host -Object '===================================='
Write-Host -Object ''
Write-Host -Object '1: GPT 分区'
Write-Host -Object ''
Write-Host -Object '2: MBR 分区'
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt "请输入选择的序号(默认为 $DefalultSelect),按回车键确认"
if ($InputOption -ieq '') {
Write-Host -Object ''
return $DefaultPartitionStyle
}
if ($InputOption -ieq '1') {
Write-Host -Object ''
return 'GPT'
}
elseif ($InputOption -ieq '2') {
Write-Host -Object ''
return 'MBR'
}
else {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
}
}
function ShowNameInput {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '=================================================='
Write-Host -Object '输入系统安装后的登录账号名,账号名建议符合如下要求' -ForegroundColor Green
Write-Host -Object '=================================================='
Write-Host -Object ''
Write-Host -Object '1: 推荐英文字母或数字的组合,尽量不使用空格或其他特殊字符' -ForegroundColor Yellow
Write-Host -Object ''
Write-Host -Object '2: 尽量不使用中文,防止某些应用软件不支持中文而无法使用' -ForegroundColor Yellow
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入登录账号名(默认为 MyPC),按回车键确认'
if ($InputOption -ieq '') {
Write-Host -Object ''
return 'MyPC'
}
else {
Write-Host -Object ''
return $InputOption
}
}
function ShowPasswordInput {
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '======================================================================'
Write-Host -Object '输入系统安装后的登录账号密码,推荐不设置密码,系统安装后再自行设置密码' -ForegroundColor Green
Write-Host -Object '======================================================================'
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入登录账号密码(默认无密码),按回车键确认'
if ($InputOption -ieq '') {
Write-Host -Object ''
return ''
}
else {
Write-Host -Object ''
return $InputOption
}
}
function ShowDriverLetterSelect {
$CurrentDisks = GetCurrentDisk
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '=================================='
Write-Host -Object '输入启动盘盘符或者镜像文件所在目录' -ForegroundColor Green
Write-Host -Object '=================================='
Write-Host -Object ''
Write-Host -Object '当前系统识别到的驱动器如下:'
$CurrentDisks.GetEnumerator() | ForEach-Object {
$msg = '硬盘类型: ' + $_.Value['MediaType'] + ', 接口类型: ' + $_.Value['BusType'] + ', 分区类型: ' `
+ $_.Value['PartitionStyle'] + ', 硬盘容量: ' + (FormatSize -Size $_.Value['Size']) + ', 硬盘名称: ' `
+ $_.Value['FriendlyName']
Write-Host -Object ''
Write-Host -Object $msg
$_.Value['Partitions'].GetEnumerator() | ForEach-Object {
if ($_.Value['DriveLetter']) {
$msg = ' |- 驱动器: ' + $_.Value['DriveLetter'] + ', 文件系统: ' + $_.Value['FileSystem'] `
+ ', 可用空间: ' + (FormatSize -Size $_.Value['SizeRemaining']) + ', 容量: ' `
+ (FormatSize -Size $_.Value['Size'])
Write-Host -Object ' |'
Write-Host -Object $msg
}
}
}
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host `
-Prompt '请输入启动盘盘符或者镜像文件所在目录(0 表示将应答文件保存到当前用户的桌面上),按回车键确认'
if ($InputOption -ieq '') {
Write-Host -Object ''
Write-Warning -Message '选择无效,请重新输入'
}
elseif ($InputOption -ieq '0') {
Write-Host -Object ''
return ''
}
elseif (Test-Path -Path "$InputOption" -PathType Container) {
Write-Host -Object ''
return $InputOption
}
elseif (Test-Path -Path $($InputOption + ':\') -PathType Container) {
Write-Host -Object ''
return $($InputOption + ':\')
}
else {
Write-Host -Object ''
Write-Warning -Message '输入路径不存在,请重新输入'
}
}
}
function ShowGetISOPath {
param($Path)
Clear-Host
Write-Host -Object ''
Write-Host -Object "=====> UnattendTool v$VersionInfo https://github.com/dsx42/UnattendTool <====="
Write-Host -Object ''
Write-Host -Object '==============================='
Write-Host -Object '选择使用应答文件的 ISO 镜像文件' -ForegroundColor Green
Write-Host -Object '==============================='
Write-Host -Object ''
Write-Host -Object '搜索 ISO 镜像文件中......'
$ISOFiles = [ordered]@{}
$Index = 0;
try {
Get-ChildItem -Path "$Path" -Filter '*.iso' -Recurse -Depth 1 -File -ErrorAction SilentlyContinue | `
ForEach-Object {
$Index = $Index + 1
$ISOFiles.Add([System.String]$Index, $_.FullName)
}
}
catch {
$msg = $Path + ' 无权限'
Write-Host -Object ''
Write-Warning -Message $msg
}
if ($ISOFiles.Count -gt 0) {
Write-Host -Object ''
Write-Host -Object '搜索到的镜像文件如下:'
$ISOFiles.GetEnumerator() | ForEach-Object {
Write-Host -Object ''
$msg = '镜像文件序号: ' + $_.Key + ', 镜像文件路径: ' + $_.Value
Write-Host -Object $msg -ForegroundColor Green
}
while ($true) {
Write-Host -Object ''
$InputOption = Read-Host -Prompt '请输入使用应答文件的 ISO 镜像文件序号,按回车键确认'
$ISOFile = $ISOFiles[$InputOption]
if ($ISOFile) {
Write-Host -Object ''