-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigDemo_Insider_Routing_WSUS_V1.ps1
1180 lines (917 loc) · 40.6 KB
/
BigDemo_Insider_Routing_WSUS_V1.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
<#
Created: 2018-02-01
Version: 1.0
Author Dave Kawula MVP and Thomas Rayner MVP
Homepage: http://www.checkyourlogs.net
Disclaimer:
This script is provided "AS IS" with no warranties, confers no rights and
is not supported by the authors or CheckyourLogs or MVPDays Publishing
Author - Dave Kawula
Twitter: @DaveKawula
Blog : http://www.checkyourlogs.net
Author - Thomas Rayner
Twitter: @MrThomasRayner
Blog : http://workingsysadmin.com
.Synopsis
Creates a big demo lab.
.DESCRIPTION
Huge Thank you to Ben Armstrong @VirtualPCGuy for giving me the source starter code for this :)
This script will build a sample lab configruation on a single Hyper-V Server:
It includes in this version 2 Domain Controllers, 1 x DHCP Server, 1 x MGMT Server, 16 x S2D Nodes
It is fully customizable as it has been created with base functions.
The Parameters at the beginning of the script will setup the domain name, organization name etc.
You will need to change the <ProductKey> Variable as it has been removed for the purposes of the print in this book.
.EXAMPLE
TODO: Dave, add something more meaningful in here
.PARAMETER WorkingDir
Transactional directory for files to be staged and written
.PARAMETER Organization
Org that the VMs will belong to
.PARAMETER Owner
Name to fill in for the OSs Owner field
.PARAMETER TimeZone
Timezone used by the VMs
.PARAMETER AdminPassword
Administrative password for the VMs
.PARAMETER DomainName
AD Domain to setup/join VMs to
.PARAMETER DomainAdminPassword
Domain recovery/admin password
.PARAMETER VirtualSwitchName
Name of the vSwitch for Hyper-V
.PARAMETER Subnet
The /24 Subnet to use for Hyper-V networking
#>
#region Parameters
[cmdletbinding()]
param
(
[Parameter(Mandatory)]
[ValidateScript({ $_ -match '[^\\]$' })] #ensure WorkingDir does not end in a backslash, otherwise issues are going to come up below
[string]
$WorkingDir = 'c:\ClusterStoreage\Volume1\DCBuild',
[Parameter(Mandatory)]
[string]
$Organization = 'MVP Rockstars',
[Parameter(Mandatory)]
[string]
$Owner = 'Dave Kawula',
[Parameter(Mandatory)]
[ValidateScript({ $_ -in ([System.TimeZoneInfo]::GetSystemTimeZones()).ID })] #ensure a valid TimeZone was passed
[string]
$Timezone = 'Pacific Standard Time',
[Parameter(Mandatory)]
[string]
$adminPassword = 'P@ssw0rd',
[Parameter(Mandatory)]
[string]
$domainName = 'MVPDays.Com',
[Parameter(Mandatory)]
[string]
$domainAdminPassword = 'P@ssw0rd',
[Parameter(Mandatory)]
[string]
$virtualSwitchName = 'Dave MVP Demo',
[Parameter(Mandatory)]
[ValidatePattern('(\d{1,3}\.){3}')] #ensure that Subnet is formatted like the first three octets of an IPv4 address
[string]
$Subnet = '172.16.200.',
[Parameter(Mandatory)]
[string]
$virtualNATSwitchName = 'Dave MVP Demo',
[Parameter(Mandatory)]
[string]
$ExtraLabfilesSource = 'C:\ClusterStorage\Volume1\DCBuild\Extralabfiles'
)
#endregion
#region Functions
function Wait-PSDirect
{
param
(
[string]
$VMName,
[Object]
$cred
)
Write-Log $VMName "Waiting for PowerShell Direct (using $($cred.username))"
while ((Invoke-Command -VMName $VMName -Credential $cred {
'Test'
} -ea SilentlyContinue) -ne 'Test')
{
Start-Sleep -Seconds 1
}
}
Function Wait-Sleep {
param (
[int]$sleepSeconds = 60,
[string]$title = "... Waiting for $sleepSeconds Seconds... Be Patient",
[string]$titleColor = "Yellow"
)
Write-Host -ForegroundColor $titleColor $title
for ($sleep = 1; $sleep -le $sleepSeconds; $sleep++ ) {
Write-Progress -ParentId -1 -Id 42 -Activity "Sleeping for $sleepSeconds seconds" -Status "Slept for $sleep Seconds:" -percentcomplete (($sleep / $sleepSeconds) * 100)
Start-Sleep 1
}
Write-Progress -Completed -Id 42 -Activity "Done Sleeping"
}
function Restart-DemoVM
{
param
(
[string]
$VMName
)
Write-Log $VMName 'Rebooting'
stop-vm $VMName
start-vm $VMName
}
function Confirm-Path
{
param
(
[string] $path
)
if (!(Test-Path $path))
{
$null = mkdir $path
}
}
function Write-Log
{
param
(
[string]$systemName,
[string]$message
)
Write-Host -Object (Get-Date).ToShortTimeString() -ForegroundColor Cyan -NoNewline
Write-Host -Object ' - [' -ForegroundColor White -NoNewline
Write-Host -Object $systemName -ForegroundColor Yellow -NoNewline
Write-Host -Object "]::$($message)" -ForegroundColor White
}
function Clear-File
{
param
(
[string] $file
)
if (Test-Path $file)
{
$null = Remove-Item $file -Recurse
}
}
function Get-UnattendChunk
{
param
(
[string] $pass,
[string] $component,
[xml] $unattend
)
return $unattend.unattend.settings |
Where-Object -Property pass -EQ -Value $pass `
|
Select-Object -ExpandProperty component `
|
Where-Object -Property name -EQ -Value $component
}
function New-UnattendFile
{
param
(
[string] $filePath
)
# Reload template - clone is necessary as PowerShell thinks this is a "complex" object
$unattend = $unattendSource.Clone()
# Customize unattend XML
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.RegisteredOrganization = 'Azure Sea Class Covert Trial' #TR-Egg
}
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.RegisteredOwner = 'Thomas Rayner - @MrThomasRayner - workingsysadmin.com' #TR-Egg
}
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.TimeZone = $Timezone
}
Get-UnattendChunk 'oobeSystem' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.UserAccounts.AdministratorPassword.Value = $adminPassword
}
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.ProductKey = $WindowsKey
}
Clear-File $filePath
$unattend.Save($filePath)
}
function New-UnattendFile1
{
param
(
[string] $filePath
)
# Reload template - clone is necessary as PowerShell thinks this is a "complex" object
$unattend = $unattendSource.Clone()
# Customize unattend XML
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.RegisteredOrganization = 'Azure Sea Class Covert Trial' #TR-Egg
}
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.RegisteredOwner = 'Thomas Rayner - @MrThomasRayner - workingsysadmin.com' #TR-Egg
}
Get-UnattendChunk 'specialize' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.TimeZone = $Timezone
}
Get-UnattendChunk 'oobeSystem' 'Microsoft-Windows-Shell-Setup' $unattend | ForEach-Object -Process {
$_.UserAccounts.AdministratorPassword.Value = $adminPassword
}
Clear-File $filePath
$unattend.Save($filePath)
}
Function Initialize-BaseImage
{
Mount-DiskImage $ServerISO
$DVDDriveLetter = (Get-DiskImage $ServerISO | Get-Volume).DriveLetter
Copy-Item -Path "$($DVDDriveLetter):\NanoServer\NanoServerImageGenerator\Convert-WindowsImage.ps1" -Destination "$($WorkingDir)\Convert-WindowsImage.ps1" -Force
Import-Module -Name "$($DVDDriveLetter):\NanoServer\NanoServerImagegenerator\NanoServerImageGenerator.psm1" -Force
if (!(Test-Path "$($BaseVHDPath)\NanoBase.vhdx"))
{
New-NanoServerImage -MediaPath "$($DVDDriveLetter):\" -BasePath $BaseVHDPath -TargetPath "$($BaseVHDPath)\NanoBase.vhdx" -Edition Standard -DeploymentType Guest -Compute -Clustering -AdministratorPassword (ConvertTo-SecureString $adminPassword -AsPlainText -Force)
# New-NanoServerImage -MediaPath "$($DVDDriveLetter):\" -BasePath $BaseVDHPath -TargetPath "$($BaseVHDPath)\NanoBase.vhdx" -GuestDrivers -DeploymentType Guest -Edition Standard -Compute -Clustering -Defender -Storage -AdministratorPassword (ConvertTo-SecureString $adminPassword -AsPlainText -Force)
}
#Copy-Item -Path '$WorkingDir\Convert-WindowsImage.ps1' -Destination "$($WorkingDir)\Convert-WindowsImage.ps1" -Force
New-UnattendFile "$WorkingDir\unattend.xml"
New-UnattendFile1 "$WorkingDir\unattend1.xml"
#Build the Windows 2016 Core Base VHDx for the Lab
if (!(Test-Path "$($BaseVHDPath)\VMServerBaseCore.vhdx"))
{
Set-Location $workingdir
#Watch the Editions --> 17079 is SERVERDATACENTERACORE and 2016 is SERVERDATACENTERCORE
# Load (aka "dot-source) the Function
. .\Convert-WindowsImage.ps1
# Prepare all the variables in advance (optional)
$ConvertWindowsImageParam = @{
SourcePath = $ServerISO1
RemoteDesktopEnable = $True
Passthru = $True
Edition = "SERVERDATACENTERACORE"
VHDFormat = "VHDX"
SizeBytes = 60GB
WorkingDirectory = $workingdir
VHDPath = "$($BaseVHDPath)\VMServerBaseCore.vhdx"
DiskLayout = 'UEFI'
UnattendPath = "$($workingdir)\unattend1.xml"
}
$VHDx = Convert-WindowsImage @ConvertWindowsImageParam
}
#Build the Windows 2016 Full UI Base VHDx for the Lab
if (!(Test-Path "$($BaseVHDPath)\VMServerBase.vhdx"))
{
Set-Location $workingdir
# Load (aka "dot-source) the Function
. .\Convert-WindowsImage.ps1
# Prepare all the variables in advance (optional)
$ConvertWindowsImageParam = @{
SourcePath = $ServerISO
RemoteDesktopEnable = $True
Passthru = $True
Edition = "ServerDataCenter"
VHDFormat = "VHDX"
SizeBytes = 60GB
WorkingDirectory = $workingdir
VHDPath = "$($BaseVHDPath)\VMServerBase.vhdx"
DiskLayout = 'UEFI'
UnattendPath = "$($workingdir)\unattend.xml"
Package = @(
"$($BaseVHDPath)\windows10.0-kb3213986-x64_a1f5adacc28b56d7728c92e318d6596d9072aec4.msu"
)
}
$VHDx = Convert-WindowsImage @ConvertWindowsImageParam
}
Clear-File "$($BaseVHDPath)\unattend.xml"
Clear-File "$($BaseVHDPath)\unattend1.xml"
Dismount-DiskImage $ServerISO
Dismount-DiskImage $ServerISO1
#Clear-File "$($WorkingDir)\Convert-WindowsImage.ps1"
}
function Download-BaseImageUpdates
{
if (!(Test-Path "$($BaseVHDPath)\windows10.0-kb3213986-x64_a1f5adacc28b56d7728c92e318d6596d9072aec4.msu"))
{
Invoke-WebRequest -Uri http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/12/windows10.0-kb3213986-x64_a1f5adacc28b56d7728c92e318d6596d9072aec4.msu -OutFile "$($BaseVHDPath)\windows10.0-kb3213986-x64_a1f5adacc28b56d7728c92e318d6596d9072aec4.msu" -Verbose
}
}
function Invoke-DemoVMPrep
{
param
(
[string] $VMName,
[string] $GuestOSName,
[switch] $FullServer
)
Write-Log $VMName 'Removing old VM'
get-vm $VMName -ErrorAction SilentlyContinue |
stop-vm -TurnOff -Force -Passthru |
remove-vm -Force
Clear-File "$($VMPath)\$($GuestOSName).vhdx"
Write-Log $VMName 'Creating new differencing disk'
if ($FullServer)
{
$null = New-VHD -Path "$($VMPath)\$($GuestOSName).vhdx" -ParentPath "$($BaseVHDPath)\VMServerBase.vhdx" -Differencing
}
else
{
$null = New-VHD -Path "$($VMPath)\$($GuestOSName).vhdx" -ParentPath "$($BaseVHDPath)\VMServerBaseCore.vhdx" -Differencing
}
Write-Log $VMName 'Creating virtual machine'
new-vm -Name $VMName -MemoryStartupBytes 4GB -SwitchName $virtualSwitchName `
-Generation 2 -Path "$($VMPath)\" | Set-VM -ProcessorCount 2
Set-VMFirmware -VMName $VMName -SecureBootTemplate MicrosoftUEFICertificateAuthority
Set-VMFirmware -Vmname $VMName -EnableSecureBoot off
Add-VMHardDiskDrive -VMName $VMName -Path "$($VMPath)\$($GuestOSName).vhdx" -ControllerType SCSI
Write-Log $VMName 'Starting virtual machine'
Enable-VMIntegrationService -Name 'Guest Service Interface' -VMName $VMName
start-vm $VMName
}
function Create-DemoVM
{
param
(
[string] $VMName,
[string] $GuestOSName,
[string] $IPNumber = '0'
)
Wait-PSDirect $VMName -cred $localCred
Invoke-Command -VMName $VMName -Credential $localCred {
param($IPNumber, $GuestOSName, $VMName, $domainName, $Subnet)
if ($IPNumber -ne '0')
{
Write-Output -InputObject "[$($VMName)]:: Setting IP Address to $($Subnet)$($IPNumber)"
$null = New-NetIPAddress -IPAddress "$($Subnet)$($IPNumber)" -InterfaceAlias 'Ethernet' -PrefixLength 24
Write-Output -InputObject "[$($VMName)]:: Setting DNS Address"
Get-DnsClientServerAddress | ForEach-Object -Process {
Set-DnsClientServerAddress -InterfaceIndex $_.InterfaceIndex -ServerAddresses "$($Subnet)1"
}
}
Write-Output -InputObject "[$($VMName)]:: Renaming OS to `"$($GuestOSName)`""
Rename-Computer -NewName $GuestOSName
Write-Output -InputObject "[$($VMName)]:: Configuring WSMAN Trusted hosts"
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value "*.$($domainName)" -Force
Set-Item WSMan:\localhost\client\trustedhosts "$($Subnet)*" -Force -concatenate
Enable-WSManCredSSP -Role Client -DelegateComputer "*.$($domainName)" -Force
} -ArgumentList $IPNumber, $GuestOSName, $VMName, $domainName, $Subnet
Restart-DemoVM $VMName
Wait-PSDirect $VMName -cred $localCred
}
function Invoke-NodeStorageBuild
{
param
(
[string]$VMName,
[string]$GuestOSName
)
Create-DemoVM $VMName $GuestOSName
Clear-File "$($VMPath)\$($GuestOSName) - Data 1.vhdx"
Clear-File "$($VMPath)\$($GuestOSName) - Data 2.vhdx"
Get-VM $VMName | Stop-VM
Add-VMNetworkAdapter -VMName $VMName -SwitchName $virtualSwitchName
New-VHD -Path "$($VMPath)\$($GuestOSName) - Data 1.vhdx" -Dynamic -SizeBytes 200GB
Add-VMHardDiskDrive -VMName $VMName -Path "$($VMPath)\$($GuestOSName) - Data 1.vhdx" -ControllerType SCSI
New-VHD -Path "$($VMPath)\$($GuestOSName) - Data 2.vhdx" -Dynamic -SizeBytes 200GB
Add-VMHardDiskDrive -VMName $VMName -Path "$($VMPath)\$($GuestOSName) - Data 2.vhdx" -ControllerType SCSI
Set-VMProcessor -VMName $VMName -Count 2 -ExposeVirtualizationExtensions $True
Add-VMNetworkAdapter -VMName $VMName -SwitchName $virtualSwitchName
Add-VMNetworkAdapter -VMName $VMName -SwitchName $virtualSwitchName
Add-VMNetworkAdapter -VMName $VMName -SwitchName $virtualSwitchName
Get-VMNetworkAdapter -VMName $VMName | Set-VMNetworkAdapter -AllowTeaming On
Get-VMNetworkAdapter -VMName $VMName | Set-VMNetworkAdapter -MacAddressSpoofing on
Start-VM $VMName
Wait-PSDirect $VMName -cred $localCred
Invoke-Command -VMName $VMName -Credential $localCred {
param($VMName, $domainCred, $domainName)
Write-Output -InputObject "[$($VMName)]:: Installing Clustering"
$null = Install-WindowsFeature -Name File-Services, Failover-Clustering, Hyper-V -IncludeManagementTools
Write-Output -InputObject "[$($VMName)]:: Joining domain as `"$($env:computername)`""
while (!(Test-Connection -ComputerName $domainName -BufferSize 16 -Count 1 -Quiet -ea SilentlyContinue))
{
Start-Sleep -Seconds 1
}
do
{
Add-Computer -DomainName $domainName -Credential $domainCred -ea SilentlyContinue
}
until ($?)
} -ArgumentList $VMName, $domainCred, $domainName
Wait-PSDirect $VMName -cred $domainCred
Invoke-Command -VMName $VMName -Credential $domainCred {
Rename-NetAdapter -Name 'Ethernet' -NewName 'LOM-P0'
Rename-NetAdapter -Name 'Ethernet 2' -NewName 'LOM-P1'
Rename-NetAdapter -Name 'Ethernet 3' -NewName 'Riser-P0'
Get-NetAdapter -Name 'Ethernet 5' | Rename-NetAdapter -NewName 'Riser-P1'
}
Restart-DemoVM $VMName
#Wait-PSDirect $VMName -cred $domainCred
}
Function Install-WSUS {
#Installs WSUS to the Target VM in the Lab
#Script core functions from Eric @XenAppBlog
param
(
[string]$VMName,
[string]$GuestOSName
)
#Adding WSUS Drive
New-VHD -Path "$($VMPath)\$($GuestOSName) - WSUS Data 1.vhdx" -Dynamic -SizeBytes 400GB
Mount-VHD -Path "$($VMPath)\$($GuestOSName) - WSUS Data 1.vhdx"
$DiskNumber = (Get-Diskimage -ImagePath "$($VMPath)\$($GuestOSName) - WSUS Data 1.vhdx").Number
Initialize-Disk -Number $DiskNumber -PartitionStyle GPT
Get-Disk -Number $DiskNumber | New-Partition -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "WSUS" -Confirm:$False
Dismount-VHD -Path "$($VMPath)\$($GuestOSName) - WSUS Data 1.vhdx"
Add-VMHardDiskDrive -VMName $VMName -Path "$($VMPath)\$($GuestOSName) - WSUS Data 1.vhdx" -ControllerType SCSI
icm -VMName $VMName -Credential $domainCred {
Get-Disk | Where OperationalStatus -EQ "Offline" | Set-Disk -IsOffline $False
Get-Disk | Where Number -NE "0" | Set-Disk -IsReadOnly $False
$Driveletter = get-wmiobject -class "Win32_Volume" -namespace "root\cimv2" | where-object {$_.Label -like "WSUS*"}
$WSUSDrive = $Driveletter.DriveLetter
Install-WindowsFeature -Name UpdateServices -IncludeManagementTools
Install-WindowsFeature -Name UpdateServices-Ui
New-Item -Path $WSUSDrive -Name WSUS -ItemType Directory
CD "C:\Program Files\Update Services\Tools"
.\wsusutil.exe postinstall "CONTENT_DIR=$($WSUSDrive)\WSUS"
Write-Verbose "Get WSUS Server Object" -Verbose
$wsus = Get-WSUSServer
Write-Verbose "Connect to WSUS server configuration" -Verbose
$wsusConfig = $wsus.GetConfiguration()
Write-Verbose "Set to download updates from Microsoft Updates" -Verbose
Set-WsusServerSynchronization -SyncFromMU
Write-Verbose "Set Update Languages to English and save configuration settings" -Verbose
$wsusConfig.AllUpdateLanguagesEnabled = $false
$wsusConfig.SetEnabledUpdateLanguages("en")
$wsusConfig.Save()
Write-Verbose "Get WSUS Subscription and perform initial synchronization to get latest categories" -Verbose
$subscription = $wsus.GetSubscription()
$subscription.StartSynchronizationForCategoryOnly()
While ($subscription.GetSynchronizationStatus() -ne 'NotProcessing') {
Write-Host "." -NoNewline
Start-Sleep -Seconds 5
}
Write-Verbose "Sync is Done" -Verbose
Write-Verbose "Disable Products" -Verbose
Get-WsusServer | Get-WsusProduct | Where-Object -FilterScript { $_.product.title -match "Office" } | Set-WsusProduct -Disable
Get-WsusServer | Get-WsusProduct | Where-Object -FilterScript { $_.product.title -match "Windows" } | Set-WsusProduct -Disable
Write-Verbose "Enable Products" -Verbose
Get-WsusServer | Get-WsusProduct | Where-Object -FilterScript { $_.product.title -match "Windows Server 2016" } | Set-WsusProduct
Write-Verbose "Disable Language Packs" -Verbose
Get-WsusServer | Get-WsusProduct | Where-Object -FilterScript { $_.product.title -match "Language Packs" } | Set-WsusProduct -Disable
Write-Verbose "Configure the Classifications" -Verbose
Get-WsusClassification | Where-Object {
$_.Classification.Title -in (
'Critical Updates',
'Definition Updates',
'Feature Packs',
'Security Updates',
'Service Packs',
'Update Rollups',
'Updates')
} | Set-WsusClassification
Write-Verbose "Configure Synchronizations" -Verbose
$subscription.SynchronizeAutomatically=$true
Write-Verbose "Set synchronization scheduled for midnight each night" -Verbose
$subscription.SynchronizeAutomaticallyTimeOfDay= (New-TimeSpan -Hours 0)
$subscription.NumberOfSynchronizationsPerDay=1
$subscription.Save()
Write-Verbose "Kick Off Synchronization" -Verbose
$subscription.StartSynchronization()
Write-Verbose "Monitor Progress of Synchronisation" -Verbose
Start-Sleep -Seconds 60 # Wait for sync to start before monitoring
while ($subscription.GetSynchronizationProgress().ProcessedItems -ne $subscription.GetSynchronizationProgress().TotalItems) {
#$subscription.GetSynchronizationProgress().ProcessedItems * 100/($subscription.GetSynchronizationProgress().TotalItems)
Start-Sleep -Seconds 5
}
}
#Restart-DemoVM $VMName
#Wait-PSDirect $VMName -cred $DomainCred
icm -VMName $VMName -Credential $domainCred {
#Change server name and port number and $True if it is on SSL
$Computer = $env:COMPUTERNAME
[String]$updateServer1 = $Computer
[Boolean]$useSecureConnection = $False
[Int32]$portNumber = 8530
# Load .NET assembly
[void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
$count = 0
# Connect to WSUS Server
$updateServer = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($updateServer1,$useSecureConnection,$portNumber)
write-host "<<<Connected sucessfully >>>" -foregroundcolor "yellow"
$updatescope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$u=$updateServer.GetUpdates($updatescope )
foreach ($u1 in $u )
{
if ($u1.IsSuperseded -eq 'True')
{
write-host Decline Update : $u1.Title
$u1.Decline()
$count=$count + 1
}
}
write-host Total Declined Updates: $count
trap
{
write-host "Error Occurred"
write-host "Exception Message: "
write-host $_.Exception.Message
write-host $_.Exception.StackTrace
exit
}
# EOF
}
}
Function Install-NetNat {
param
(
[string]$VMName,
[string]$GuestOSName
)
Write-Output -InputObject "[$($VMName)]:: Configuring NAT on the Hyper-V Internal Switch `"$($env:computername)`""
$CheckNATSwitch = get-vmswitch | where Name -eq $virtualNATSwitchName | Select Name
If ($CheckNATSwitch -ne $null) {
write-Host "Internal NAT Switch Found"}
Else {
write-Host "Not Found"
Write-Host "Creating NAT Switch"
New-VMSwitch -SwitchName $virtualNATSwitchName -SwitchType Internal
$ifindex = Get-NetAdapter | Where Name -like *$virtualNATSwitchName* | New-NetIPAddress 192.168.10.1 -PrefixLength 24
Get-Netnat | Remove-NetNat -confirm:$false
New-NetNat -Name $virtualNATSwitchName -InternalIPInterfaceAddressPrefix 192.168.10.0/24
}
}
Function Install-RRAS{
param
(
[string] $VMName,
[string] $GuestOSName,
[string] $IPAddress
)
Add-VMNetworkAdapter -VMName $VMName -SwitchName $virtualNATSwitchName
Invoke-Command -VMName $VMName -Credential $domainCred {
Write-Output -InputObject "[$($VMName)]:: Setting InternetIP Address to 192.168.10.254"
$null = New-NetIPAddress -IPAddress "192.168.10.254" -InterfaceAlias 'Ethernet 2' -PrefixLength 24
$newroute = '192.168.10.1'
Write-Output -InputObject "[$($VMName)]:: Configuring Default Gateway"
$null = Get-Netroute | Where DestinationPrefix -eq "0.0.0.0/0" | Remove-NetRoute -Confirm:$False
#$null = Test-NetConnection localhost
new-netroute -InterfaceAlias "Ethernet 2" -NextHop $newroute -DestinationPrefix '0.0.0.0/0' -verbose
$null = Get-NetAdapter | where name -EQ "Ethernet" | Rename-NetAdapter -NewName CorpNet
$null = Get-NetAdapter | where name -EQ "Ethernet 2" | Rename-NetAdapter -NewName Internet
Write-Output -InputObject "[$($VMName)]:: Installing RRAS"
$null = Install-WindowsFeature -Name RemoteAccess,Routing,RSAT-RemoteAccess-Mgmt
#$null = Stop-Service -Name WDSServer -ErrorAction SilentlyContinue
#$null = Set-Service -Name WDSServer -StartupType Disabled -ErrorAction SilentlyContinue
$ExternalInterface="Internet"
$InternalInterface="CorpNet"
Write-Output -InputObject "[$($VMName)]:: Coniguring RRAS - Adding Internal and External Adapters"
$null = Start-Process -Wait:$true -FilePath "netsh" -ArgumentList "ras set conf ENABLED"
$null = Set-Service -Name RemoteAccess -StartupType Automatic
$null = Start-Service -Name RemoteAccess
Write-Output -InputObject "[$($VMName)]:: Configuring NAT - Lab is now Internet Enabled"
$null = Start-Process -Wait:$true -FilePath "netsh" -ArgumentList "routing ip nat install"
$null = Start-Process -Wait:$true -FilePath "netsh" -ArgumentList "routing ip nat add interface ""CorpNet"""
$null = Test-NetConnection 192.168.10.1
$null = Test-NetConnection 4.2.2.2
$null = cmd.exe /c "netsh routing ip nat add interface $externalinterface"
$null = cmd.exe /c "netsh routing ip nat set interface $externalinterface mode=full"
$null = Test-NetConnection 192.168.10.1
# $null = Test-NetConnection $($Subnet)1
$null = Test-NetConnection 4.2.2.2
Write-Output -InputObject "[$($VMName)]:: Disable FireWall"
$null = cmd.exe /c "netsh firewall set opmode disable"
}
}
#endregion
#region Variable Init
$BaseVHDPath = "$($WorkingDir)\BaseVHDs"
$VMPath = "$($WorkingDir)\VMs"
$localCred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList 'Administrator', (ConvertTo-SecureString -String $adminPassword -AsPlainText -Force)
$domainCred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList "$($domainName)\Administrator", (ConvertTo-SecureString -String $domainAdminPassword -AsPlainText -Force)
#$ServerISO = "D:\DCBuild\10586.0.151029-1700.TH2_RELEASE_SERVER_OEMRET_X64FRE_EN-US.ISO"
#$ServerISO = "d:\DCBuild\14393.0.160808-1702.RS1_Release_srvmedia_SERVER_OEMRET_X64FRE_EN-US.ISO"
#ServerISO = 'D:\DCBuild\en_windows_server_2016_technical_preview_5_x64_dvd_8512312.iso'
#$ServerISO = 'c:\ClusterStorage\Volume1\DCBuild\en_windows_server_2016_x64_dvd_9327751.iso' #Updated for RTM Build 2016
$ServerISO = 'f:\dcbuild_Insider\en_windows_server_2016_x64_dvd_9718492.iso' #THIS NEEDS to be Modified for your Lab
$ServerISO1 = 'F:\DCBuild_Insider\Windows_InsiderPreview_Server_17079.iso' #THIS NEEDS to be Modified for your Lab
$WindowsKey = '<ProductKey>' #Dave's Technet KEY Remove for Publishing of Book
$unattendSource = [xml]@"
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<servicing></servicing>
<settings pass="specialize">
<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">
<ComputerName>*</ComputerName>
<ProductKey><ProductKey></ProductKey>
<RegisteredOrganization>Organization</RegisteredOrganization>
<RegisteredOwner>Owner</RegisteredOwner>
<TimeZone>TZ</TimeZone>
</component>
</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">
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideLocalAccountScreen>true</HideLocalAccountScreen>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>Work</NetworkLocation>
<ProtectYourPC>1</ProtectYourPC>
</OOBE>
<UserAccounts>
<AdministratorPassword>
<Value>password</Value>
<PlainText>True</PlainText>
</AdministratorPassword>
</UserAccounts>
</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>en-us</InputLocale>
<SystemLocale>en-us</SystemLocale>
<UILanguage>en-us</UILanguage>
<UILanguageFallback>en-us</UILanguageFallback>
<UserLocale>en-us</UserLocale>
</component>
</settings>
</unattend>
"@
#endregion
Write-Log 'Host' 'Getting started...'
Confirm-Path $BaseVHDPath
Confirm-Path $VMPath
Write-Log 'Host' 'Building Base Images'
Write-Log 'Host' 'Downloading January 2018 CU for Windows Server 2016'
if (!(Test-Path -Path "$($BaseVHDPath)\windows10.0-kb3213986-x64_a1f5adacc28b56d7728c92e318d6596d9072aec4.msu"))
{
. Download-BaseImageUpdates
}
if (!(Test-Path -Path "$($BaseVHDPath)\VMServerBase.vhdx"))
{
. Initialize-BaseImage
}
if (!(Test-Path -Path "$($BaseVHDPath)\VMServerBaseCore.vhdx"))
{
. Initialize-BaseImage
}
if ((Get-VMSwitch | Where-Object -Property name -EQ -Value $virtualSwitchName) -eq $null)
{
New-VMSwitch -Name $virtualSwitchName -SwitchType Private
}
Invoke-DemoVMPrep 'DC01' 'DC01' -FullServer
Invoke-DemoVMPrep 'DHCP01' 'DHCP01'-FullServer
Invoke-DemoVMPrep 'Management01' 'Management01' -FullServer
Invoke-DemoVMPrep 'Router01' 'Router01' -FullServer
Invoke-DemoVMPrep 'VMM01' 'VMM01' -FullServer
$VMName = 'DC01'
$GuestOSName = 'DC01'
$IPNumber = '1'
Create-DemoVM $VMName $GuestOSName $IPNumber
Invoke-Command -VMName $VMName -Credential $localCred {
param($VMName, $domainName, $domainAdminPassword)
$newroute = '172.16.100.254'
Write-Output -InputObject "[$($VMName)]:: Configuring Default Gateway"
$null = Get-Netroute | Where DestinationPrefix -eq "0.0.0.0/0" | Remove-NetRoute -Confirm:$False
$null = Test-NetConnection localhost
new-netroute -InterfaceAlias "Ethernet" -NextHop $newroute -DestinationPrefix '0.0.0.0/0' -verbose
Write-Output -InputObject "[$($VMName)]:: Installing AD"
$null = Install-WindowsFeature AD-Domain-Services -IncludeManagementTools
Write-Output -InputObject "[$($VMName)]:: Enabling Active Directory and promoting to domain controller"
Install-ADDSForest -DomainName $domainName -InstallDNS -NoDNSonNetwork -NoRebootOnCompletion `
-SafeModeAdministratorPassword (ConvertTo-SecureString -String $domainAdminPassword -AsPlainText -Force) -confirm:$false
} -ArgumentList $VMName, $domainName, $domainAdminPassword
Restart-DemoVM $VMName
Wait-PSDirect $VMName -cred $domainCred
Invoke-Command -VMName $VMName -Credential $domainCred {
param($VMName, $domainName, $domainAdminPassword)
Write-Output -InputObject "[$($VMName)]:: Installing ADCS"
$null = Install-WindowsFeature AD-Certificate -IncludeAllSubFeature -IncludeManagementTools
} -ArgumentList $VMName, $domainName, $domainAdminPassword
Restart-DemoVM $VMName
$VMName = 'DHCP01'
$GuestOSName = 'DHCP01'
$IPNumber = '3'
Create-DemoVM $VMName $GuestOSName $IPNumber
Invoke-Command -VMName $VMName -Credential $localCred {
param($VMName, $domainCred, $domainName)
$newroute = '172.16.100.254'
Write-Output -InputObject "[$($VMName)]:: Configuring Default Gateway"
$null = Get-Netroute | Where DestinationPrefix -eq "0.0.0.0/0" | Remove-NetRoute -Confirm:$False
$null = Test-NetConnection localhost
new-netroute -InterfaceAlias "Ethernet" -NextHop $newroute -DestinationPrefix '0.0.0.0/0' -verbose
Write-Output -InputObject "[$($VMName)]:: Installing DHCP"
$null = Install-WindowsFeature DHCP -IncludeManagementTools
Write-Output -InputObject "[$($VMName)]:: Joining domain as `"$($env:computername)`""
while (!(Test-Connection -ComputerName $domainName -BufferSize 16 -Count 1 -Quiet -ea SilentlyContinue))
{
Start-Sleep -Seconds 1
}
do
{
Add-Computer -DomainName $domainName -Credential $domainCred -ea SilentlyContinue
}
until ($?)
} -ArgumentList $VMName, $domainCred, $domainName
Restart-DemoVM $VMName
Wait-PSDirect $VMName -cred $domainCred
Invoke-Command -VMName $VMName -Credential $domainCred {
param($VMName, $domainName, $Subnet, $IPNumber)
Write-Output -InputObject "[$($VMName)]:: Waiting for name resolution"
while ((Test-NetConnection -ComputerName $domainName).PingSucceeded -eq $false)
{
Start-Sleep -Seconds 1
}
Write-Output -InputObject "[$($VMName)]:: Configuring DHCP Server"
Set-DhcpServerv4Binding -BindingState $true -InterfaceAlias Ethernet
Add-DhcpServerv4Scope -Name 'IPv4 Network' -StartRange "$($Subnet)10" -EndRange "$($Subnet)200" -SubnetMask 255.255.255.0
Set-DhcpServerv4OptionValue -OptionId 6 -value "$($Subnet)1"
Set-DhcpServerv4OptionValue -OptionId 3 -value "$($Subnet)254"
Add-DhcpServerInDC -DnsName "$($env:computername).$($domainName)"
foreach($i in 1..99)
{
$mac = '00-b5-5d-fe-f6-' + ($i % 100).ToString('00')
$ip = $Subnet + '1' + ($i % 100).ToString('00')
$desc = 'Container ' + $i.ToString()
$scopeID = $Subnet + '0'
Add-DhcpServerv4Reservation -IPAddress $ip -ClientId $mac -Description $desc -ScopeId $scopeID
}
} -ArgumentList $VMName, $domainName, $Subnet, $IPNumber
Restart-DemoVM $VMName
$VMName = 'DC01'
$GuestOSName = 'DC01'
$IPNumber = '1'
Wait-PSDirect $VMName -cred $domainCred
Invoke-Command -VMName $VMName -Credential $domainCred {
param($VMName, $password)
Write-Output -InputObject "[$($VMName)]:: Creating user account for Dave"
do
{
Start-Sleep -Seconds 5
New-ADUser `
-Name 'Dave' `
-SamAccountName 'Dave' `
-DisplayName 'Dave' `
-AccountPassword (ConvertTo-SecureString -String $password -AsPlainText -Force) `
-ChangePasswordAtLogon $false `
-Enabled $true -ea 0
}
until ($?)
Add-ADGroupMember -Identity 'Domain Admins' -Members 'Dave'
} -ArgumentList $VMName, $domainAdminPassword
Invoke-Command -VMName $VMName -Credential $domainCred {
param($VMName, $password)
Write-Output -InputObject "[$($VMName)]:: Creating user account for SVC_SQL"
do
{
Start-Sleep -Seconds 5
New-ADUser `
-Name 'SVC_SQL' `