-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-zerto-completed-vpgs.ps1
786 lines (699 loc) · 77 KB
/
get-zerto-completed-vpgs.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
<#
.SYNOPSIS
This script figures out which initializations have completed in a zvm based on a source report.
.DESCRIPTION
The script loads the source report which contains a protection status for replicated VMs, then retrieves current status from a ZVM and compares both. For all VMs which have changed from Initializing to MeetingSLA, a report is created and sent to a list of email recipients. The script then archives the source report and renames the updated report as source. If a source report does not exist, the script will simply create one and do nothing else.
.PARAMETER help
Displays a help message (seriously, what did you think this was?)
.PARAMETER history
Displays a release history for this script (provided the editors were smart enough to document this...)
.PARAMETER log
Specifies that you want the output messages to be written in a log file as well as on the screen.
.PARAMETER debugme
Turns off SilentlyContinue on unexpected error messages.
.PARAMETER zvm
ZVM fully qualified domain name or IP address.
.PARAMETER zvmport
TCP port number used to connect to the ZVM (default if not specified is 9669).
.PARAMETER username
Username used to connect to the ZVM.
.PARAMETER password
Password used to connect to the ZVM.
.PARAMETER authInfo
Pass on an authentication hash instead of a username and password.
.PARAMETER printonly
Specify this parameter if you only want to display results on the screen as opposed to sending an email.
.EXAMPLE
Connect to a ZVM of your choice:
PS> .\get-zerto-completed-vpgs.ps1 -zvm zvm01.local -username admin -password admin
.LINK
http://www.nutanix.com/services
.NOTES
Author: Stephane Bourdeaud ([email protected])
Revision: March 2nd 2016
#>
######################################
## parameters and initial setup ##
######################################
#let's start with some command line parsing
Param
(
#[parameter(valuefrompipeline = $true, mandatory = $true)] [PSObject]$myParam1,
[parameter(mandatory = $false)] [switch]$help,
[parameter(mandatory = $false)] [switch]$history,
[parameter(mandatory = $false)] [switch]$log,
[parameter(mandatory = $false)] [switch]$debugme,
[parameter(mandatory = $false)] [string]$zvm,
[parameter(mandatory = $false)] [string]$zvmport,
[parameter(mandatory = $false)] [string]$username,
[parameter(mandatory = $false)] [string]$password,
[parameter(mandatory = $false)] [string]$authInfo,
[parameter(mandatory = $false)] [switch]$printonly
)
# get rid of annoying error messages
if (!$debugme) {$ErrorActionPreference = "SilentlyContinue"}
# Allow the use of self-signed SSL certificates.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
########################
## main functions ##
########################
#this function is used to output log data
Function OutputLogData
{
#input: log category, log message
#output: text to standard output
<#
.SYNOPSIS
Outputs messages to the screen and/or log file.
.DESCRIPTION
This function is used to produce screen and log output which is categorized, time stamped and color coded.
.NOTES
Author: Stephane Bourdeaud
.PARAMETER myCategory
This the category of message being outputed. If you want color coding, use either "INFO", "WARNING", "ERROR" or "SUM".
.PARAMETER myMessage
This is the actual message you want to display.
.EXAMPLE
PS> OutputLogData -mycategory "ERROR" -mymessage "You must specify a cluster name!"
#>
param
(
[string] $category,
[string] $message
)
begin
{
$myvarDate = get-date
$myvarFgColor = "Gray"
switch ($category)
{
"INFO" {$myvarFgColor = "Green"}
"WARNING" {$myvarFgColor = "Yellow"}
"ERROR" {$myvarFgColor = "Red"}
"SUM" {$myvarFgColor = "Magenta"}
}
}
process
{
Write-Host -ForegroundColor $myvarFgColor "$myvarDate [$category] $message"
if ($log) {Write-Output "$myvarDate [$category] $message" >>$myvarOutputLogFile}
}
end
{
Remove-variable category
Remove-variable message
Remove-variable myvarDate
Remove-variable myvarFgColor
}
}#end function OutputLogData
##Function Definitions
#Get a site identifier by invoking Zerto APIs, given a Zerto API session and a site name:
function ZertogetSiteIdentifierByName ($zertoSessionHeader, $siteName, $BASEURL){
$url = $BASEURL + "virtualizationsites"
$response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/xml"
ForEach ($site in $response.ArrayOfVirtualizationSiteApi.VirtualizationSiteApi) {
if ($site.VirtualizationSiteName -eq $siteName){
return $site.SiteIdentifier
}
}
}
#Get a storage identifier by invoking Zerto APIs, given a Zerto Virtual Replication API session and a storage name:
function ZertogetDatastoreIdentifierByName ($zertoSessionHeader, $siteIdentfier, $datastoreName, $BASEURL){
$url = $BASEURL + "virtualizationsites/"+$siteIdentfier + "/datastores"
$response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/xml"
ForEach ($datastore in $response.ArrayOfDatastoreNativeApi.DatastoreNativeApi) {
if ($datastore.DatastoreName -eq $datastoreName){
return $datastore.DatastoreIdentifier
}
}
}
#Get unprotected VM identifiers by invoking Zerto APIs, given a Zerto API session, a site identifier, and a list of VMs to add to the VPG:
function ZertogetUnprotectedVMsIdentifiers($zertoSessionHeader, $siteIdentfier, $VMNames, $BASEURL){
$url = $BASEURL + "virtualizationsites/"+$siteIdentfier + "/vms"
$unprotectedVMsIdentifiers = @()
$response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/xml"
ForEach ($vm in $response.ArrayOfVmNativeApi.VmNativeApi) {
if ($VMNames.IndexOf($vm.VmName) -gt -1){
$unprotectedVMsIdentifiers+=($vm.VmIdentifier)
}
}
return $unprotectedVMsIdentifiers
}
#Authenticate with Zerto APIs: create a Zerto API session and return it, to be used in other APIs
function ZertogetZertoXSession ($myvarZvm, $myvarZvmPort, $myvarUsername, $myvarPassword){
#Authenticate with Zerto APIs:
$xZertoSessionURI = "https://" + $myvarZvm + ":"+$myvarZvmPort+"/v1/session/Add"
$authInfo = ("{0}:{1}" -f $myvarUsername,$myvarPassword)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}
$body = '{"AuthenticationMethod": "1"}'
$contentType = "application/json"
$xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers -Method POST -Body $body -ContentType $contentType
#Extract x-zerto-session from the response and add it to the actual API:
$xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
return $xZertoSession
}
#Authenticate with Zerto APIs: create a Zerto API session and return it, to be used in other APIs
function ZertogetZertoXSessionHash ($myvarZvm, $myvarZvmPort, $myvarAuthInfo){
#Authenticate with Zerto APIs:
$xZertoSessionURI = "https://" + $myvarZvm + ":"+$myvarZvmPort+"/v1/session/Add"
$myvarAuthInfo = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($myvarAuthInfo))
$myvarAuthInfo = [System.Text.Encoding]::UTF8.GetBytes($myvarAuthInfo)
$myvarAuthInfo = [System.Convert]::ToBase64String($myvarAuthInfo)
$headers = @{Authorization=("Basic {0}" -f $myvarAuthInfo)}
$body = '{"AuthenticationMethod": "1"}'
$contentType = "application/json"
$xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers -Method POST -Body $body -ContentType $contentType
#Extract x-zerto-session from the response and add it to the actual API:
$xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
return $xZertoSession
}
#Build VM elements to be added to the VPGs API, based on a list of VM identifiers
function ZertobuildVMsElement ($VMs, $BASEURL) {
$response = "<VmsIdentifiers>"
ForEach ($vm in $VMs) {
$response+="<string xmlns="+'"http://schemas.microsoft.com/2003/10/Serialization/Arrays"'+">"+$vm+"</string>"
}
$response += "</VmsIdentifiers>"
return $response
}
#Get a list of VPGs and their status by invoking Zerto APIs, given a Zerto API session:
function ZertogetVPGs ($zertoSessionHeader, $BASEURL){
$url = $BASEURL + "vpgs"
$response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/json"
return $response
}
#Get a list of protected VMs and their status by invoking Zerto APIs, given a Zerto API session:
function ZertogetVMs ($zertoSessionHeader, $BASEURL){
$url = $BASEURL + "vms"
$response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/json"
return $response
}
#Get a list of VRAs and their status by invoking Zerto APIs, given a Zerto API session:
function ZertogetVRAs ($zertoSessionHeader, $BASEURL){
$url = $BASEURL + "vras"
$response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/json"
return $response
}
#########################
## main processing ##
#########################
#check if we need to display help and/or history
$HistoryText = @'
Maintenance Log
Date By Updates (newest updates at the top)
---------- ---- ---------------------------------------------------------------
02/10/2016 sb Initial release.
03/02/2016 sb Added printonly switch and revised logic for comparison.
################################################################################
'@
$myvarScriptName = ".\get-zerto-completed-vpgs.ps1"
if ($help) {get-help $myvarScriptName; exit}
if ($History) {$HistoryText; exit}
#let's load the Nutanix cmdlets
#if ((Get-PSSnapin -Name NutanixCmdletsPSSnapin -ErrorAction SilentlyContinue) -eq $null)#is it already there?
#{
# Add-PSSnapin NutanixCmdletsPSSnapin #no? let's add it
# if (!$?) #have we been able to add it successfully?
# {
# OutputLogData -category "ERROR" -message "Unable to load the Nutanix snapin. Please make sure the Nutanix Cmdlets are installed on this server."
# return
# }
#}
#initialize variables
#misc variables
$myvarElapsedTime = [System.Diagnostics.Stopwatch]::StartNew() #used to store script begin timestamp
$myvarvCenterServers = @() #used to store the list of all the vCenter servers we must connect to
$myvarOutputLogFile = (Get-Date -UFormat "%Y_%m_%d_%H_%M_")
$myvarOutputLogFile += "OutputLog.log"
############################################################################
# command line arguments initialization
############################################################################
#let's initialize parameters if they haven't been specified
[System.Collections.ArrayList]$myvarResults = New-Object System.Collections.ArrayList($null) #used for storing all protected vm entries. This is what will be exported to csv
$myvarSourceReport = "zerto-report-source-vpgs.csv"
$myvarUpdatedReport = "zerto-report-updated-vpgs.csv"
$myvarReplication = @{}
$myvarReplication.CompletedReplications = @()
$myvarReplication.Replications = @()
$myvarReplication.NewReplications = @()
$myvarReplication.NewCompletedReplications = @()
# variables that you need to customize
$myvarEmailFrom = "[email protected]"
$myvarSmtpServer = "koltsmtpr.ccp.edp.local"
if (!$zvmport) {$zvmport = "9669"}
if (!$zvm) {$zvm = read-host "Enter the FQDN or IP address of the ZVM"} #prompt for ZVM
if (!$username -and !$authInfo) {$username = read-host "Enter the ZVM username"} #prompt for username
if (!$password -and !$authInfo) {$password = read-host "Enter the ZVM password"} #prompt for username
$myvarBASEURL = "https://" + $zvm + ":"+$zvmport+"/v1/" #base URL for all APIs
################################
## Main execution here ##
################################
#Initialize REST API session with the Zerto ZVM
OutputLogData -category "INFO" -message "Connecting to ZVM $zvm..."
if ($authInfo)
{
$myvarxZertoSession = ZertogetZertoXSessionHash $zvm $zvmport $authInfo
}
else
{
$myvarxZertoSession = ZertogetZertoXSession $zvm $zvmport $username $password
}
$myvarzertoSessionHeader = @{"x-zerto-session"=$myvarxZertoSession}
#check to see if we have a valid session
if ($myvarxZertoSession)
{#do we have a valid zerto session?
if (Test-Path ".\zerto-report-source-vpgs.csv") #check if source report exists
{ #source report exists, so let's continue processing
#load the source report in a reference variable
$myvarSourceList = @(Import-Csv -Path ".\zerto-report-source-vpgs.csv")
#retrieve updated report
#Iterate protected VMs with JSON:
OutputLogData -category "INFO" -message "Retrieving list of protected VMs from $zvm for the source report..."
$myvarVMListJSON = ZertogetVMs $myvarzertoSessionHeader $myvarBASEURL
#process each returned entry/protected vm
if ($myvarVMListJSON)
{#we have protected vms, let's process
OutputLogData -category "INFO" -message "Processing retrieved list of protected VMs for the source report..."
foreach ($myvarVM in $myvarVMListJSON)
{
#Figure out the status message
$myvarStatus = switch($myvarVM.Status)
{
0 {"Initializing"} #– The VPG is being initialized. This includes when a VPG is created, and during the initial sync between sites.
1 {"MeetingSLA"} #– The VPG is meeting the SLA specification.
2 {"NotMeetingSLA"} #– The VPG is not meeting the SLA specification for both the journal history and RPO SLA settings.
3 {"HistoryNotMeetingSLA"} #– The VPG is not meeting the SLA specification for the journal history.
4 {"RpoNotMeetingSLA"} #– The VPG is not meeting the SLA specification for the RPO SLA setting.
5 {"FailingOver"} #– The VPG is in a Failover operation.
6 {"Moving"} #– The VPG is in a Move operation.
7 {"Deleting"} #– The VPG is being deleted.
8 {"Recovered"} #– The VPG has been recovered.
}
#Figure out the sub-status message
$myvarSubStatus = switch($myvarVM.SubStatus)
{
0 {"None"}
1 {"InitialSync"}
2 {"Creating"}
3 {"VolumeInitialSync"}
4 {"Sync"}
5 {"RecoveryPossible"}
6 {"DeltaSync"}
7 {"NeedsConfiguration"}
8 {"Error"}
9 {"EmptyProtectionGroup"}
10 {"DisconnectedFromPeerNoRecoveryPoints"}
11 {"FullSync"}
12 {"VolumeDeltaSync"}
13 {"VolumeFullSync"}
14 {"FailingOverCommitting"}
15 {"FailingOverBeforeCommit"}
16 {"FailingOverRollingBack"}
17 {"Promoting"}
18 {"MovingCommitting"}
19 {"MovingBeforeCommit"}
20 {"MovingRollingBack"}
21 {"Deleting"}
22 {"PendingRemove"}
23 {"BitmapSync"}
24 {"DisconnectedFromPeer"}
25 {"ReplicationPausedUserInitiated"}
26 {"ReplicationPausedSystemInitiated"}
27 {"RecoveryStorageProfileError"}
28 {"Backup"}
29 {"RollingBack"}
30 {"RecoveryStorageError"}
31 {"JournalStorageError"}
32 {"VmNotProtectedError"}
}
#populate the hash for this protected vm
$myvarVMInfo = @{"VmName" = $myvarVM.VmName; `
"VpgName" = $myvarVM.VpgName; `
"ProvisionedStorageInMB" = $myvarVM.ProvisionedStorageInMB; `
"UsedStorageInMB" = $myvarVM.UsedStorageInMB; `
"Status" = $myvarStatus; `
"SubStatus" = $myvarSubStatus; `
"ActualRPO" = $myvarVM.ActualRPO; `
"ThroughputInMB" = $myvarVM.ThroughputInMB}
#populate the array for our final report
$myvarResults.Add((New-Object PSObject -Property $myvarVMInfo)) | Out-Null
}#end foreach vm
#export the results to csv
OutputLogData -category "INFO" -message "Creating updated report as $myvarUpdatedReport..."
$myvarResults | export-csv -NoTypeInformation $myvarUpdatedReport
$myvarUpdatedList = @(Import-Csv -Path ".\zerto-report-updated-vpgs.csv")
}#endif we have protected vms
#compare source with update
#find out which vms are newly protected (they are not in the source list) and have already completed
OutputLogData -category "INFO" -message "Processing results..."
foreach ($myvarVMsource in $myvarSourceList) #process each entry in the source list
{
$myvarVMSourceExists = $false #we use this variable to find out if the entry in source is also in the updated list
$myvarVMSourceExistsReplicating = $false #we use this variable to keep track of status changes for an entry that is in both lists
$myvarVMSourceExistsCompleted = $false #we use this variable to keep track of status changes for an entry that is in both lists
foreach ($myvarVMupdated in $myvarUpdatedList) #compare with each entry in the updated list
{
if ($myvarVMsource.VmName -eq $myvarVMupdated.VmName) #we compare the VM names
{
$myvarVMSourceExists = $true #we found a match so let's make sure we keep track of the fact that this vm is both in source and updated
if ($myvarVMupdated.Status -eq "Initializing") #vm is in both lists and is still replicating
{
$myvarVMSourceExistsReplicating = $true #mark it as such
$myvarReplication.Replications += $myvarVMSource
}#end if the vm is still replicating
else #vm is in both lists and it's not replicating, so let's check if it has completed
{
if ((($myvarVMupdated.Status -eq "MeetingSLA") -or ($myvarVMupdated.Status -eq "NotMeetingSLA")) -and ($myvarVMsource.Status -eq "Initializing")) #vm was initializing and has finished replicating
{
$myvarVMSourceExistsCompleted = $true #mark it as such
$myvarReplication.CompletedReplications += $myvarVMUpdated
}#end if is replication done?
}#end else
}#end if vm is in both lists
}#end process each updated entry comparison
}#end process each source entry
#now let's find out which vms are in updated AND not in source and what status they have
foreach ($myvarVMupdated in $myvarUpdatedList) #process each entry in the updated list
{
$myvarVMSourceNew = $true #assume the entry is new until proven otherwise
$myvarVMSourceNewReplicating = $false #we use this variable to keep track of status changes for an entry that is only in updated
$myvarVMSourceNewCompleted = $false #we use this variable to keep track of status changes for an entry that is oinly in updated
foreach ($myvarVMsource in $myvarSourceList) #compare with each entry in source
{
if ($myvarVMsource.VmName -eq $myvarVMUpdated.VmName) #we compare the VM names
{
$myvarVMSourceNew = $false #the vm is in both list, so we'll ignore it as it has been processed elsewhere (in the previous section)
}#end if vm is in both lists
}#end foreach entry in source comparison
if ($myvarVMsourceNew) #the vm is only in updated
{
if ($myvarVMUpdated.Status -eq "Initializing") #...and it is still replicating
{
$myvarVMSourceNewReplicating = $true
$myvarReplication.NewReplications += $myvarVMUpdated
}#endif the vm is still replicating
else
{
if (($myvarVMupdated.Status -eq "MeetingSLA") -or ($myvarVMupdated.Status -eq "NotMeetingSLA")) #it's done replicating
{
$myvarVMSourceNewCompleted = $true
$myvarReplication.NewCompletedReplications += $myvarVMUpdated
}#endif the vm is done replicating
}#end else
}#endif the vm is new
}#end foreach entry in updated
$myvarReportTimeStamp = (Get-Date -UFormat "%Y_%m_%d_%H_%M")
if (!$printonly)
{
#send mail to recipients if there are any completed replications
if (($myvarReplication.CompletedReplications) -or ($myvarReplication.NewCompletedReplications) -or ($myvarReplication.NewReplications))
{
$myvarEmailRecipients = "[email protected]","[email protected]","[email protected]","[email protected]"
#send that email
OutputLogData -category "INFO" -message "Building the email content..."
$myvarEmailSubject = "Zerto Replication Report " + $myvarReportTimeStamp
#building tables
$myvarTableCompletedReplications = New-Object system.Data.DataTable "CompletedReplications"
$myvarTableReplications = New-Object system.Data.DataTable "Replications"
$myvarTableNewReplications = New-Object system.Data.DataTable "NewReplications"
$myvarTableNewCompletedReplications = New-Object system.Data.DataTable "NewCompletedReplications"
$myvarcol1 = New-Object system.Data.DataColumn VM,([string])
$myvarcol2 = New-Object system.Data.DataColumn Status,([string])
$myvarcol3 = New-Object system.Data.DataColumn VPG,([string])
$myvarcol4 = New-Object system.Data.DataColumn ThroughputMB,([string])
$myvarcol5 = New-Object system.Data.DataColumn UsedStorageMB,([string])
$myvarTableCompletedReplications.columns.add($myvarcol1)
$myvarTableCompletedReplications.columns.add($myvarcol2)
$myvarTableCompletedReplications.columns.add($myvarcol3)
$myvarTableCompletedReplications.columns.add($myvarcol4)
$myvarTableCompletedReplications.columns.add($myvarcol5)
$myvarcol1 = New-Object system.Data.DataColumn VM,([string])
$myvarcol2 = New-Object system.Data.DataColumn Status,([string])
$myvarcol3 = New-Object system.Data.DataColumn VPG,([string])
$myvarcol4 = New-Object system.Data.DataColumn ThroughputMB,([string])
$myvarcol5 = New-Object system.Data.DataColumn UsedStorageMB,([string])
$myvarTableReplications.columns.add($myvarcol1)
$myvarTableReplications.columns.add($myvarcol2)
$myvarTableReplications.columns.add($myvarcol3)
$myvarTableReplications.columns.add($myvarcol4)
$myvarTableReplications.columns.add($myvarcol5)
$myvarcol1 = New-Object system.Data.DataColumn VM,([string])
$myvarcol2 = New-Object system.Data.DataColumn Status,([string])
$myvarcol3 = New-Object system.Data.DataColumn VPG,([string])
$myvarcol4 = New-Object system.Data.DataColumn ThroughputMB,([string])
$myvarcol5 = New-Object system.Data.DataColumn UsedStorageMB,([string])
$myvarTableNewReplications.columns.add($myvarcol1)
$myvarTableNewReplications.columns.add($myvarcol2)
$myvarTableNewReplications.columns.add($myvarcol3)
$myvarTableNewReplications.columns.add($myvarcol4)
$myvarTableNewReplications.columns.add($myvarcol5)
$myvarcol1 = New-Object system.Data.DataColumn VM,([string])
$myvarcol2 = New-Object system.Data.DataColumn Status,([string])
$myvarcol3 = New-Object system.Data.DataColumn VPG,([string])
$myvarcol4 = New-Object system.Data.DataColumn ThroughputMB,([string])
$myvarcol5 = New-Object system.Data.DataColumn UsedStorageMB,([string])
$myvarTableNewCompletedReplications.columns.add($myvarcol1)
$myvarTableNewCompletedReplications.columns.add($myvarcol2)
$myvarTableNewCompletedReplications.columns.add($myvarcol3)
$myvarTableNewCompletedReplications.columns.add($myvarcol4)
$myvarTableNewCompletedReplications.columns.add($myvarcol5)
foreach ($myvarVM in $myvarReplication.CompletedReplications)
{
$myvarrow = $myvarTableCompletedReplications.NewRow()
$myvarrow.VM = $myvarVM.VmName
$myvarrow.Status = $myvarVM.Status
$myvarrow.VPG = $myvarVM.VpgName
$myvarrow.ThroughputMB = $myvarVM.ThroughputInMB
$myvarrow.UsedStorageMB = $myvarVM.UsedStorageInMB
$myvarTableCompletedReplications.Rows.Add($myvarrow)
}
foreach ($myvarVM in $myvarReplication.NewCompletedReplications)
{
$myvarrow = $myvarTableNewCompletedReplications.NewRow()
$myvarrow.VM = $myvarVM.VmName
$myvarrow.Status = $myvarVM.Status
$myvarrow.VPG = $myvarVM.VpgName
$myvarrow.ThroughputMB = $myvarVM.ThroughputInMB
$myvarrow.UsedStorageMB = $myvarVM.UsedStorageInMB
$myvarTableNewCompletedReplications.Rows.Add($myvarrow)
}
foreach ($myvarVM in $myvarReplication.Replications)
{
$myvarrow = $myvarTableReplications.NewRow()
$myvarrow.VM = $myvarVM.VmName
$myvarrow.Status = $myvarVM.Status
$myvarrow.VPG = $myvarVM.VpgName
$myvarrow.ThroughputMB = $myvarVM.ThroughputInMB
$myvarrow.UsedStorageMB = $myvarVM.UsedStorageInMB
$myvarTableReplications.Rows.Add($myvarrow)
}
foreach ($myvarVM in $myvarReplication.NewReplications)
{
$myvarrow = $myvarTableNewReplications.NewRow()
$myvarrow.VM = $myvarVM.VmName
$myvarrow.Status = $myvarVM.Status
$myvarrow.VPG = $myvarVM.VpgName
$myvarrow.ThroughputMB = $myvarVM.ThroughputInMB
$myvarrow.UsedStorageMB = $myvarVM.UsedStorageInMB
$myvarTableNewReplications.Rows.Add($myvarrow)
}
if ($myvarReplication.CompletedReplications)
{
$myvarhtml = "The following VMs have finished replicating:<br /><br />"
$myvarhtml += "<table><tr><td>VM</td><td>Status</td><td>VPG</td><td>ThroughputMB</td><td>UsedStorageMB</td></tr>"
foreach ($myvarrow in $myvarTableCompletedReplications.Rows)
{
$myvarhtml += "<tr><td>" + $myvarrow[0] + "</td><td>" + $myvarrow[1] + "</td><td>" + $myvarrow[2] + "</td><td>" + $myvarrow[3] + "</td><td>" + $myvarrow[4] + "</td></tr>"
}
$myvarhtml += "</table>"
$myvarEmailBody += "<br /><br />" + $myvarhtml
}
if ($myvarReplication.NewCompletedReplications)
{
$myvarhtml = "The following VMs are new and have finished replicating:<br /><br />"
$myvarhtml += "<table><tr><td>VM</td><td>Status</td><td>VPG</td><td>ThroughputMB</td><td>UsedStorageMB</td></tr>"
foreach ($myvarrow in $myvarTableNewCompletedReplications.Rows)
{
$myvarhtml += "<tr><td>" + $myvarrow[0] + "</td><td>" + $myvarrow[1] + "</td><td>" + $myvarrow[2] + "</td><td>" + $myvarrow[3] + "</td><td>" + $myvarrow[4] + "</td></tr>"
}
$myvarhtml += "</table>"
$myvarEmailBody += "<br /><br />" + $myvarhtml
}
if ($myvarReplication.NewReplications)
{
$myvarhtml = "The following VMs are new and still replicating:<br /><br />"
$myvarhtml += "<table><tr><td>VM</td><td>Status</td><td>VPG</td><td>ThroughputMB</td><td>UsedStorageMB</td></tr>"
foreach ($myvarrow in $myvarTableNewReplications.Rows)
{
$myvarhtml += "<tr><td>" + $myvarrow[0] + "</td><td>" + $myvarrow[1] + "</td><td>" + $myvarrow[2] + "</td><td>" + $myvarrow[3] + "</td><td>" + $myvarrow[4] + "</td></tr>"
}
$myvarhtml += "</table>"
$myvarEmailBody += "<br /><br />" + $myvarhtml
}
if ($myvarReplication.Replications)
{
$myvarhtml = "The following VMs are still replicating:<br /><br />"
$myvarhtml += "<table><tr><td>VM</td><td>Status</td><td>VPG</td><td>ThroughputMB</td><td>UsedStorageMB</td></tr>"
foreach ($myvarrow in $myvarTableReplications.Rows)
{
$myvarhtml += "<tr><td>" + $myvarrow[0] + "</td><td>" + $myvarrow[1] + "</td><td>" + $myvarrow[2] + "</td><td>" + $myvarrow[3] + "</td><td>" + $myvarrow[4] + "</td></tr>"
}
$myvarhtml += "</table>"
$myvarEmailBody += "<br /><br />" + $myvarhtml
}
OutputLogData -category "INFO" -message "Sending the email..."
Send-MailMessage -SmtpServer $myvarSmtpServer -From $myvarEmailFrom -To $myvarEmailRecipients -Subject $myvarEmailSubject -Body $myvarEmailBody -bodyashtml
#archive source report
$myvarArchivedReportName = "zerto-report-source-vpgs-" + $myvarReportTimeStamp + ".csv"
Rename-Item zerto-report-source-vpgs.csv $myvarArchivedReportName
OutputLogData -category "INFO" -message "Saved the source report to $myvarArchivedReportName"
#rename updated report
Rename-Item zerto-report-updated-vpgs.csv zerto-report-source-vpgs.csv
OutputLogData -category "INFO" -message "Saved the updated report as the new source report..."
}
else
{
OutputLogData -category "INFO" -message "There are no completed replications so I have nothing to do!"
}
} #end if not printonly
else
{
#print only the output (as opposed to sending an email)
if (($myvarReplication.CompletedReplications) -or ($myvarReplication.NewCompletedReplications) -or ($myvarReplication.NewReplications))
{
if ($myvarReplication.CompletedReplications)
{
OutputLogData -category "INFO" -message "The following VMs have been replicated since the last check:"
$myvarReplication.CompletedReplications | ft -autosize
}
if ($myvarReplication.NewCompletedReplications)
{
OutputLogData -category "INFO" -message "The following VMs are new and have been replicated since the last check:"
$myvarReplication.NewCompletedReplications | ft -autosize
}
if ($myvarReplication.NewReplications)
{
OutputLogData -category "INFO" -message "The following VMs are new and still replicating:"
$myvarReplication.NewReplications | ft -autosize
}
if ($myvarReplication.Replications)
{
OutputLogData -category "INFO" -message "The following VMs are still replicating:"
$myvarReplication.Replications | ft -autosize
}
#archive source report
$myvarArchivedReportName = "zerto-report-source-vpgs-" + $myvarReportTimeStamp + ".csv"
Rename-Item zerto-report-source-vpgs.csv $myvarArchivedReportName
OutputLogData -category "INFO" -message "Saved the source report to $myvarArchivedReportName"
#rename updated report
Rename-Item zerto-report-updated-vpgs.csv zerto-report-source-vpgs.csv
OutputLogData -category "INFO" -message "Saved the updated report as the new source report..."
}
else
{
OutputLogData -category "INFO" -message "There are no completed replications so I have nothing to do!"
}
} #end if printonly
} #endif source report exists
else #there is no source report, so let's create one
{
#Iterate protected VMs with JSON:
OutputLogData -category "INFO" -message "Retrieving list of protected VMs from $zvm for the source report..."
$myvarVMListJSON = ZertogetVMs $myvarzertoSessionHeader $myvarBASEURL
#process each returned entry/protected vm
if ($myvarVMListJSON) {
OutputLogData -category "INFO" -message "Processing retrieved list of protected VMs for the source report..."
foreach ($myvarVM in $myvarVMListJSON){
#Figure out the status message
$myvarStatus = switch($myvarVM.Status) {
0 {"Initializing"} #– The VPG is being initialized. This includes when a VPG is created, and during the initial sync between sites.
1 {"MeetingSLA"} #– The VPG is meeting the SLA specification.
2 {"NotMeetingSLA"} #– The VPG is not meeting the SLA specification for both the journal history and RPO SLA settings.
3 {"HistoryNotMeetingSLA"} #– The VPG is not meeting the SLA specification for the journal history.
4 {"RpoNotMeetingSLA"} #– The VPG is not meeting the SLA specification for the RPO SLA setting.
5 {"FailingOver"} #– The VPG is in a Failover operation.
6 {"Moving"} #– The VPG is in a Move operation.
7 {"Deleting"} #– The VPG is being deleted.
8 {"Recovered"} #– The VPG has been recovered.
}
#Figure out the sub-status message
$myvarSubStatus = switch($myvarVM.SubStatus) {
0 {"None"}
1 {"InitialSync"}
2 {"Creating"}
3 {"VolumeInitialSync"}
4 {"Sync"}
5 {"RecoveryPossible"}
6 {"DeltaSync"}
7 {"NeedsConfiguration"}
8 {"Error"}
9 {"EmptyProtectionGroup"}
10 {"DisconnectedFromPeerNoRecoveryPoints"}
11 {"FullSync"}
12 {"VolumeDeltaSync"}
13 {"VolumeFullSync"}
14 {"FailingOverCommitting"}
15 {"FailingOverBeforeCommit"}
16 {"FailingOverRollingBack"}
17 {"Promoting"}
18 {"MovingCommitting"}
19 {"MovingBeforeCommit"}
20 {"MovingRollingBack"}
21 {"Deleting"}
22 {"PendingRemove"}
23 {"BitmapSync"}
24 {"DisconnectedFromPeer"}
25 {"ReplicationPausedUserInitiated"}
26 {"ReplicationPausedSystemInitiated"}
27 {"RecoveryStorageProfileError"}
28 {"Backup"}
29 {"RollingBack"}
30 {"RecoveryStorageError"}
31 {"JournalStorageError"}
32 {"VmNotProtectedError"}
}
#populate the hash for this protected vm
$myvarVMInfo = @{"VmName" = $myvarVM.VmName; `
"VpgName" = $myvarVM.VpgName; `
"ProvisionedStorageInMB" = $myvarVM.ProvisionedStorageInMB; `
"UsedStorageInMB" = $myvarVM.UsedStorageInMB; `
"Status" = $myvarStatus; `
"SubStatus" = $myvarSubStatus; `
"ActualRPO" = $myvarVM.ActualRPO; `
"ThroughputInMB" = $myvarVM.ThroughputInMB}
#populate the array for our final report
$myvarResults.Add((New-Object PSObject -Property $myvarVMInfo)) | Out-Null
}
#export the results to csv
OutputLogData -category "INFO" -message "Creating source report as $myvarSourceReport..."
$myvarResults | export-csv -NoTypeInformation $myvarSourceReport
}#endif VMListJSON
else #no protected vms were found
{
OutputLogData -category "WARN" -message "Did not find any protected VMs on $zvm..."
}
}#end else if source report does not exist
}#endif check if valid session
else #we didn't get a valid session from the ZVM
{
OutputLogData -category "ERROR" -message "Could not connect to $zvm..."
}
#########################
## cleanup ##
#########################
#let's figure out how much time this all took
OutputLogData -category "SUM" -message "total processing time: $($myvarElapsedTime.Elapsed.ToString())"
#cleanup after ourselves and delete all custom variables
Remove-Variable myvar*
Remove-Variable help
Remove-Variable history
Remove-Variable log
Remove-Variable zvm
Remove-Variable zvmport
Remove-Variable username
Remove-Variable password
Remove-Variable debugme
Remove-Variable authInfo
Remove-Variable printonly