-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy_otosukeru.ps1
896 lines (737 loc) · 30.6 KB
/
deploy_otosukeru.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
<#
.SYNOPSIS
----------------------------------------------------------------------
Project Otosukeru - Dynamic Proxy Deployment with Terraform
----------------------------------------------------------------------
Version : 1.2
Requires : Veeam Backup & Replication v9.5 Update 4 or later
Supported : Veeam Backup & Replication v10 BETA 2
Author : Anthony Spiteri
Blog : https://anthonyspiteri.net
GitHub : https://www.github.com/anthonyspiteri
.DESCRIPTION
Known Issues and Limitations:
- vSphere API timeouts can happen during apply/destroy phase of Terraform. See Log file to troubleshoot.
- Speed of Proxy deployment depends on underlying infrastructure as well as VM Template. (Testing has shown 5 Windows Proxies can be deployed in 5 minutes)
- Bug on first run where Scale Down might take affect. Rerun with same commands to workaround.
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$Windows,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$Ubuntu,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$CentOS,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$Destroy,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$DHCP,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[int]$SetProxies,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$ProxyPerHost,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$NASProxy,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[Switch]$Workgroup
)
if (!$Windows -and !$Ubuntu -and !$CentOS -and !$Destroy)
{
Write-Host ""
Write-Host ":: - ERROR! Script was run without using a parameter..." -ForegroundColor Red -BackgroundColor Black
Write-Host ":: - Please use: -Windows, -Ubuntu, -Centos or -Destroy" -ForegroundColor Yellow -BackgroundColor Black
Write-Host ""
break
}
$StartTime = Get-Date
#To be run on Server Isntalled with Veeam Backup & Replicaton
if (!(get-pssnapin -name VeeamPSSnapIn -erroraction silentlycontinue))
{
add-pssnapin VeeamPSSnapIn
}
#Get Variables from Master Config
$config = Get-Content config.json | ConvertFrom-Json
function Pause
{
write-Host ""
write-Host ":: Press Enter to continue..." -ForegroundColor Yellow -BackgroundColor Black
Read-Host | Out-Null
}
function ConnectVBRServer
{
#Connect to the Backup & Replication Server and exit on fail
Disconnect-VBRServer
Try
{
Connect-VBRServer -user $config.VBRDetails.Username -password $config.VBRDetails.Password
}
Catch
{
Write-Host -ForegroundColor Red "ERROR: $_" -ErrorAction Stop
Disconnect-VBRServer
Stop-Transcript
Write-Error -Exception "Exiting as we couldn't connect to Veeam Server" -ErrorAction Stop
}
}
function WorkOutProxyCount
{
try
{
$JobObject = Get-VBRJob
$Objects = $JobObject.GetObjectsInJob()
}
catch
{
Write-Host -ForegroundColor Red "ERROR: $_" -ErrorAction Stop
Stop-Transcript
Write-Error -Exception "Exiting as you don't have any Jobs on the Veeam Server" -ErrorAction Stop
}
$JobObject = Get-VBRJob
$VMcount = $Objects.count
#Get ESXi Host Count
$Hosts = Get-VBRServer -Type ESXi
$HostCount = $Hosts.count
if($VMcount -lt 10)
{
$VBRProxyCount = 2
}
elseif ($VMcount -le 20)
{
$VBRProxyCount = 4
}
else
{
$VBRProxyCount = 6
}
if($ProxyPerHost)
{
$VBRProxyCount = $HostCount
}
if($SetProxies)
{
$VBRProxyCount = $SetProxies
}
$global:ProxyCount = $VBRProxyCount
}
function WorkOutIfScaleDown
{
$ProxyList = Get-Content proxy_ips.json | ConvertFrom-Json
$ProxyArray =@($ProxyList)
if($ProxyCount -lt $ProxyList.Value.Count)
{
$ScaleDownValue = $True
}
else
{
$ScaleDownValue = $False
}
$global:ScaleDown = $ScaleDownValue
}
function RenameFileForAntiAffinity
{
$wkdir = Get-Location
if($Windows)
{
Set-Location -Path .\proxy_windows
}
if($Ubuntu -or $CentOS)
{
Set-Location -Path .\proxy_linux
}
Rename-Item .\anti-affinity_tf -NewName .\anti-affinity.tf
Set-Location $wkdir
}
function RenameFileBackForAntiAffinity
{
$wkdir = Get-Location
if($Windows)
{
Set-Location -Path .\proxy_windows
}
if($Ubuntu -or $CentOS)
{
Set-Location -Path .\proxy_linux
}
Rename-Item .\anti-affinity.tf -NewName .\anti-affinity_tf
Set-Location $wkdir
}
function RenameFileForDHCP
{
$wkdir = Get-Location
if($Windows)
{
Set-Location -Path .\proxy_windows
}
if($Ubuntu -or $CentOS)
{
Set-Location -Path .\proxy_linux
}
Rename-Item .\otosukeru-1.tf -NewName .\otosukeru-1_tf
Rename-Item .\otosukeru-1-DHCP_tf -NewName .\otosukeru-1-DHCP.tf
Set-Location $wkdir
}
function RenameFileBackForDHCP
{
$wkdir = Get-Location
if($Windows)
{
Set-Location -Path .\proxy_windows
}
if($Ubuntu -or $CentOS)
{
Set-Location -Path .\proxy_linux
}
Rename-Item .\otosukeru-1_tf -NewName .\otosukeru-1.tf
Rename-Item .\otosukeru-1-DHCP.tf -NewName .\otosukeru-1-DHCP_tf
Set-Location $wkdir
}
function RenameFileForWorkGroup
{
$wkdir = Get-Location
if($Windows)
{
Set-Location -Path .\proxy_windows
}
Rename-Item .\otosukeru-1.tf -NewName .\otosukeru-1_tf
Rename-Item .\otosukeru-1-NoAD_tf -NewName .\otosukeru-1-NoAD.tf
Set-Location $wkdir
}
function RenameFileBackForWorkGroup
{
$wkdir = Get-Location
if($Windows)
{
Set-Location -Path .\proxy_windows
}
Rename-Item .\otosukeru-1_tf -NewName .\otosukeru-1.tf
Rename-Item .\otosukeru-1-NoAD.tf -NewName .\otosukeru-1-NoAD_tf
Set-Location $wkdir
}
function WindowsProxyBuild
{
$host.ui.RawUI.WindowTitle = "Deploying Windows Proxies with Terraform"
$wkdir = Get-Location
Set-Location -Path .\proxy_windows
& .\terraform.exe init
& .\terraform.exe apply --var "vsphere_proxy_number=$ProxyCount" -auto-approve
& .\terraform.exe output -json proxy_ip_addresses > ..\proxy_ips.json
& .\terraform.exe output -json proxy_vm_names > ..\proxy_vms.json
Set-Location $wkdir
}
function LinuxProxyBuild
{
$host.ui.RawUI.WindowTitle = "Deploying Linux Proxies with Terraform"
if($Ubuntu)
{
$distro = "ubuntu"
}
if($CentOS)
{
$distro = "centos"
}
$wkdir = Get-Location
Set-Location -Path .\proxy_linux
& .\terraform.exe init
& .\terraform.exe apply --var "vpshere_linux_distro=$distro" --var "vsphere_proxy_number=$ProxyCount" -auto-approve
& .\terraform.exe output -json proxy_ip_addresses > ..\proxy_ips.json
& .\terraform.exe output -json proxy_vm_names > ..\proxy_vms.json
Set-Location $wkdir
}
function ProxyDestroy
{
if($Destroy -and $Windows)
{
$host.ui.RawUI.WindowTitle = "Destroying Windows Proxies with Terraform"
$wkdir = Get-Location
Set-Location -Path .\proxy_windows
& .\terraform.exe destroy --force
Set-Location $wkdir
}
if($Destroy -and ($Ubuntu -or $CentOS))
{
$host.ui.RawUI.WindowTitle = "Destroying Linux Proxies with Terraform"
$wkdir = Get-Location
Set-Location -Path .\proxy_linux
& .\terraform.exe destroy --force
Set-Location $wkdir
}
}
function AddVeeamProxy
{
$host.ui.RawUI.WindowTitle = "Adding Veeam Proxies"
$ProxyList = Get-Content proxy_ips.json | ConvertFrom-Json
$ProxyArray =@($ProxyList)
$ProxyVMNames = Get-Content proxy_vms.json | ConvertFrom-Json
$ProxyVMArray =@($ProxyVMNames)
if(!$ProxyArray)
{
Write-Error -Exception "Exiting due to Terraform Proxy Deployment Issue" -ErrorAction Stop
}
if(!$Workgroup)
{
$WindowsUsername = $config.VBRDetails.Username
$WindowsPassword = $config.VBRDetails.Password
$WindowsDescription = "Windows Domain Account"
}
if($Workgroup)
{
$WindowsUsername = $config.VBRDetails.Username2
$WindowsPassword = $config.VBRDetails.Password2
$WindowsDescription = "Windows Server Account"
}
$ExistingWinCredential = Get-VBRCredentials -Name $WindowsUsername
$ExistingLinuxCredential = Get-VBRCredentials | Where-Object {$_.Description -eq "Proxy Linux Admin"}
if($Windows -and !$ExistingWinCredential)
{
Add-VBRCredentials -Type Windows -User $WindowsUsername -Password $WindowsPassword -Description $WindowsDescription | Out-Null
}
if($Ubuntu -and !$ExistingLinuxCredential)
{
Add-VBRCredentials -Type Linux -User $config.LinuxProxy.LocalUsername -Password $config.LinuxProxy.LocalPasswordUbuntu -ElevateToRoot -Description "Proxy Linux Admin" | Out-Null
}
if($CentOS -and !$ExistingLinuxCredential)
{
Add-VBRCredentials -Type Linux -User $config.LinuxProxy.LocalUsername -Password $config.LinuxProxy.LocalPasswordCentOS -ElevateToRoot -Description "Proxy Linux Admin" | Out-Null
}
for ($i=0; $i -lt $ProxyCount; $i++)
{
$ProxyEntity = $ProxyArray.value[$i]
$ProxyVMEntity = $ProxyVMArray.value[$i]
$ProxyName = $ProxyArray.value[$i]
#Return True or False if Proxy Exists in VBR
$ProxyExists = Get-VBRViProxy | Where-Object {$_.Name -eq $ProxyName}
#Add Proxy to Backup & Replication
Write-Host ":: Adding Proxy Server to Backup & Replication" -ForegroundColor Yellow
if($Windows)
{
#Get and Set Windows Credential
if(!$ProxyExists)
{
$WindowsCredential = Get-VBRCredentials | where-object {$_.Description -eq $WindowsDescription}
}
#Add Windows Server to VBR and Configure Proxy
if(!$ProxyExists)
{
try
{
Add-VBRWinServer -Name $ProxyEntity -Description "Dynamic Veeam Proxy" -Credentials $WindowsCredential -ErrorAction Stop | Out-Null
}
Catch
{
Write-Host -ForegroundColor Red "ERROR: $_" -ErrorAction Stop
Get-VBRCredentials | where-object {$_.Description -eq $WindowsDescription} | Remove-VBRCredentials -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Stop-Transcript
Write-Error -Exception "Exiting due to issues adding Windows Proxy to Veeam Server" -ErrorAction Stop
}
Write-Host ":: Creating New Veeam Windows Proxy" -ForegroundColor Yellow
Add-VBRViProxy -Server $ProxyEntity -MaxTasks 4 -TransportMode HotAdd -ConnectedDatastoreMode Auto -EnableFailoverToNBD | Out-Null
}
}
if ($Ubuntu -or $CentOS)
{
#Get and Set Linux Credentials and Set ProxyVM from VM Entity List
if(!$ProxyExists)
{
$LinuxCredential = Get-VBRCredentials | where-object {$_.Description -eq "Proxy Linux Admin"}
}
$ProxyVM = Find-VBRViEntity -Name $ProxyVMEntity
if(!$ProxyExists)
{
try
{
Add-VBRLinux -Name $ProxyEntity -Description "Dynamic Veeam Proxy" -Credentials $LinuxCredential -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null
}
catch
{
Write-Host -ForegroundColor Red "ERROR: $_" -ErrorAction Stop
Get-VBRCredentials | where-object {$_.Description -eq "Proxy Linux Admin"} | Remove-VBRCredentials -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Stop-Transcript
Write-Error -Exception "Exiting due to issues adding Linux Proxy to Veeam Server" -ErrorAction Stop
}
Write-Host ":: Creating New Veeam Linux Proxy" -ForegroundColor Yellow
}
if(!$ProxyExists)
{
try
{
Add-VBRViLinuxProxy -Server $ProxyEntity -Description "Dynamic Veeam Proxy" -MaxTasks 4 -ProxyVM $ProxyVM -Force -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null
}
catch
{
Write-Host -ForegroundColor Red "ERROR: $_" -ErrorAction Stop
Get-VBRCredentials | where-object {$_.Description -eq "Proxy Linux Admin"} | Remove-VBRCredentials -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Stop-Transcript
Write-Error -Exception "Exiting due to issues Configuring Linux Proxy" -ErrorAction Stop
}
}
}
if ($ProxyExists) {Write-Host "--" $ProxyEntity "Exists in Configurtion" -ForegroundColor Green} else {Write-Host "--" $ProxyEntity "Configured" -ForegroundColor Green}
Write-Host
}
}
function AddVeeamNASProxy
{
$host.ui.RawUI.WindowTitle = "Adding Veeam NAS File Proxies"
$ProxyList = Get-Content proxy_ips.json | ConvertFrom-Json
$ProxyArray =@($ProxyList)
if(!$ProxyArray)
{
Write-Error -Exception "Exiting due to Terraform Proxy Deployment Issue" -ErrorAction Stop
}
if(!$Workgroup)
{
$WindowsUsername = $config.VBRDetails.Username
$WindowsPassword = $config.VBRDetails.Password
$WindowsDescription = "Windows Domain Account"
}
if($Workgroup)
{
$WindowsUsername = $config.VBRDetails.Username2
$WindowsPassword = $config.VBRDetails.Password2
$WindowsDescription = "Windows Server Account"
}
$ExistingWinCredential = Get-VBRCredentials -Name $WindowsUsername
if($Windows -and !$ExistingWinCredential)
{
Add-VBRCredentials -Type Windows -User $WindowsUsername -Password $WindowsPassword -Description $WindowsDescription | Out-Null
}
for ($i=0; $i -lt $ProxyCount; $i++)
{
$ProxyEntity = $ProxyArray.value[$i]
$ProxyName = $ProxyArray.value[$i]
#Return True or False if Proxy Exists in VBR
$NASProxyExists = Get-VBRNASProxyServer -Name $ProxyName
#Add Proxy to Backup & Replication
Write-Host ":: Adding NAS File Proxy Server to Backup & Replication" -ForegroundColor Yellow
if($Windows)
{
#Get and Set Windows Credential
if(!$NASProxyExists)
{
$WindowsCredential = Get-VBRCredentials | where-object {$_.Description -eq $WindowsDescription}
}
#Add Windows Server to VBR and Configure Proxy
if(!$NASProxyExists)
{
try
{
Add-VBRWinServer -Name $ProxyEntity -Description "Dynamic Veeam NAS File Proxy" -Credentials $WindowsCredential -ErrorAction Stop | Out-Null
}
Catch
{
Write-Host -ForegroundColor Red "ERROR: $_" -ErrorAction Stop
Get-VBRCredentials | where-object {$_.Description -eq $WindowsDescription} | Remove-VBRCredentials -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Stop-Transcript
Write-Error -Exception "Exiting due to issues adding NAS File Proxy to Veeam Server" -ErrorAction Stop
}
Write-Host ":: Creating New Veeam NAS File Proxy" -ForegroundColor Yellow
Add-VBRNASProxyServer -Server $ProxyEntity -ConcurrentTaskNumber 2 | Out-Null
}
}
if ($NASProxyExists) {Write-Host "--" $ProxyEntity "Exists in Configurtion" -ForegroundColor Green} else {Write-Host "--" $ProxyEntity "Configured" -ForegroundColor Green}
Write-Host
}
}
function RemoveVeeamProxy
{
$host.ui.RawUI.WindowTitle = "Removing Veeam Proxies"
$ProxyList = Get-Content proxy_ips.json | ConvertFrom-Json
$ProxyArray =@($ProxyList)
for ($i=0; $i -lt $ProxyCount; $i++)
{
$ProxyEntity = $ProxyArray.value[$i]
$ProxyName = $ProxyArray.value[$i]
#Return True or False if Proxy Exists in VBR
$ProxyExists = Get-VBRViProxy | Where-object {$_.Name -eq $ProxyName}
$NASProxyExists = Get-VBRNASProxyServer -Name $ProxyName
if(!$Workgroup)
{
$WindowsUsername = $config.VBRDetails.Username
$WindowsDescription = "Windows Domain Account"
}
if($Workgroup)
{
$WindowsUsername = $config.VBRDetails.Username2
$WindowsDescription = "Windows Server Account"
}
#Remove Proxy From Backup & Replication
if (!$NASProxy -and $ProxyExists)
{
Write-Host ":: Removing Proxy Server from Backup & Replication" -ForegroundColor Yellow
Get-VBRViProxy -Name $ProxyEntity | Remove-VBRViProxy -Confirm:$false -WarningAction SilentlyContinue | Out-Null
}
if ($NASProxy -and $NASProxyExists)
{
Write-Host ":: Removing NAS File Proxy from Backup & Replication" -ForegroundColor Yellow
Get-VBRNASProxyServer -Name $ProxyEntity | Remove-VBRNASProxyServer -Confirm:$false -WarningAction SilentlyContinue | Out-Null
}
Get-VBRServer -Type Windows -Name $ProxyEntity | Remove-VBRServer -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Get-VBRServer -Type Linux -Name $ProxyEntity | Remove-VBRServer -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Write-Host "--" $ProxyEntity "Removed" -ForegroundColor Red -BackgroundColor Black
Write-Host
}
if($Windows) { Get-VBRCredentials | where-object {$_.Description -eq $WindowsDescription} | Remove-VBRCredentials -Confirm:$false -WarningAction SilentlyContinue | Out-Null }
if($Ubuntu -or $CentOS) { Get-VBRCredentials | where-object {$_.Description -eq "Proxy Linux Admin"} | Remove-VBRCredentials -Confirm:$false -WarningAction SilentlyContinue | Out-Null }
}
function ScaleDownVeeamProxy
{
$host.ui.RawUI.WindowTitle = "Scaling Down Veeam Proxies"
$ProxyList = Get-Content proxy_ips.json | ConvertFrom-Json
$ProxyArray =@($ProxyList)
#Reverse Proxy Array to make sure we are removing oldest Proxy to match Terraform
[array]::Reverse($ProxyList.Value)
#Set Iterations of For Loop
$ProxyArrayFinish = ($ProxyArray.Value.Count - $ProxyCount)
for ($i=0; $i -lt $ProxyArrayFinish; $i++)
{
$ProxyEntity = $ProxyList.value[$i]
$ProxyName = $ProxyList.value[$i]
#Return True or False if Proxy Exists in VBR
$ProxyExists = Get-VBRViProxy | Where-Object {$_.Name -eq $ProxyName}
$NASProxyExists = Get-VBRNASProxyServer -Name $ProxyName
#Remove Proxy From Backup & Replication
if (!$NASProxy -and $ProxyExists)
{
Write-Host ":: Removing Proxy Server from Backup & Replication" -ForegroundColor Yellow
Get-VBRViProxy -Name $ProxyEntity | Remove-VBRViProxy -Confirm:$false -WarningAction SilentlyContinue | Out-Null
}
if ($NASProxy -and $NASProxyExists)
{
Write-Host ":: Removing NAS File Proxy from Backup & Replication" -ForegroundColor Yellow
Get-VBRNASProxyServer -Name $ProxyEntity | Remove-VBRNASProxyServer -Confirm:$false -WarningAction SilentlyContinue | Out-Null
}
Get-VBRServer -Type Windows -Name $ProxyEntity | Remove-VBRServer -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Get-VBRServer -Type Linux -Name $ProxyEntity | Remove-VBRServer -Confirm:$false -WarningAction SilentlyContinue | Out-Null
Write-Host "--" $ProxyEntity "Removed" -ForegroundColor Red -BackgroundColor Black
Write-Host
}
}
#Execute Functions
Start-Transcript logs\ProjectOtosukeru-Log.txt -Force
$StartTimeVB = Get-Date
ConnectVBRServer
Write-Host ""
Write-Host ":: - Connected to Backup & Replication Server - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeVB = Get-Date
$durationVB = [math]::Round((New-TimeSpan -Start $StartTimeVB -End $EndTimeVB).TotalMinutes,2)
Write-Host "Execution Time" $durationVB -ForegroundColor Green -BackgroundColor Black
Write-Host ""
$StartTimeLR = Get-Date
Write-Host ""
Write-Host ":: - Getting Job Details and Working out Dynamix Proxy Count - ::" -ForegroundColor Green -BackgroundColor Black
WorkOutProxyCount
WorkOutIfScaleDown
$EndTimeLR = Get-Date
$durationLR = [math]::Round((New-TimeSpan -Start $StartTimeLR -End $EndTimeLR).TotalMinutes,2)
Write-Host "Execution Time" $durationLR -ForegroundColor Green -BackgroundColor Black
Write-Host ""
if ($Windows -and !$Destroy -and !$ScaleDown){
#Run the code for Windows Proxies
if ($ProxyPerHost)
{
RenameFileForAntiAffinity
}
if ($DHCP)
{
RenameFileForDHCP
}
if ($Workgroup)
{
RenameFileForWorkGroup
}
$StartTimeTF = Get-Date
WindowsProxyBuild
Write-Host ""
Write-Host ":: - Windows Proxy VMs Deployed via Terraform - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeTF = Get-Date
$durationTF = [math]::Round((New-TimeSpan -Start $StartTimeTF -End $EndTimeTF).TotalMinutes,2)
Write-Host "Execution Time" $durationTF -ForegroundColor Green -BackgroundColor Black
Write-Host ""
if (!$NASProxy)
{
$StartTimeTF = Get-Date
AddVeeamProxy
Write-Host ""
Write-Host ":: - Windows Proxies Configured - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeTF = Get-Date
$durationTF = [math]::Round((New-TimeSpan -Start $StartTimeTF -End $EndTimeTF).TotalMinutes,2)
Write-Host "Execution Time" $durationTF -ForegroundColor Green -BackgroundColor Black
Write-Host ""
}
if ($NASProxy)
{
$StartTimeTF = Get-Date
AddVeeamNASProxy
Write-Host ""
Write-Host ":: - NAS File Proxies Configured - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeTF = Get-Date
$durationTF = [math]::Round((New-TimeSpan -Start $StartTimeTF -End $EndTimeTF).TotalMinutes,2)
Write-Host "Execution Time" $durationTF -ForegroundColor Green -BackgroundColor Black
Write-Host ""
}
if ($ProxyPerHost)
{
RenameFileBackForAntiAffinity
}
if ($DHCP)
{
RenameFileBackForDHCP
}
if ($Workgroup)
{
RenameFileBackForWorkGroup
}
}
if (($Ubuntu -or $CentOS) -and !$Destroy -and !$ScaleDown){
#Run the code for Linux Proxies
if ($ProxyPerHost)
{
RenameFileForAntiAffinity
}
if ($DHCP)
{
RenameFileForDHCP
}
if ($Workgroup)
{
RenameFileForWorkGroup
}
$StartTimeTF = Get-Date
LinuxProxyBuild
Write-Host ""
Write-Host ":: - Windows Proxies Deployed via Terraform - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeTF = Get-Date
$durationTF = [math]::Round((New-TimeSpan -Start $StartTimeTF -End $EndTimeTF).TotalMinutes,2)
Write-Host "Execution Time" $durationTF -ForegroundColor Green -BackgroundColor Black
Write-Host ""
$StartTimeTF = Get-Date
AddVeeamProxy
Write-Host ""
Write-Host ":: - Windows Proxies Configured - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeTF = Get-Date
$durationTF = [math]::Round((New-TimeSpan -Start $StartTimeTF -End $EndTimeTF).TotalMinutes,2)
Write-Host "Execution Time" $durationTF -ForegroundColor Green -BackgroundColor Black
Write-Host ""
if ($ProxyPerHost)
{
RenameFileBackForAntiAffinity
}
if ($DHCP)
{
RenameFileBackForDHCP
}
if ($Workgroup)
{
RenameFileBackForWorkGroup
}
}
if ($Destroy -and !$ScaleDown){
#Run the code to Remove Proxies
$StartTimeCL = Get-Date
if ($ProxyPerHost)
{
RenameFileForAntiAffinity
}
if ($DHCP)
{
RenameFileForDHCP
}
if ($Workgroup)
{
RenameFileForWorkGroup
}
RemoveVeeamProxy
Write-Host ""
Write-Host ":: - Clearing Backup & Replication Server Configuration - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeCL = Get-Date
$durationCL = [math]::Round((New-TimeSpan -Start $StartTimeCL -End $EndTimeCL).TotalMinutes,2)
Write-Host "Execution Time" $durationCL -ForegroundColor Green -BackgroundColor Black
Write-Host ""
$StartTimeCL = Get-Date
ProxyDestroy
Write-Host ""
Write-Host ":: - Destroying Proxies with Terraform - ::" -ForegroundColor Green -BackgroundColor Black
$EndTimeCL = Get-Date
$durationCL = [math]::Round((New-TimeSpan -Start $StartTimeCL -End $EndTimeCL).TotalMinutes,2)
Write-Host "Execution Time" $durationCL -ForegroundColor Green -BackgroundColor Black
Write-Host ""
if ($ProxyPerHost)
{
RenameFileBackForAntiAffinity
}
if ($DHCP)
{
RenameFileBackForDHCP
}
if ($Workgroup)
{
RenameFileBackForWorkGroup
}
}
if ($ScaleDown){
#Run the code to Scale Down Proxies
$StartTimeCL = Get-Date
if ($ProxyPerHost)
{
RenameFileForAntiAffinity
}
if ($DHCP)
{
RenameFileForDHCP
}
if ($Workgroup)
{
RenameFileForWorkGroup
}
Write-Host ""
Write-Host ":: - Scaling Down Proxies from Backup & Replication Server Configuration - ::" -ForegroundColor Green -BackgroundColor Black
ScaleDownVeeamProxy
$EndTimeCL = Get-Date
$durationCL = [math]::Round((New-TimeSpan -Start $StartTimeCL -End $EndTimeCL).TotalMinutes,2)
Write-Host "Execution Time" $durationCL -ForegroundColor Green -BackgroundColor Black
Write-Host ""
$StartTimeCL = Get-Date
Write-Host ""
Write-Host ":: - Scaling Down Proxies with Terraform - ::" -ForegroundColor Green -BackgroundColor Black
if($Windows)
{
WindowsProxyBuild
}
if($Ubuntu -or $CentOS)
{
LinuxProxyBuild
}
$EndTimeCL = Get-Date
$durationCL = [math]::Round((New-TimeSpan -Start $StartTimeCL -End $EndTimeCL).TotalMinutes,2)
Write-Host "Execution Time" $durationCL -ForegroundColor Green -BackgroundColor Black
Write-Host ""
if ($ProxyPerHost)
{
RenameFileBackForAntiAffinity
}
if ($DHCP)
{
RenameFileBackForDHCP
}
if ($Workgroup)
{
RenameFileBackForWorkGroup
}
}
Stop-Transcript
$EndTime = Get-Date
$duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)
$host.ui.RawUI.WindowTitle = "AUTOMATION AND ORCHESTRATION COMPLETE"
Write-Host "Total Execution Time" $duration -ForegroundColor Green -BackgroundColor Black
Write-Host