-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject 1.ps1
1053 lines (762 loc) · 39.5 KB
/
Project 1.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
# The purpose of the project was to deploy a new lab envrionement using Hyper-V that includes Active Directory with two sites, instnaciating GPO policy and deploying a Microsoft based iSCSI storage solution with network wide shares and WDS
#region Modules
# The ADDSDeployment can only be imported after the AD Installed see line 68
#Import-Module ADDSDeployment
Import-Module GroupPolicy
#endregion
#region Constants
#region Credential
$C_CRED_LOCAL = New-Object System.Management.Automation.PSCredential -ArgumentList "Administrator",$(ConvertTo-SecureString -AsPlainText "Pa55w.rd" -Force)
$C_CRED_DOMAIN = New-Object System.Management.Automation.PSCredential -ArgumentList "Proj\Administrator",$(ConvertTo-SecureString -AsPlainText "Pa55w.rd" -Force)
#endregion
#region Domain Configurations
$C_DOMAINNAME="proj.local"
$C_WINSNAME = "PROJ"
#endregion
#region VMNames
$C_HYPERV_NAME_BASE = "PROJ-BASE-SERVER"
$C_HYPERV_NAME_DC = "PROJ-DC-SERVER"
$C_HYPERV_NAME_STR = "PROJ-STR-SERVER"
$C_HYPERV_NAME_FS0 = "PROJ-FS0-SERVER"
$C_HYPERV_NAME_FS1 = "PROJ-FS1-SERVER"
$C_HYPERV_NAME_CL1 = "PROJ-CL1"
$C_HYPERV_NAME_WDS = "PROJ-WDS-SERVER"
#endregion
#endregion
#region Scripts
#region Create Differncing
function fncCreateBase
{
$strVMName = $C_HYPERV_NAME_BASE
$strPath = "E:\Project1\$strVMName"
$strHDPath = "$strPath\Virtual Hard Disks\$strVMName-DISK.vhdx"
# Creates the VM
New-VM -Name $strVMName `
-NoVHD `
-Generation 2 `
-MemoryStartupBytes 2048MB `
-BootDevice CD `
-Path $strPath
# Disable CheckPoint
Set-VM -VMName $strVMName -CheckpointType Disabled
# Create a VHD
New-VHD -Path $strHDPath `
-SizeBytes 120GB `
-Dynamic
# Attatch VHD to VM
Add-VMHardDiskDrive -VMName $strVMName `
-ControllerType SCSI `
-ControllerNumber 0 `
-Path $strHDPath
}
#endregion
#region Set DC
#region Create DC
Function fncCreateDC
{
$strVMName = $C_HYPERV_NAME_DC
$strPath = "E:\Project1\$C_HYPERV_NAME_DC"
$strHDPath = "$strPath\Virtual Hard Disks\$strVMName-DISK.vhdx"
# Creates the VM
New-VM -Name $strVMName `
-NoVHD `
-Generation 2 `
-MemoryStartupBytes 4096MB `
-BootDevice CD `
-Path $strPath
New-VHD -ParentPath "E:\Project1\$C_HYPERV_NAME_BASE\Virtual Hard Disks\$C_HYPERV_NAME_BASE-DISK.vhdx" `
-path $strHDPath `
-Differencing
Add-VMHardDiskDrive -VMName $strVMName `
-ControllerType SCSI `
-ControllerNumber 0 `
-ControllerLocation 1 `
-Path $strHDPath
Connect-VMNetworkAdapter -VMName $C_HYPERV_NAME_DC `
-SwitchName "PROJ_INTERNAL"
# Set Hard Disk as startup device(automatically it sets the network device as startup object)
Set-VMFirmware -VMName $strVMName -FirstBootDevice $(Get-VMFirmware -VMName $strVMName).BootOrder[2]
}
#endregion
#region Set DC name
function fncSetDCNameIP
{
$sbScriptBlock = {
# Set Static IP and Computer name
New-NetIPAddress -InterfaceAlias "Ethernet" `
-IPAddress 10.0.1.1 `
-DefaultGateway 10.0.1.1 `
-PrefixLength 24
Set-DnsClientServerAddress -InterfaceAlias Ethernet `
-ServerAddresses "10.0.1.1"
Rename-Computer -NewName "TLV-PROJ-DC"
Install-WindowsFeature -Name 'DHCP' –IncludeManagementTools -Restart
Restart-Computer -Force
}
Invoke-Command -VMName $C_HYPERV_NAME_DC -Credential $C_CRED_LOCAL -ScriptBlock $sbScriptBlock
}
#endregion
#region Install AD
Function fncInstallAD
{
$sbScriptBlock = {
param($C_DOMAINNAME,
$C_WINSNAME)
# Install Active Directory
# Install Features
Install-WindowsFeature AD-Domain-Services -IncludeManagementTools
Import-Module ADDSDeployment
# Set the basic forest
Install-ADDSForest -CreateDnsDelegation:$false `
-DatabasePath "C:\Windows\NTDS" `
-DomainMode Win2012R2 `
-DomainName $C_DOMAINNAME `
-DomainNetbiosName $C_WINSNAME `
-ForestMode Win2012R2 `
-InstallDns:$true `
-LogPath "c:\windows\ntds" `
-NoRebootOnCompletion:$true `
-SysvolPath "c:\windows\sysvol" `
-Force:$true `
-SafeModeAdministratorPassword $(ConvertTo-SecureString "Pa55w.rd" -AsPlainText -Force)
Restart-Computer -Force
}
Invoke-Command -VMName $C_HYPERV_NAME_DC -Credential $C_CRED_LOCAL -ScriptBlock $sbScriptBlock -ArgumentList $C_DOMAINNAME,$C_WINSNAME
}
#endregion
#region Set Site
Function fncSetSite
{
$sbScriptBlock = {
Rename-ADObject -Identity "CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=PROJ,DC=COM" `
-NewName TLV
Get-ADReplicationSite TLV | Set-ADReplicationSite -Replace @{ "location" = "Tel Aviv, Israel" }
New-ADReplicationSubnet -Name "10.0.1.0/24" `
-Location "Tel Aviv, Israel"
}
Invoke-Command -VMName $C_HYPERV_NAME_DC -Credential $C_CRED_DOMAIN -ScriptBlock $sbScriptBlock
}
#endregion
#region Set DHCP
function fncSetDHCPInDC
{
$sbScriptBlock = {
Add-DhcpServerV4Scope -Name "DHCP Scope" -StartRange 10.0.1.100 -EndRange 10.0.1.253 -SubnetMask 255.255.255.0
Set-DhcpServerV4OptionValue -DnsServer 10.0.1.1 -Router 10.0.1.1
Set-DhcpServerv4Scope -ScopeId 10.0.1.100 -LeaseDuration 1.00:00:00
Restart-service dhcpserver
}
Invoke-Command -VMName $C_HYPERV_NAME_DC -Credential $C_CRED_DOMAIN -ScriptBlock $sbScriptBlock
}
#endregion
#region Create Users
function fncImportUsersFromCSV
{
$sbScriptBlock = {
param($C_DOMAINNAME)
#region Create Users Script
#region Formatting
# Format DC path based on $C_DOMAINNAME
function fncFormatDC
{
return $("DC=$C_DOMAINNAME".Replace(".",",DC="))
}
# Format OU path based on a given syntax subou.subou...subou.primeou for example Users.Test
function fncFormatOU
{
param([string]$strOU)
return $([string]$( "OU=$strOU").Replace(".",",OU="))
}
# Format the path fully
function fncFormatFinalString
{
param([string]$strOU)
return [string]$("$(fncFormatOU $strOU),$(fncFormatDC)")
}
#endregion
#region Assistent function for creation of Users based on CSV
# Check if OU path exists based on a given the syntax of the OU path as "Sub...SubSubOU.SubOU.OU" for example Users.Test
function fncCheckIfOUExist
{
Param([string]$strOU)
return [adsi]::Exists("LDAP://$(fncFormatFinalString $strOU)")
}
Function fncCheckIfGroupExist
{
Param([string]$strOU,
[string]$strGroup)
return [adsi]::Exists("LDAP://CN=$strGroup,$strPath")
}
# Create OU recusivly in the main path of the domain given the syntax of the OU path as "Sub...SubSubOU.SubOU.OU" for example Users.Test
Function fncRecurseCreateOU
{
param([string]$strOU)
$arrOU = $strOU.Split('.')
$strPath="$(fncFormatDC)"
for ($nCurrOU=$($arrOU.Length-1); $nCurrOU -gt -1; $nCurrOU--)
{
If(!(Get-ADOrganizationalUnit -Filter "DistinguishedName -eq 'OU=$($arrOU[$nCurrOU]),$strPath'"))
{
try
{
New-ADOrganizationalUnit -Name $arrOU[$nCurrOU] `
-Path $strPath `
-ProtectedFromAccidentalDeletion $false `
-Server $C_DOMAINNAME
}
catch{}
}
$strPath = "OU=$($arrOU[$nCurrOU])" + ',' + $strPath
}
}
# Creates a new group in an ou named groups under the root
Function fncCreateGroup
{
param($strOU,
$strGroupName)
$strRelvantOU = $strOU.Split('.')[$strOU.Split('.').Length-1]
$strPath = fncFormatFinalString "Groups.$strRelvantOU"
If(!(Get-ADOrganizationalUnit -Filter "DistinguishedName -eq '$strPath'"))
{
New-ADOrganizationalUnit -Name "Groups" `
-Path $(fncFormatFinalString $strRelvantOU) `
-ProtectedFromAccidentalDeletion $false `
-Server $C_DOMAINNAME
}
if (!$(fncCheckIfGroupExist $strOU $strGroupName))
{
New-ADGroup -DisplayName $strGroupName `
-Name $strGroupName `
-SamAccountName $strGroupName `
-GroupScope Global `
-Path $strPath `
-Server $C_DOMAINNAME `
-ManagedBy "Administrators" `
-GroupCategory Security `
}
}
# Function To add a single user
Function fncAddUser
{
param([string]$strUserName,
[string]$strFirstName,
[string]$strLastName,
[string]$strPassword,
[string]$strOU,
[string]$strGroup)
if($(fncCheckIfOUExist $strOU) -eq $false)
{
fncRecurseCreateOU $strOU
}
try
{
fncCreateGroup $strOU $strGroup
}catch{}
try
{
New-ADUser -SamAccountName $strUserName `
-UserPrincipalName "$strUserName@$C_DOMAINNAME" `
-Name "$strFirstName $strLastName" `
-GivenName "$strFirstName" `
-Surname "$strLastName" `
-Enabled $true `
-DisplayName "$strFirstName $strLastName" `
-Path $(fncFormatFinalString $strOU) `
-AccountPassword $(ConvertTo-SecureString $strPassword -AsPlainText -Force)`
-ChangePasswordAtLogon $true
}catch{}
# Add user to a new group. In Identity it is formated as "CN=[Group Name],OU=Groups,OU=[Top most ou(e.g. Marketing in Users.Marketing)],DC=proj,DC=com"
try
{
Add-ADGroupMember -Identity $strGroup `
-Members "$strUserName" `
}catch{}
}
#endregion
#endregion
# Import from CSV
#===========================================================================
# Example Of the CSV Format:
#===========================================================================
# fname,lname,uname,pass,ou
# [First Name],[Last Name],[User Name],[Password],[Sub...SubSubOU.SubOU.OU]
Function fncImportUsersFromCSV
{
param([string]$strPath)
$csv = Import-Csv $strPath
foreach ($usrCurrUser in $csv)
{
try
{
fncAddUser $usrCurrUser.uname $usrCurrUser.fname $usrCurrUser.lname $usrCurrUser.pass $usrCurrUser.ou $usrCurrUser.group
echo("User $($usrCurrUser.uname) has been created at $(fncFormatDC $usrCurrUser.ou)")
}
catch
{
echo("User $($usrCurrUser.uname) at $(fncFormatDC $usrCurrUser.ou) has failed due to $($_.Exception.Message)")
}
}
}
fncImportUsersFromCSV e:\USERS.CSV
}
Invoke-Command -VMName $C_HYPERV_NAME_DC -Credential $C_CRED_DOMAIN -ScriptBlock $sbScriptBlock -ArgumentList $C_DOMAINNAME
}
#endregion
#region GPO
Function fncSetPasswordPolicy
{
$sbScriptBlock = {
param($C_DOMAINNAME)
Set-ADDefaultDomainPasswordPolicy -Identity $C_DOMAINNAME `
-MaxPasswordAge 120d `
-ComplexityEnabled $true `
-MinPasswordLength 7 `
-PasswordHistoryCount 24 `
-LockoutDuration 0 `
-LockoutObservationWindow 0 `
-LockoutThreshold 3
}
Invoke-Command -VMName $C_HYPERV_NAME_DC -Credential $C_CRED_DOMAIN -ScriptBlock $sbScriptBlock -ArgumentList $C_DOMAINNAME
}
#endregion
#endregion
#region Storage
#region Storage
#region Create Storage
Function fncCreateStorageVM
{
$strVMName = $C_HYPERV_NAME_STR
$strPath = "E:\Project1\$strVMName"
$strHDPath = "$strPath\Virtual Hard Disks\$strVMName-DISK.vhdx"
# Creates the VM
New-VM -Name $strVMName `
-NoVHD `
-Generation 2 `
-MemoryStartupBytes 2048MB `
-Path $strPath
# Creates a new HD
New-VHD -ParentPath "E:\Project1\$C_HYPERV_NAME_BASE\Virtual Hard Disks\$C_HYPERV_NAME_BASE-DISK.vhdx" `
-path $strHDPath `
-Differencing
# Attach main path
Add-VMHardDiskDrive -VMName $strVMName `
-Path $strHDPath `
-ControllerNumber 0
#region Setting Network
# Add the internal network switch
Connect-VMNetworkAdapter -SwitchName "PROJ_ST" -VMName $strVMName
# Set Hard Disk as startup device(automatically it sets the network device as startup object)
Set-VMFirmware -VMName $strVMName -FirstBootDevice $(Get-VMFirmware -VMName $strVMName).BootOrder[1]
}
#endregion
#endregion Create Storage
#region Initialize Storage
Function fncInitStr
{
$sbScriptBlock = {
$strVMName = "TLV-PROJ-STR"
$strIP1 = "10.0.3.1"
# Set Static IP and Computer name
New-NetIPAddress -InterfaceAlias "Ethernet" `
-IPAddress $strIP1 `
-PrefixLength 24
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" `
-ServerAddresses "10.0.3.1"
Rename-Computer -NewName $strVMName
# Install the iscsi target feature
Install-WindowsFeature -Name FS-iSCSITarget-Server, FS-Fileserver `
-IncludeAllSubFeature `
-IncludeManagementTools
Restart-Computer -Force
}
Invoke-Command -VMName $C_HYPERV_NAME_STR `
-Credential $C_CRED_LOCAL `
-ScriptBlock $sbScriptBlock
}
#endregion
#region Setup iSCSI target
Function fncSetiSCSI
{
$sbScriptBlock = {
Function fncAddDrive
{
param ( $strDriveName,
$strSize)
$strPath = "C:\vhd\$($strDriveName).vhdx"
echo $strSize
# Create New VHD
New-IscsiVirtualDisk -path $strPath `
-SizeBytes $strSize
# Add vhd to the iscsi server
Add-IscsiVirtualDiskTargetMapping -TargetName "iSCSISTR" `
-Path $strPath
}
# Starting iSCSI service
Set-Service -Name msiscsi `
-StartupType Automatic
Start-Service msiscsi
# Set the settings for a new iScsi target server
New-IscsiServerTarget -TargetName "iSCSISTR" `
-InitiatorId @("IPAddress:10.0.3.2","IPAddress:10.0.3.3")
fncAddDrive "strQuorum" 1GB
fncAddDrive "strStorage50G" 50GB
}
Invoke-Command -VMName $C_HYPERV_NAME_STR `
-Credential $C_CRED_LOCAL `
-ScriptBlock $sbScriptBlock
}
#endregion
#endregion Storage
#region FS
#region Create FS-VM
function fncNewFSVM
{
param([string]$strVMName)
#region Variables
$strVMName = $strVMName
$strPath = "E:\Project1\$strVMName"
$strHDPath = "$strPath\Virtual Hard Disks\$strVMName-DISK.vhdx"
#endregion
#region VM Creation
# Creates the VM
New-VM -Name $strVMName `
-NoVHD `
-Generation 2 `
-MemoryStartupBytes 2048MB `
-BootDevice CD `
-Path $strPath`
#endregion
#region Setting Network
# Add the internal network switch
Connect-VMNetworkAdapter -SwitchName "PROJ_ST" -VMName $strVMName
#endregion
#region Setting Storage
# Create new main drive
New-VHD -ParentPath "E:\Project1\$C_HYPERV_NAME_BASE\Virtual Hard Disks\$C_HYPERV_NAME_BASE-DISK.vhdx" `
-path $strHDPath `
-Differencing
# Attach main path
Add-VMHardDiskDrive -VMName $strVMName `
-Path $strHDPath `
#Set Hard Disk as boot path (by default it is in the 2th rank)
Set-VMFirmware -VMName $strVMName -FirstBootDevice $(Get-VMFirmware -VMName $strVMName).BootOrder[2]
#endregion
}
Function fncCreateFSFUll
{
for($nCountVM = 0; $nCountVM -lt 2; $nCountVM ++)
{
fncNewFSVM "PROJ-FS$nCountVM-Server"
}
}
#endregion
#region Set the FS initial settings
Function fncSetFSName
{
$arrVM = get-vm proj-fs*
for ($nCountMachine = 0;$nCountMachine -lt 2; $nCountMachine++)
{
$vmFS = $arrVM[$nCountMachine]
$sbScriptBlock = {
param ($nCountMachine)
$strVMName = "TLV-PROJ-FS$nCountMachine"
$strIP1 = "10.0.3.$($nCountMachine+2)"
# Set Static IP and Computer name
New-NetIPAddress -InterfaceAlias "Ethernet" `
-IPAddress $strIP1 `
-PrefixLength 24 `
-Verbose
# Install Failover-Clustering
Install-WindowsFeature Failover-Clustering -IncludeAllSubFeature `
-IncludeManagementTools
Install-WindowsFeature FS-FileServer -IncludeAllSubFeature `
-IncludeManagementTools
Rename-Computer -NewName $strVMName
Restart-Computer -Force
}
Invoke-Command -VMName $vmFS.name -Credential $C_CRED_LOCAL -ScriptBlock $sbScriptBlock -ArgumentList $nCountMachine -AsJob -Verbose
}
}
#endregion
#region Set iSCSI Initator
Function fncSetIscsiInitiator
{
$arrVM = get-vm proj-fs*
for ($nCountMachine = 0;$nCountMachine -lt 2; $nCountMachine++)
{
$vmFS = $arrVM[$nCountMachine]
$sbScriptBlock = {
param ($nCountMachine)
# Autostart iscsiservice on start
Set-Service -Name msiscsi -StartupType Automatic
Start-Service msiscsi
# Set Iscsi target portal
New-IscsiTargetPortal –TargetPortalAddress 10.0.3.1
Get-IscsiTarget | Connect-IscsiTarget -IsPersistent $true
# Initialize disks, format them and bring them online
if($nCountMachine -eq 0)
{
get-disk | Where-Object -FilterScript {$_.Size -eq 1GB} | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false -Force | Set-Disk -IsOffline $false
get-disk | Where-Object -FilterScript {$_.Size -eq 50GB} | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false -Force | Set-Disk -IsOffline $false
}
}
Invoke-Command -VMName $vmFS.name -Credential $C_CRED_LOCAL -ScriptBlock $sbScriptBlock -ArgumentList $nCountMachine
}
}
#endregion
#region Join DC
function fncAddNetworkInfrastructureToFSForDC
{
$arrVM = Get-VM "PROJ-FS*"
# Add Network Adapater
foreach($vm in $arrVM)
{
Add-VMNetworkAdapter -VMName $vm.name -SwitchName "PROJ_INTERNAL"
}
for ($nCountMachine = 0;$nCountMachine -lt 2; $nCountMachine++)
{
$vmFS = $arrVM[$nCountMachine]
$sbScriptBlock = {
param ($nCountMachine,
$cred,
$domain )
# Change IP to 10.0.1.0/24 range. 10.0.1.1 is used by DC
Get-NetIPAddress -InterfaceAlias "Ethernet 2" | New-NetIpAddress -IPAddress "10.0.1.$($nCountMachine+2)" -PrefixLength 24
Set-DnsClientServerAddress -InterfaceAlias "Ethernet 2" -ServerAddresses "10.0.1.1"
Add-Computer -DomainName $domain `
-Credential $cred
Restart-Computer -Force
}
Invoke-Command -VMName $vmFS.name `
-Credential $C_CRED_LOCAL `
-ScriptBlock $sbScriptBlock `
-ArgumentList $nCountMachine, $C_CRED_DOMAIN, $C_DOMAINNAME `
-AsJob
}
}
#endregion
#region Set Failover Clustering Networking
Function fncSetFailoverClustering
{
$arrVM = get-vm "proj-fs*"
foreach($vm in $arrVM)
{
Add-VMNetworkAdapter -VMName $vm.name -SwitchName "PROJ_FS"
}
for ($nCountMachine = 0;$nCountMachine -lt 2; $nCountMachine++)
{
$vmFS = $arrVM[$nCountMachine]
$sbScriptBlock = {
param ($nCountMachine)
Get-NetIPAddress -InterfaceAlias "Ethernet 3" | New-NetIpAddress -IPAddress "10.0.2.$($nCountMachine+1)" -PrefixLength 24
}
Invoke-Command -VMName $vmFS.name `
-Credential $C_CRED_DOMAIN `
-ScriptBlock $sbScriptBlock `
-ArgumentList $nCountMachine
}
$sbScriptBlock = {
Test-Cluster –Node TLV-PROJ-FS0, TLV-PROJ-FS1
New-Cluster -Name FSFailover `
-Node TLV-PROJ-FS0, TLV-PROJ-FS1 `
-StaticAddress 10.0.2.30/24,10.0.1.30/24 `
-IgnoreNetwork 10.0.3.0/24 `
-AdministrativeAccessPoint ActiveDirectoryAndDns -
}
Invoke-Command -VMName $C_HYPERV_NAME_FS0 `
-Credential $C_CRED_DOMAIN `
-ScriptBlock $sbScriptBlock
}
#endregion
#endregion
#endregion
#region Shares
#region Create shares for each department
Function fncCreateShares
{
$sbScriptBlock = {
Add-ClusterFileServerRole -Storage "Cluster Disk 1" `
-Name SharedStorage `
-StaticAddress 10.0.1.40/24 `
-IgnoreNetwork 10.0.2.0/24, 10.0.3.0/24
Function fncCreateShare
{
param([string]$strName)
$strTempName = $strName.Replace(" ","")
$strPath = "f:\$strName"
mkdir $strPath
New-SmbShare -Name $strName `
-Path $strPath `
-FullAccess "Proj\Admin$strTempName" `
-ChangeAccess "Proj\Man$strTempName", "Proj\Users$strTempName"
}
fncCreateShare "Costumer Services"
fncCreateShare "Human Resources"
fncCreateShare "Management"
fncCreateShare "Manufacturing"
fncCreateShare "Marketing"
fncCreateShare "Sales"
}
Invoke-Command -VMName $C_HYPERV_NAME_FS0 `
-Credential $C_CRED_DOMAIN `
-ScriptBlock $sbScriptBlock `
}
#endregion
#endregion
#region WDS
#region WDS Setup
#region Create WDS
Function fncCreateWDS
{
$strVMName = $C_HYPERV_NAME_WDS
$strPath = "E:\Project1\$strVMName"
$strHDPath = "$strPath\Virtual Hard Disks\$C_HYPERV_NAME_CL1-DISK.vhdx"
# Creates the VM
New-VM -Name $strVMName `
-NoVHD `
-Generation 2 `
-MemoryStartupBytes 3062MB `
-BootDevice CD `
-Path $strPath
New-VHD -ParentPath "E:\Project1\$C_HYPERV_NAME_BASE\Virtual Hard Disks\$C_HYPERV_NAME_BASE-DISK.vhdx" `
-path $strHDPath `
-Differencing
Add-VMHardDiskDrive -VMName $strVMName `
-ControllerType SCSI `
-ControllerNumber 0 `
-ControllerLocation 1 `
-Path $strHDPath
Connect-VMNetworkAdapter -VMName $strVMName `
-SwitchName "PROJ_INTERNAL"
# Set Hard Disk as startup device(automatically it sets the network device as startup object)
Set-VMFirmware -VMName $strVMName -FirstBootDevice $(Get-VMFirmware -VMName $strVMName).BootOrder[2]
}
#endregion
#region Init WDS name and IP
function fncInitWDS
{
$sbScriptBlock = {
param($strName)
$strVMName = $strName
# Set Static IP and Computer name
New-NetIPAddress -InterfaceAlias "Ethernet" `
-IPAddress 10.0.1.4 `
-DefaultGateway 10.0.1.1 `
-PrefixLength 24
# Set DNS Settings
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" `
-ServerAddresses "10.0.1.1"
Rename-Computer -NewName $strVMName
Restart-Computer -Force
}
Invoke-Command -VMName $C_HYPERV_NAME_WDS `
-Credential $C_CRED_LOCAL `
-ScriptBlock $sbScriptBlock `
-ArgumentList $C_HYPERV_NAME_WDS
}
#endregion
#region Join WDS to the Domain
function fncWDSJoinDC
{
$sbScriptBlock = {
param ($cred,
$domain )
Add-Computer -DomainName $domain `
-Credential $cred
Restart-Computer -Force
}
Invoke-Command -VMName $C_HYPERV_NAME_WDS `
-Credential $C_CRED_LOCAL `
-ScriptBlock $sbScriptBlock `
-ArgumentList $C_CRED_DOMAIN, $C_DOMAINNAME `
-AsJob
}
#endregion
#endregion
#region Setup CL1
#region Create CL1
Function fncCreateCL1
{
$strVMName = $C_HYPERV_NAME_CL1
$strPath = "E:\Project1\$C_HYPERV_NAME_CL1"
$strHDPath = "E:\Project1\$C_HYPERV_NAME_CL1\Virtual Hard Disks\$C_HYPERV_NAME_CL1-DISK.vhdx"
# Creates the VM
New-VM -Name $strVMName `
-NoVHD `
-Generation 2 `
-MemoryStartupBytes 1024MB `
-BootDevice CD `
-Path $strPath
New-VHD -path $strHDPath `
-SizeBytes 50gb `
-Dynamic
Add-VMHardDiskDrive -VMName $strVMName `
-ControllerType SCSI `
-ControllerNumber 0 `
-ControllerLocation 1 `
-Path $strHDPath
Connect-VMNetworkAdapter -VMName $C_HYPERV_NAME_CL1 `
-SwitchName "PROJ_INTERNAL"
}
#endregion
#endregion
#endregion
#endregion
#region triggers
#region Base
# Slide 01
#fncCreateBase
# Slide 05
#attrib.exe +R "E:\Project1\$C_HYPERV_NAME_BASE\Virtual Hard Disks\$C_HYPERV_NAME_BASE-DISK.vhdx"
#endregion
#region DC
# Slide 06
#fncCreateDC
# Slide 08
#fncSetDCNameIP
# Slide 09
#fncInstallAD
# Slide 10
#fncSetSite
# Slide 12
#fncSetDHCPInDC
# Slide 13
#fncImportUsersFromCSV
# Slide 15
#fncSetPasswordPolicy
#endregion
#region Storage
# Slide 17