/
New-LabVm.ps1
865 lines (728 loc) · 36.7 KB
/
New-LabVm.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
[CmdletBinding()]
Param(
# Param1 VM new name
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ValueFromRemainingArguments = $false,
HelpMessage = 'Name of the Virtual Machine. This name will be used for the VM name on Hyper-V and for the Windows host name.',
Position = 0)]
[ValidateNotNullOrEmpty()]
[ValidateLength(0, 20)]
[string]
$vmName,
# Param2 integer representing the OS type of the VM
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ValueFromRemainingArguments = $false,
HelpMessage = 'Type of OS for the Virtual Machine.',
Position = 1)]
[ValidateNotNullOrEmpty()]
[ValidateSet('Win10', 'Win11', 'W2k19', 'W2k19-CORE', 'W2022', 'W2022-CORE')]
[string]
$vmOsType,
# Param3 Data File
[Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ValueFromRemainingArguments = $false,
HelpMessage = 'File containing the configuration data of the new VM (just in case VM name exists)',
Position = 2)]
[string]
$DataFile
)
<#
.Synopsis
Create a MOD virtual machine
.DESCRIPTION
Create a MOD (modified) virtual machine by providing only 3 parameters.
This script is harcoded to use my laptop virtual environment; this must be changed to fit your own needs.
All VMs go into C:\VMs
Any created VM has a differential disk based on existing master disks
Win 10, Win 11, Win 2019, Win 2019 Core, Win 2022, Win 2022 Core
The Windows Hostname will be the same as the VM name provided.
.EXAMPLE
To create Win 8.1 called VMWIN8
New-LabVm -vmName VMWIN8 -vmOsType 'Win11'
.EXAMPLE
To create Win Server 2022 Core called Srv01
New-LabVm Srv01 'W2022-CORE'
.PARAMETER vmName
Name of the Virtual Machine. This name will be used for the VM name on Hyper-V and for the Windows host name.
.PARAMETER vmOsType
Type of OS for the Virtual Machine.
.PARAMETER DataFile
File containing the configuration data of the new VM (just in case VM name exists)
.NOTES
Version: 1.7
DateModified: 8/Mar/2024
LasModifiedBy: Vicente Rodriguez Eguibar
vicente@eguibarIT.com
Eguibar Information Technology S.L.
http://www.eguibarit.com
#>
Set-StrictMode -Version Latest
#Check if script is running as ADMINISTRATOR
[bool]$IsAdmin = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match 'S-1-5-32-544')
If (-not $IsAdmin) {
Write-Error -Message 'This script must be executed as ADMINISTRATOR!!!'
Exit
}
################################################################################
# Constants
$VmFolder = 'C:\VMs'
If (-not $PSBoundParameters['DataFile']) {
Write-Verbose -Message 'Getting default location of Data file.'
$DataFile = 'C:\VMs\MainData.psd1'
} else {
Write-Verbose -Message ('Using {0} Data file.' -f $PSBoundParameters['DataFile'])
}
################################################################################
# Variables
[System.String]$VmSwitchName = 'LAN'
[System.Int32]$ProcessorCount = 2
$VM = $null
$Splat = $null
$PC = $null
[System.Int64]$MemoryStartupBytes = 1024MB
[System.Int64]$MemoryMinimum = 512MB
[System.Int64]$MemoryMaximum = 4096MB
################################################################################
# Checks
# Check if the VMs folder exists (Default folder for my VMs
If (Test-Path -Path $VmFolder) {
# Check if the new VM name already exist
if (-Not (Test-Path -Path $vmName)) {
Write-Verbose -Message ('VM folder {0} does not exist. Creating it.' -f $vmName)
# If the folder doesn't exist, then create it
New-Item -Path $VmFolder -Value $vmName -ItemType Directory -Force | Out-Null
}
} Else {
# The VMs folder does not exist, then create it and the new VM folder
Write-Verbose -Message 'Default VMs folder does not exist. Creating it.'
New-Item -Path 'C:\' -Value 'VMs' -ItemType Folder -Force | Out-Null
New-Item -Path $VmFolder -Value $vmName -ItemType Folder -Force | Out-Null
}
# Set the new diff path name
$vmVhdNewDisk = '{0}\{1}\{1}.vhdx' -f $VmFolder, $vmName
#Check if VmSwitch exists, otherwise create it
$VmSwitch = Get-VMSwitch -Name $VmSwitchName -ErrorAction SilentlyContinue
If (-not $VmSwitch) {
Write-Verbose -Message 'Default vSwitch "LAN" does not exist. Creating it.'
$Splat = @{
Name = $VmSwitchName
NetAdapterName = 'Ethernet'
AllowManagementOS = $true
EnableIov = $True
Notes = 'vSwitch used for VM to communicate, using physical NIC for communication.'
}
$VmSwitch = New-VMSwitch @Splat | Set-NetConnectionProfile -NetworkCategory Private
}
################################################################################
# Set the master disk based on choosen OS Type
# Define the Root Folder where master disks reside
$MastersRoot = 'C:\VMs\Masters'
switch ($vmOsType) {
# Option 1 -> Windows 10
'Win10' {
# Set the Master Disk path name
$vmVhdParentDisk = '{0}\_OK_Win10x64-Jan-2021.vhdx' -f $MastersRoot
} #----- End of Option 1 -----
# Option 2 -> Windows Server 2019 DesktopExperience
'W2k19' {
# Set the Master Disk path name
$vmVhdParentDisk = '{0}\_OK_W2019-GUI-Dec-2020.vhdx' -f $MastersRoot
#Define memory params
$MemoryStartupBytes = 2048MB
$MemoryMinimum = 512MB
$MemoryMaximum = 4096MB
#Define CPU counts
$ProcessorCount = 4
} #----- End of Option 2 -----
# Option 3 -> Windows Server 2019 CORE
'W2k19-CORE' {
# Set the Master Disk path name
$vmVhdParentDisk = '{0}\_OK_W2019-Core-Dec-2020.vhdx' -f $MastersRoot
#Define memory params
$MemoryStartupBytes = 2048MB
$MemoryMinimum = 512MB
$MemoryMaximum = 4096MB
#Define CPU counts
$ProcessorCount = 4
} #----- End of Option 3 -----
# Option 4 -> Windows Server 2022 DesktopExperience
'W2022' {
# Set the Master Disk path name
$vmVhdParentDisk = '{0}\_OK_W2022-GUI-Dec-2023.vhdx' -f $MastersRoot
#Define memory params
$MemoryStartupBytes = 4096MB
$MemoryMinimum = 4096MB
$MemoryMaximum = 8192MB
#Define CPU counts
$ProcessorCount = 8
} #----- End of Option 4 -----
# Option 5 -> Windows Server 2022 CORE
'W2022-CORE' {
# Set the Master Disk path name
$vmVhdParentDisk = '{0}\_OK_W2022-Core-Feb2024.vhdx' -f $MastersRoot
#Define memory params
$MemoryStartupBytes = 4096MB
$MemoryMinimum = 4096MB
$MemoryMaximum = 8192MB
#Define CPU counts
$ProcessorCount = 8
} #----- End of Option 5 -----
# Option 6 -> Windows 11
'Win11' {
# Set the Master Disk path name
$vmVhdParentDisk = '{0}\_OK_Win11-Dec2023.vhdx' -f $MastersRoot
} #----- End of Option 6 -----
} # --- End of switch ---
#Create the new Differential Disk, having the master
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Creating the new differential disk...'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
# Ensure disk does not exists.
$DiskExists = Get-VHD -Path $vmVhdNewDisk -ErrorAction SilentlyContinue
If ($DiskExists) {
Remove-Item $vmVhdNewDisk -Force | Out-Null
}
#Create the disk
$splat = @{
Path = $vmVhdNewDisk
ParentPath = $vmVhdParentDisk
Differencing = $true
}
New-VHD @splat | Out-Null
# Create the new VM
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Creating the ' $vmName ' virtual machine...'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
$splat = @{
Name = $vmName
Generation = 2
Path = $VmFolder
VHDPath = $vmVhdNewDisk
BootDevice = 'VHD'
SwitchName = $VmSwitch.Name
ComputerName = 'localhost'
#Version = 11
}
$VM = New-VM @splat
# Configure Integration Services
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Configuring Integration Services...'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
Get-VMIntegrationService -VMName $vmName | ForEach-Object { Enable-VMIntegrationService -Name $_.Name -VMName $vmName } | Out-Null
# Configure Memory for the VM
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Configuring Memory...'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
$splat = @{
VM = $VM
DynamicMemoryEnabled = $true
MinimumBytes = $MemoryMinimum
StartupBytes = $MemoryStartupBytes
MaximumBytes = $MemoryMaximum
Passthru = $true
}
Set-VMMemory @splat | Out-Null
# Configure CPU for the VM
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Configuring CPU...'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
$splat = @{
VMName = $vmName
Count = $ProcessorCount
Reserve = 10
Maximum = 75
Passthru = $true
}
Set-VMProcessor @splat | Out-Null
# Configure Memory for the VM
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Configuring other parameters...'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
$splat = @{
AutomaticCheckpointsEnabled = $false
AutomaticStartAction = 'StartIfRunning'
AutomaticStopAction = 'ShutDown'
ComputerName = 'localhost'
Name = $vmName
Note = '{0} - {1}' -f $vmName, $vmOsType
Passthru = $true
}
Set-VM @splat | Out-Null
# Enable SecureBoot
Set-VMFirmware -VMName $vmName -SecureBootTemplate 'MicrosoftWindows' -EnableSecureBoot On
# Add virtual TPM
$HGOwner = Get-HgsGuardian UntrustedGuardian
$KeyProtector = New-HgsKeyProtector -Owner $HGOwner -AllowUntrustedRoot
Set-VMKeyProtector -VMName $vmName -KeyProtector $KeyProtector.RawData
Enable-VMTPM -VM $vmName
#Mount the newly created VHDX file
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Mount and patch differential VHDX (inyect unattended.xml file)'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
# This is not working on Windwos11 October 2011 version
#[System.String]$mount = (Mount-DiskImage -ImagePath $vmVhdNewDisk -StorageType VHDX -Access ReadWrite -PassThru | Get-Disk | Get-Partition | Get-Volume).DriveLetter
# Define new folder for temporary mount VHDx image
$TempMount = New-Item -Path $VmFolder -Name TempMount -ItemType Directory
# Mount VHDx image without drive letter and get the disk
$MountedVhd = Mount-DiskImage -ImagePath $vmVhdNewDisk -NoDriveLetter -PassThru | Get-Disk
# Select ONLY Basic partitions
$Partitions = $MountedVhd | Get-Partition | Where-Object { $_.Type -eq 'Basic' }
# Provide access to the mounted path (C:\VMs\TmpMount\)
$Partitions | Add-PartitionAccessPath -AccessPath $TempMount
# Generate static MAC address
<#
the number of dynamic MAC addresses that a Hyper-V host can produce is 256. Suppose we have the MAC address aa-bb-cc-dd-ee-ff.
The first 3 octets (aa-bb-cc) refer to a Microsoft’s Unique Identifier that is used in all Hyper-V hosts (00: 15: 5D).
The next 2 octets (dd-ee) are generated by the last two octets of the IP address that was first set up on the Hyper-V server.
The last octet (ff) is generated from the range 0x0 – 0xFF.
#>
# Microsoft’s Unique Identifier that is used in all Hyper-V hosts (00: 15: 5D)
$TmpMAC = '00-15-5D-'
# next 3 octets
$TmpMAC += [BitConverter]::ToString([BitConverter]::GetBytes((Get-Random -Maximum 0xFFFFFFFFFFFF)), 0, 3)
Get-VM $vmName | Set-VMNetworkAdapter -StaticMacAddress ($TmpMAC.Replace('-', ''))
# Get VM MAC address
#$VmMacAddress = (Get-VM $vmName | Get-VMNetworkAdapter).MacAddress -replace '..(?!$)', '$&:'
# Get and prepare IP configuration
If (Test-Path -Path $DataFile) {
try {
# Read the configuration file (PSD1)
$ht = Import-PowerShellDataFile $DataFile
Write-Verbose -Message ('Data File {0} loaded succesfully' -f $DataFile)
} catch {
throw
} #end Try-Catch
# Check if newly created VM exist on the configuration
if ($ht.AllNodes.ForEach({ $_.NodeName }).contains($VmName)) {
# Define Hashtable to hold the values from configuration file (PSD1)
$PC = @{
NodeName = [System.String]
DefaultGatewayIpV4 = [System.String]
DefaultGatewayIpV6 = [System.String]
DNS1IpV4 = [System.String]
DNS2IpV4 = [System.String]
DNS3IpV4 = [System.String]
DNS4IpV4 = [System.String]
DNS5IpV4 = [System.String]
DNS1IpV6 = [System.String]
DNS2IpV6 = [System.String]
DNS3IpV6 = [System.String]
DNS4IpV6 = [System.String]
DNS5IpV6 = [System.String]
TimeZone = [System.String]
IPv4 = [System.String]
MaskV4 = [System.String]
IPv6 = [System.String]
MaskV6 = [System.String]
NetBIOSDomainName = [System.String]
DnsDomainName = [System.String]
Description = [System.String]
Disks = [System.String]
}
# Iterate all nodes
Foreach ($item in $ht.AllNodes) {
# Get wildcard data (all nodes)
If ($item.NodeName -eq '*') {
$PC.DefaultGatewayIpV4 = $item.DefaultGatewayIpV4
$PC.DefaultGatewayIpV6 = $item.DefaultGatewayIpV6
$PC.DNS1IpV4 = $item.DNS1IpV4
$PC.DNS2IpV4 = $item.DNS2IpV4
$PC.DNS3IpV4 = $item.DNS3IpV4
$PC.DNS4IpV4 = $item.DNS4IpV4
$PC.DNS5IpV4 = $item.DNS5IpV4
$PC.DNS1IpV6 = $item.DNS1IpV6
$PC.DNS2IpV6 = $item.DNS2IpV6
$PC.DNS3IpV6 = $item.DNS3IpV6
$PC.DNS4IpV6 = $item.DNS4IpV6
$PC.DNS5IpV6 = $item.DNS5IpV6
$PC.TimeZone = $item.TimeZone
} # End If
# Get specific host data
If ($item.NodeName -eq $VmName) {
$PC.IPv4 = $item.IPv4
$PC.IPv6 = $item.IPv6
$PC.NetBIOSDomainName = $item.NetBIOSDomainName
$PC.DnsDomainName = $item.DnsDomainName
$PC.Description = $item.Description
Try {
$PC.Disks = $item.Disks
} Catch {
$PC.Disks = 'SingleDisk'
}
} # End If
} # End Foreach
} # End If
} # Get and prepare IP configuration
Write-Verbose -Message ('Ipv4 address: {0}' -f $PC.IPv4)
Write-Verbose -Message ('Ipv4 Default gateway: {0}' -f $PC.DefaultGatewayIpV4)
Write-Verbose -Message ('Ipv4 Primary DNS: {0}' -f $PC.DNS1IpV4 )
Write-Verbose -Message ('Ipv4 Secondary DNS: {0}' -f $PC.DNS2IpV4)
Write-Verbose -Message ('Ipv4 additional DNS: {0}, {1} and {2}' -f $PC.DNS3IpV4, $PC.DNS4IpV4, $PC.DNS5IpV4)
Write-Verbose -Message ('Ipv6 address: {0}' -f $PC.IPv6)
Write-Verbose -Message ('Ipv6 Default gateway: {0}' -f $PC.DefaultGatewayIpV6)
Write-Verbose -Message ('Ipv6 Primary DNS: {0}' -f $PC.DNS1IpV6 )
Write-Verbose -Message ('Ipv6 Secondary DNS: {0}' -f $PC.DNS2IpV6)
Write-Verbose -Message ('Ipv6 additional DNS: {0}, {1} and {2}' -f $PC.DNS3IpV6, $PC.DNS4IpV6, $PC.DNS5IpV6)
# IP configuration section of the Unattend file
if ($PC.ipv4 -or $PC.ipv6) {
$UnattendIpConfig = @"
<component name="Microsoft-Windows-TCPIP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Interfaces>
<Interface wcm:action="add">
<Identifier>$TmpMAC</Identifier>
<Ipv4Settings>
<DhcpEnabled>false</DhcpEnabled>
<Metric>10</Metric>
<RouterDiscoveryEnabled>false</RouterDiscoveryEnabled>
</Ipv4Settings>
<Ipv6Settings>
<DhcpEnabled>false</DhcpEnabled>
<Metric>10</Metric>
<RouterDiscoveryEnabled>false</RouterDiscoveryEnabled>
</Ipv6Settings>
<UnicastIpAddresses>
<IpAddress wcm:action="add" wcm:keyValue="1">$($PC.IPv4)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="2">$($PC.IPv6)</IpAddress>
</UnicastIpAddresses>
<Routes>
<Route wcm:action="add">
<Identifier>0</Identifier>
<Metric>10</Metric>
<NextHopAddress>$($PC.DefaultGatewayIpV4)</NextHopAddress>
<Prefix>0.0.0.0/0</Prefix>
</Route>
<Route wcm:action="add">
<Identifier>1</Identifier>
<Metric>10</Metric>
<NextHopAddress>$($PC.DefaultGatewayIpV6)</NextHopAddress>
<Prefix>::0/0</Prefix>
</Route>
</Routes>
</Interface>
</Interfaces>
</component>
<component name="Microsoft-Windows-DNS-Client" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Interfaces>
<Interface wcm:action="add">
<Identifier>$TmpMAC</Identifier>
<DNSServerSearchOrder>
<IpAddress wcm:action="add" wcm:keyValue="1">$($PC.DNS1IpV4)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="2">$($PC.DNS2IpV4)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="3">$($PC.DNS3IpV4)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="4">$($PC.DNS4IpV4)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="5">$($PC.DNS5IpV4)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="6">$($PC.DNS1IpV6)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="7">$($PC.DNS2IpV6)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="8">$($PC.DNS3IpV6)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="9">$($PC.DNS4IpV6)</IpAddress>
<IpAddress wcm:action="add" wcm:keyValue="10">$($PC.DNS5IpV6)</IpAddress>
</DNSServerSearchOrder>
<DNSDomain>$($PC.DnsDomainName)</DNSDomain>
<DisableDynamicUpdate>false</DisableDynamicUpdate>
<EnableAdapterDomainNameRegistration>true</EnableAdapterDomainNameRegistration>
</Interface>
</Interfaces>
<DNSSuffixSearchOrder>
<DomainName wcm:action="add" wcm:keyValue="1">$($PC.DnsDomainName)</DomainName>
</DNSSuffixSearchOrder>
</component>
"@
} else {
$UnattendIpConfig = $null
} #end If-Else
# Get Domain DN
[string]$AdDn = $PC.DnsDomainName
$AdDn = 'DC={0},DC={1}' -f $AdDn.split('.')[0], $AdDn.split('.')[1]
# Tier0 OUs
switch -wildcard ($VmName) {
# PAWs
'Paw0*' {
$DestOU = ('OU=PawT0,OU=PAW,OU=Admin,{0}' -f $AdDn)
}
'Paw1*' {
$DestOU = ('OU=PawT1,OU=PAW,OU=Admin,{0}' -f $AdDn)
}
'Paw2*' {
$DestOU = ('OU=PawT2,OU=PAW,OU=Admin,{0}' -f $AdDn)
}
# Tier0
{
($_ -eq 'Adfs*') -or
($_ -eq 'Ca*') -or
($_ -eq 'Dsc*') -or
('Linux1', 'Linux2' -contains $_) -or
($_ -eq 'Mdt*') -or
($_ -eq 'Sccm*') -or
($_ -eq 'Scom*') -or
('SQL1', 'SQL2' -contains $_) -or
('Srv1', 'Srv2', 'Srv3', 'Srv4' -contains $_) -or
($_ -eq 'Vmm*') -or
($_ -eq 'Wac*') -or
($_ -eq 'Wsus*')
} {
$DestOU = ('OU=InfraT0,OU=Infra,OU=Admin,{0}' -f $AdDn)
}
#Tier2
{ 'PC1', 'PC4', 'PC7', 'PC10' -Contains $_ } {
$DestOU = ('OU=Desktops,OU=BAAD,OU=Sites,{0}' -f $AdDn)
}
{ 'PC2', 'PC5', 'PC8', 'PC11' -Contains $_ } {
$DestOU = ('OU=Desktops,OU=GOOD,OU=Sites,{0}' -f $AdDn)
}
{ 'PC3', 'PC6', 'PC9', 'PC12' -Contains $_ } {
$DestOU = ('OU=Desktops,OU=UGLY,OU=Sites,{0}' -f $AdDn)
}
{ 'Lap1', 'Lap4', 'Lap7' -Contains $_ } {
$DestOU = ('OU=Laptops,OU=BAAD,OU=Sites,{0}' -f $AdDn)
}
{ 'Lap2', 'Lap5', 'Lap8' -Contains $_ } {
$DestOU = ('OU=Laptops,OU=GOOD,OU=Sites,{0}' -f $AdDn)
}
{ 'Lap3', 'Lap6', 'Lap9' -Contains $_ } {
$DestOU = ('OU=Laptops,OU=UGLY,OU=Sites,{0}' -f $AdDn)
}
Default {
$DestOU = ('OU=Quarantine-PC,{0}' -f $AdDn)
}
} #end Switch
# Domain Controllers
If ($VmName -like 'DC') {
$DestOU = ('OU=InfraStaging,OU=Infra,OU=Admin,{0}' -f $AdDn)
} #end If
# Tier1 OU
If ('Srv5', 'Srv6', 'Srv7', 'Srv8', 'Srv9', 'Srv10', 'Srv11', 'Srv12', 'Srv13', 'Srv14', 'Srv15', 'Srv16', 'Srv17', 'Srv18', 'Srv19', 'Srv20' -Contains $vmName) {
$DestOU = ('OU=Servers,{0}' -f $AdDn)
} #end If
If ('SQL3', 'Srv4' -Contains $vmName) {
$DestOU = ('OU=Sql,OU=Servers,{0}' -f $AdDn)
} #end If
If ('Linux3', 'Linux4', 'Linux5', 'Linux6' -Contains $vmName) {
$DestOU = ('OU=Linux,OU=Servers,{0}' -f $AdDn)
} #end If
# Domain Join section of the Unattend file
If ($VmName -ne ('DC1' -or 'DC5' -or 'DC9')) {
Write-Verbose -Message ('Destination OU for {0} will be "{1}"' -f $VmName, $DestOU)
$UnattendDomainJoin = @"
<component name="Microsoft-Windows-UnattendedJoin" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Identification>
<Credentials>
<Domain>$($PC.DnsDomainName)</Domain>
<Password>P@ssword 123456</Password>
<Username>TheUgly</Username>
</Credentials>
<JoinDomain>$($PC.DnsDomainName)</JoinDomain>
<MachineObjectOU>$DestOU</MachineObjectOU>
</Identification>
</component>
"@
} else {
Write-Verbose -Message ('VM {0} will not be joined to any domain' -f $VmName)
$UnattendDomainJoin = $null
} #end If-Else
# Generate Unattend file
$unattend = @"
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<servicing></servicing>
<settings pass="generalize">
<component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SkipRearm>1</SkipRearm>
</component>
<component name="Microsoft-Windows-PnpSysprep" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PersistAllDeviceInstalls>true</PersistAllDeviceInstalls>
</component>
</settings>
<settings pass="specialize">
<component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InputLocale>0c0a:0000040a</InputLocale>
<SystemLocale>es-ES</SystemLocale>
<UILanguage>en-US</UILanguage>
<UILanguageFallback>es-ES</UILanguageFallback>
<UserLocale>es-ES</UserLocale>
</component>
<component name="Microsoft-Windows-Security-SPP-UX" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SkipAutoActivation>true</SkipAutoActivation>
</component>
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="wow64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ComputerName>$VmName</ComputerName>
<RegisteredOrganization>Eguibar Information Technology S.L.</RegisteredOrganization>
<RegisteredOwner>Vicente Rodriguez Eguibar</RegisteredOwner>
<TimeZone>Romance Standard Time</TimeZone>
</component>
<component name="Microsoft-Windows-SQMApi" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CEIPEnabled>0</CEIPEnabled>
</component>
$UnattendIpConfig
$UnattendDomainJoin
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstLogonCommands>
<SynchronousCommand wcm:action="add">
<CommandLine>powershell -NoLogo -sta -NoProfile -NoInteractive -WindowStyle Hidden -Command {Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force}</CommandLine>
<Description>Set Execution Policy to RemoteSigned</Description>
<Order>1</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>2</Order>
<CommandLine>powershell -NoLogo -sta -NoProfile -NoInteractive -WindowStyle Hidden -Command {Enable-PSRemoting -SkipNetworkProfileCheck -Force}</CommandLine>
<Description>Enable PsRemoting</Description>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>3</Order>
<CommandLine>powershell -NoLogo -sta -NoProfile -NoInteractive -WindowStyle Hidden -Command {Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name Shell -Value 'pwsh.exe -NoExit'}</CommandLine>
<Description>Make PowerShell the default shell</Description>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>4</Order>
<CommandLine>powercfg /h /type Off</CommandLine>
<Description>Reduce hiberfile size</Description>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
</FirstLogonCommands>
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideLocalAccountScreen>false</HideLocalAccountScreen>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<ProtectYourPC>1</ProtectYourPC>
</OOBE>
<UserAccounts>
<AdministratorPassword>
<Value>UABAAHMAcwB3AG8AcgBkACAAMQAyADMANAA1ADYAQQBkAG0AaQBuAGkAcwB0AHIAYQB0AG8AcgBQAGEAcwBzAHcAbwByAGQA</Value>
<PlainText>false</PlainText>
</AdministratorPassword>
</UserAccounts>
<Display>
<HorizontalResolution>1920</HorizontalResolution>
<VerticalResolution>1080</VerticalResolution>
</Display>
<AutoLogon>
<Password>
<Value>UABAAHMAcwB3AG8AcgBkACAAMQAyADMANAA1ADYAUABhAHMAcwB3AG8AcgBkAA==</Value>
<PlainText>false</PlainText>
</Password>
<Enabled>true</Enabled>
<LogonCount>5</LogonCount>
<Username>Administrator</Username>
</AutoLogon>
<RegisteredOrganization>Eguibar Information Technology S.L.</RegisteredOrganization>
<RegisteredOwner>Vicente Rodriguez Eguibar</RegisteredOwner>
<TimeZone>Romance Standard Time</TimeZone>
</component>
<component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InputLocale>0c0a:0000040a</InputLocale>
<SystemLocale>es-ES</SystemLocale>
<UILanguage>en-US</UILanguage>
<UILanguageFallback>es-ES</UILanguageFallback>
<UserLocale>es-ES</UserLocale>
</component>
<component name="Security-Malware-Windows-Defender" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DisableAntiSpyware>false</DisableAntiSpyware>
<EnableRemoteManagedDefaults>true</EnableRemoteManagedDefaults>
</component>
<component name="Microsoft-Windows-International-Core" processorArchitecture="wow64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InputLocale>0c0a:0000040a</InputLocale>
<SystemLocale>es-ES</SystemLocale>
<UILanguage>es-ES</UILanguage>
<UserLocale>es-ES</UserLocale>
<UILanguageFallback>en-US</UILanguageFallback>
</component>
</settings>
<settings pass="offlineServicing">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="wow64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OfflineUserAccounts>
<OfflineAdministratorPassword>
<Value>UABAAHMAcwB3AG8AcgBkACAAMQAyADMANAA1ADYATwBmAGYAbABpAG4AZQBBAGQAbQBpAG4AaQBzAHQAcgBhAHQAbwByAFAAYQBzAHMAdwBvAHIAZAA=</Value>
<PlainText>false</PlainText>
</OfflineAdministratorPassword>
</OfflineUserAccounts>
<ComputerName>$VmName</ComputerName>
<RegisteredOrganization>Eguibar Information Technology S.L.</RegisteredOrganization>
<RegisteredOwner>Vicente Rodriguez Eguibar</RegisteredOwner>
</component>
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OfflineUserAccounts>
<OfflineAdministratorPassword>
<Value>UABAAHMAcwB3AG8AcgBkACAAMQAyADMANAA1ADYATwBmAGYAbABpAG4AZQBBAGQAbQBpAG4AaQBzAHQAcgBhAHQAbwByAFAAYQBzAHMAdwBvAHIAZAA=</Value>
<PlainText>false</PlainText>
</OfflineAdministratorPassword>
</OfflineUserAccounts>
<ComputerName>$VmName</ComputerName>
<RegisteredOrganization>Eguibar Information Technology S.L.</RegisteredOrganization>
<RegisteredOwner>Vicente Rodriguez Eguibar</RegisteredOwner>
</component>
<component name="Microsoft-Windows-LUA-Settings" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EnableLUA>false</EnableLUA>
</component>
</settings>
</unattend>
"@
# Copy the above unattend to VHDX C:\Windows\Panther\unattend.xml (Alternatively to C:\Windows\System32\Sysprep\Unattend.xml)
# Set-Content -Value $unattend -Path ('{0}:\Windows\Panther\unattend.xml' -f $mount.Trim())
Write-Verbose -Message 'Creating Unattend.xml file on new VM.'
Set-Content -Value $unattend -Path ('{0}\Windows\Panther\unattend.xml' -f $TempMount)
Write-Verbose -Message 'Copy of Unattend.xml file created on C:\VMs\Unattend-01-01-1600.xml'
Set-Content -Value $unattend -Path ('C:\VMs\Unattend_{0}.xml' -f (Get-Date -Format 'dd-MMM-yyyy'))
# Make windows to use the unattend.xml file
#Use-WindowsUnattend -Path ('{0}:\' -f $mount.Trim()) -UnattendPath ('{0}:\Windows\Panther\unattend.xml' -f $mount.Trim()) -LogLevel WarningsInfo
Write-Verbose -Message 'Sealing image after apply Unattend.xml'
Use-WindowsUnattend -Path $TempMount -UnattendPath ('{0}\Windows\Panther\unattend.xml' -f $TempMount) -LogLevel WarningsInfo
#Dismount Image
Dismount-DiskImage -ImagePath $vmVhdNewDisk -StorageType VHDX
# Remove the mountPoint
Remove-Item $TempMount -Force
# Configure additional disks on DC
If ($PC.Disks -eq 'Multiple-Disks') {
Write-Verbose -Message 'Creating additional disk for DomainController'
$DcDisks = @(
'N-NTDS',
'L-NTDSlogs',
'S-SYSVOL',
'P-Pagefile',
'T-Temp',
'E-EventLogs'
)
Foreach ($Disk in $DcDisks) {
# Get the current disk path
$CurrentDisk = '{0}\{1}\{2}.vhdx' -f $VmFolder, $vmName, $Disk
#Check if current disk exists
$DiskExists = Get-VHD -Path $CurrentDisk -ErrorAction SilentlyContinue
If ($DiskExists) {
Remove-Item $CurrentDisk -Force | Out-Null
}
#Create the disk
$splat = @{
Path = $CurrentDisk
SizeBytes = 32GB
Dynamic = $true
}
New-VHD @splat | Out-Null
# Attach the disk
Add-VMHardDiskDrive -VMName $vmName -Path $CurrentDisk -ControllerType SCSI
}
}
# Starting VM
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host ' Starting VM'
Write-Host ' ---------------------------------------------------------------' -ForegroundColor green
Write-Host
Start-VM -VM $VM
Wait-VM -VM $vm -For Heartbeat
Wait-VM -VM $vm -For Heartbeat
Write-Host '#########################################################################' -ForegroundColor green
Write-Host '# #' -ForegroundColor green
Write-Host '# Virtual Machine created. Now you can start playing with it. #' -ForegroundColor green
Write-Host '# #' -ForegroundColor green
Write-Host '#########################################################################' -ForegroundColor green
# $VmSwitchName = New-VMSwitch -Name 'LAN' -SwitchType External -Notes 'vSwitch (LAN) used for VM to communicate, having physical host ICS enabled for this switch'
# GET-VM | GET-VMNetworkAdapter | Connect-VMNetworkAdapter -Switchname 'vSwitch'
# Set-NetConnectionProfile -InterfaceAlias 'LAN' -NetworkCategory Private