forked from sbourdeaud/nutanix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-AhvVmReport.ps1
executable file
·1020 lines (924 loc) · 39.1 KB
/
get-AhvVmReport.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
<#
.SYNOPSIS
This script retrieves the complete list of virtual machines from Prism Central.
.DESCRIPTION
This script retrieves the complete list of virtual machines from Prism Central, including each VM specs and exports the results to csv in the current directory.
.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 prismcentral
Nutanix Prism Central fully qualified domain name or IP address.
.PARAMETER prismCreds
Specifies a custom credentials file name (will look for %USERPROFILE\Documents\WindowsPowerShell\CustomCredentials\$prismCreds.txt on Windows or in $home/$prismCreds.txt on Mac and Linux).
.PARAMETER ngt
Retrieves additional VM information, including Nutanix Guest Tools status and version. This requires that the credentials to access Prism Element are the same as the ones used for Prism Central.
.EXAMPLE
.\get-AhvVmReport.ps1 -cluster ntnxc1.local
Connect to a Nutanix Prism Central VM of your choice and retrieve the list of VMs.
.LINK
http://github.com/sbourdeaud/nutanix
.NOTES
Author: Stephane Bourdeaud ([email protected])
Revision: April 15th 2022
#>
#region parameters
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 = $true)] [string]$prismcentral,
[parameter(mandatory = $false)] $prismCreds,
[parameter(mandatory = $false)] [switch]$ngt
)
#endregion
#region function
#this function is used to process output to console (timestamped and color coded) and log file
function Write-LogOutput
{#used to format output
<#
.SYNOPSIS
Outputs color coded messages to the screen and/or log file based on the category.
.DESCRIPTION
This function is used to produce screen and log output which is categorized, time stamped and color coded.
.PARAMETER Category
This the category of message being outputed. If you want color coding, use either "INFO", "WARNING", "ERROR" or "SUM".
.PARAMETER Message
This is the actual message you want to display.
.PARAMETER LogFile
If you want to log output to a file as well, use logfile to pass the log file full path name.
.NOTES
Author: Stephane Bourdeaud ([email protected])
.EXAMPLE
.\Write-LogOutput -category "ERROR" -message "You must be kidding!"
Displays an error message.
.LINK
https://github.com/sbourdeaud
#>
[CmdletBinding(DefaultParameterSetName = 'None')] #make this function advanced
param
(
[Parameter(Mandatory)]
[ValidateSet('INFO','WARNING','ERROR','SUM','SUCCESS','STEP','DEBUG','DATA')]
[string]
$Category,
[string]
$Message,
[string]
$LogFile
)
process
{
$Date = get-date #getting the date so we can timestamp the output entry
$FgColor = "Gray" #resetting the foreground/text color
switch ($Category) #we'll change the text color depending on the selected category
{
"INFO" {$FgColor = "Green"}
"WARNING" {$FgColor = "Yellow"}
"ERROR" {$FgColor = "Red"}
"SUM" {$FgColor = "Magenta"}
"SUCCESS" {$FgColor = "Cyan"}
"STEP" {$FgColor = "Magenta"}
"DEBUG" {$FgColor = "White"}
"DATA" {$FgColor = "Gray"}
}
Write-Host -ForegroundColor $FgColor "$Date [$category] $Message" #write the entry on the screen
if ($LogFile) #add the entry to the log file if -LogFile has been specified
{
Add-Content -Path $LogFile -Value "$Date [$Category] $Message"
Write-Verbose -Message "Wrote entry to log file $LogFile" #specifying that we have written to the log file if -verbose has been specified
}
}
}#end function Write-LogOutput
#this function loads a powershell module
function LoadModule
{#tries to load a module, import it, install it if necessary
<#
.SYNOPSIS
Tries to load the specified module and installs it if it can't.
.DESCRIPTION
Tries to load the specified module and installs it if it can't.
.NOTES
Author: Stephane Bourdeaud
.PARAMETER module
Name of PowerShell module to import.
.EXAMPLE
PS> LoadModule -module PSWriteHTML
#>
param
(
[string] $module
)
begin
{
}
process
{
Write-LogOutput -Category "INFO" -LogFile $myvar_log_file -Message "Trying to get module $($module)..."
if (!(Get-Module -Name $module))
{#we could not get the module, let's try to load it
try
{#import the module
Import-Module -Name $module -ErrorAction Stop
Write-LogOutput -Category "SUCCESS" -LogFile $myvar_log_file -Message "Imported module '$($module)'!"
}#end try
catch
{#we couldn't import the module, so let's install it
Write-LogOutput -Category "INFO" -LogFile $myvar_log_file -Message "Installing module '$($module)' from the Powershell Gallery..."
try
{#install module
Install-Module -Name $module -Scope CurrentUser -Force -ErrorAction Stop
}
catch
{#could not install module
Write-LogOutput -Category "ERROR" -LogFile $myvar_log_file -Message "Could not install module '$($module)': $($_.Exception.Message)"
exit 1
}
try
{#now that it is intalled, let's import it
Import-Module -Name $module -ErrorAction Stop
Write-LogOutput -Category "SUCCESS" -LogFile $myvar_log_file -Message "Imported module '$($module)'!"
}#end try
catch
{#we couldn't import the module
Write-LogOutput -Category "ERROR" -LogFile $myvar_log_file -Message "Unable to import the module $($module).psm1 : $($_.Exception.Message)"
Write-LogOutput -Category "WARNING" -LogFile $myvar_log_file -Message "Please download and install from https://www.powershellgallery.com"
Exit 1
}#end catch
}#end catch
}
}
end
{
}
}
function Set-CustomCredentials
{#creates files to store creds
#input: path, credname
#output: saved credentials file
<#
.SYNOPSIS
Creates a saved credential file using DAPI for the current user on the local machine.
.DESCRIPTION
This function is used to create a saved credential file using DAPI for the current user on the local machine.
.NOTES
Author: Stephane Bourdeaud
.PARAMETER path
Specifies the custom path where to save the credential file. By default, this will be %USERPROFILE%\Documents\WindowsPowershell\CustomCredentials.
.PARAMETER credname
Specifies the credential file name.
.EXAMPLE
.\Set-CustomCredentials -path c:\creds -credname prism-apiuser
Will prompt for user credentials and create a file called prism-apiuser.txt in c:\creds
#>
param
(
[parameter(mandatory = $false)]
[string]
$path,
[parameter(mandatory = $true)]
[string]
$credname
)
begin
{
if (!$path)
{
if ($IsLinux -or $IsMacOS)
{
$path = $home
}
else
{
$path = "$Env:USERPROFILE\Documents\WindowsPowerShell\CustomCredentials"
}
Write-Host "$(get-date) [INFO] Set path to $path" -ForegroundColor Green
}
}
process
{
#prompt for credentials
$credentialsFilePath = "$path\$credname.txt"
$credentials = Get-Credential -Message "Enter the credentials to save in $path\$credname.txt"
#put details in hashed format
$user = $credentials.UserName
$securePassword = $credentials.Password
#convert secureString to text
try
{
$password = $securePassword | ConvertFrom-SecureString -ErrorAction Stop
}
catch
{
throw "$(get-date) [ERROR] Could not convert password : $($_.Exception.Message)"
}
#create directory to store creds if it does not already exist
if(!(Test-Path $path))
{
try
{
$result = New-Item -type Directory $path -ErrorAction Stop
}
catch
{
throw "$(get-date) [ERROR] Could not create directory $path : $($_.Exception.Message)"
}
}
#save creds to file
try
{
Set-Content $credentialsFilePath $user -ErrorAction Stop
}
catch
{
throw "$(get-date) [ERROR] Could not write username to $credentialsFilePath : $($_.Exception.Message)"
}
try
{
Add-Content $credentialsFilePath $password -ErrorAction Stop
}
catch
{
throw "$(get-date) [ERROR] Could not write password to $credentialsFilePath : $($_.Exception.Message)"
}
Write-Host "$(get-date) [SUCCESS] Saved credentials to $credentialsFilePath" -ForegroundColor Cyan
}
end
{}
}
#this function is used to retrieve saved credentials for the current user
function Get-CustomCredentials
{#retrieves creds from files
#input: path, credname
#output: credential object
<#
.SYNOPSIS
Retrieves saved credential file using DAPI for the current user on the local machine.
.DESCRIPTION
This function is used to retrieve a saved credential file using DAPI for the current user on the local machine.
.NOTES
Author: Stephane Bourdeaud
.PARAMETER path
Specifies the custom path where the credential file is. By default, this will be %USERPROFILE%\Documents\WindowsPowershell\CustomCredentials.
.PARAMETER credname
Specifies the credential file name.
.EXAMPLE
.\Get-CustomCredentials -path c:\creds -credname prism-apiuser
Will retrieve credentials from the file called prism-apiuser.txt in c:\creds
#>
param
(
[parameter(mandatory = $false)]
[string]
$path,
[parameter(mandatory = $true)]
[string]
$credname
)
begin
{
if (!$path)
{
if ($IsLinux -or $IsMacOS)
{
$path = $home
}
else
{
$path = "$Env:USERPROFILE\Documents\WindowsPowerShell\CustomCredentials"
}
Write-Host "$(get-date) [INFO] Retrieving credentials from $path" -ForegroundColor Green
}
}
process
{
$credentialsFilePath = "$path\$credname.txt"
if(!(Test-Path $credentialsFilePath))
{
throw "$(get-date) [ERROR] Could not access file $credentialsFilePath : $($_.Exception.Message)"
}
$credFile = Get-Content $credentialsFilePath
$user = $credFile[0]
$securePassword = $credFile[1] | ConvertTo-SecureString
$customCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $securePassword
Write-Host "$(get-date) [SUCCESS] Returning credentials from $credentialsFilePath" -ForegroundColor Cyan
}
end
{
return $customCredentials
}
}
#this function is used to make sure we use the proper Tls version (1.2 only required for connection to Prism)
function Set-PoshTls
{#disables unsecure Tls protocols
<#
.SYNOPSIS
Makes sure we use the proper Tls version (1.2 only required for connection to Prism).
.DESCRIPTION
Makes sure we use the proper Tls version (1.2 only required for connection to Prism).
.NOTES
Author: Stephane Bourdeaud ([email protected])
.EXAMPLE
.\Set-PoshTls
Makes sure we use the proper Tls version (1.2 only required for connection to Prism).
.LINK
https://github.com/sbourdeaud
#>
[CmdletBinding(DefaultParameterSetName = 'None')] #make this function advanced
param
(
)
begin
{
}
process
{
Write-Host "$(Get-Date) [INFO] Adding Tls12 support" -ForegroundColor Green
[Net.ServicePointManager]::SecurityProtocol = `
([Net.ServicePointManager]::SecurityProtocol -bor `
[Net.SecurityProtocolType]::Tls12)
}
end
{
}
}
#this function is used to configure posh to ignore invalid ssl certificates
function Set-PoSHSSLCerts
{#configures posh to ignore self-signed certs
<#
.SYNOPSIS
Configures PoSH to ignore invalid SSL certificates when doing Invoke-RestMethod
.DESCRIPTION
Configures PoSH to ignore invalid SSL certificates when doing Invoke-RestMethod
#>
begin
{
}#endbegin
process
{
Write-Host "$(Get-Date) [INFO] Ignoring invalid certificates" -ForegroundColor Green
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type) {
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}#endif
[ServerCertificateValidationCallback]::Ignore()
}#endprocess
end
{
}#endend
}#end function Set-PoSHSSLCerts
#this function is used to make a REST api call to Prism
function Invoke-PrismAPICall
{#makes a REST API call to Prism
<#
.SYNOPSIS
Makes api call to prism based on passed parameters. Returns the json response.
.DESCRIPTION
Makes api call to prism based on passed parameters. Returns the json response.
.NOTES
Author: Stephane Bourdeaud
.PARAMETER method
REST method (POST, GET, DELETE, or PUT)
.PARAMETER credential
PSCredential object to use for authentication.
PARAMETER url
URL to the api endpoint.
PARAMETER payload
JSON payload to send.
.EXAMPLE
.\Invoke-PrismAPICall -credential $MyCredObject -url https://myprism.local/api/v3/vms/list -method 'POST' -payload $MyPayload
Makes a POST api call to the specified endpoint with the specified payload.
#>
param
(
[parameter(mandatory = $true)]
[ValidateSet("POST","GET","DELETE","PUT")]
[string]
$method,
[parameter(mandatory = $true)]
[string]
$url,
[parameter(mandatory = $false)]
[string]
$payload,
[parameter(mandatory = $true)]
[System.Management.Automation.PSCredential]
$credential
)
begin
{
}
process
{
Write-Host "$(Get-Date) [INFO] Making a $method call to $url" -ForegroundColor Green
try {
#check powershell version as PoSH 6 Invoke-RestMethod can natively skip SSL certificates checks and enforce Tls12 as well as use basic authentication with a pscredential object
if ($PSVersionTable.PSVersion.Major -gt 5)
{
$headers = @{
"Content-Type"="application/json";
"Accept"="application/json"
}
if ($payload)
{
$resp = Invoke-RestMethod -Method $method -Uri $url -Headers $headers -Body $payload -SkipCertificateCheck -SslProtocol Tls12 -Authentication Basic -Credential $credential -ErrorAction Stop
}
else
{
$resp = Invoke-RestMethod -Method $method -Uri $url -Headers $headers -SkipCertificateCheck -SslProtocol Tls12 -Authentication Basic -Credential $credential -ErrorAction Stop
}
}
else
{
$username = $credential.UserName
$password = $credential.Password
$headers = @{
"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))) ));
"Content-Type"="application/json";
"Accept"="application/json"
}
if ($payload)
{
$resp = Invoke-RestMethod -Method $method -Uri $url -Headers $headers -Body $payload -ErrorAction Stop
}
else
{
$resp = Invoke-RestMethod -Method $method -Uri $url -Headers $headers -ErrorAction Stop
}
}
Write-Host "$(get-date) [SUCCESS] Call $method to $url succeeded." -ForegroundColor Cyan
if ($debugme) {Write-Host "$(Get-Date) [DEBUG] Response Metadata: $($resp.metadata | ConvertTo-Json)" -ForegroundColor White}
}
catch {
$saved_error = $_.Exception
$saved_error_message = ($_.ErrorDetails.Message | ConvertFrom-Json).message_list.message
$resp_return_code = $_.Exception.Response.StatusCode.value__
# Write-Host "$(Get-Date) [INFO] Headers: $($headers | ConvertTo-Json)"
if ($resp_return_code -eq 409)
{
Write-Host "$(Get-Date) [WARNING] $saved_error_message" -ForegroundColor Yellow
Throw
}
else
{
if ($saved_error_message -match 'rule already exists')
{
Throw "$(get-date) [WARNING] $saved_error_message"
}
else
{
if ($payload) {Write-Host "$(Get-Date) [INFO] Payload: $payload" -ForegroundColor Green}
Throw "$(get-date) [ERROR] $resp_return_code $saved_error_message"
}
}
}
finally {
#add any last words here; this gets processed no matter what
}
}
end
{
return $resp
}
}
#helper-function Get-RESTError
function Help-RESTError
{#tries to retrieve full REST messages
$global:helpme = $body
$global:helpmoref = $moref
$global:result = $_.Exception.Response.GetResponseStream()
$global:reader = New-Object System.IO.StreamReader($global:result)
$global:responseBody = $global:reader.ReadToEnd();
return $global:responsebody
break
}#end function Get-RESTError
function Get-PrismCentralObjectList
{#retrieves multiple pages of Prism REST objects v3
[CmdletBinding()]
param
(
[Parameter(mandatory = $true)][string] $pc,
[Parameter(mandatory = $true)][string] $object,
[Parameter(mandatory = $true)][string] $kind
)
begin
{
if (!$length) {$length = 100} #we may not inherit the $length variable; if that is the case, set it to 100 objects per page
$total, $cumulated, $first, $last, $offset = 0 #those are used to keep track of how many objects we have processed
[System.Collections.ArrayList]$myvarResults = New-Object System.Collections.ArrayList($null) #this is variable we will use to keep track of entities
$url = "https://{0}:9440/api/nutanix/v3/{1}/list" -f $pc,$object
$method = "POST"
$content = @{
kind=$kind;
offset=0;
length=$length
}
$payload = (ConvertTo-Json $content -Depth 4) #this is the initial payload at offset 0
}
process
{
Do {
try {
$resp = Invoke-PrismAPICall -method $method -url $url -payload $payload -credential $prismCredentials
if ($total -eq 0) {$total = $resp.metadata.total_matches} #this is the first time we go thru this loop, so let's assign the total number of objects
$first = $offset #this is the first object for this iteration
$last = $offset + ($resp.entities).count #this is the last object for this iteration
if ($total -le $length)
{#we have less objects than our specified length
$cumulated = $total
}
else
{#we have more objects than our specified length, so let's increment cumulated
$cumulated += ($resp.entities).count
}
Write-Host "$(Get-Date) [INFO] Processing results from $(if ($first) {$first} else {"0"}) to $($last) out of $($total)" -ForegroundColor Green
if ($debugme) {Write-Host "$(Get-Date) [DEBUG] Response Metadata: $($resp.metadata | ConvertTo-Json)" -ForegroundColor White}
#grab the information we need in each entity
ForEach ($entity in $resp.entities) {
$myvarResults.Add($entity) | Out-Null
}
$offset = $last #let's increment our offset
#prepare the json payload for the next batch of entities/response
$content = @{
kind=$kind;
offset=$offset;
length=$length
}
$payload = (ConvertTo-Json $content -Depth 4)
}
catch {
$saved_error = $_.Exception.Message
# Write-Host "$(Get-Date) [INFO] Headers: $($headers | ConvertTo-Json)"
if ($payload) {Write-Host "$(Get-Date) [INFO] Payload: $payload" -ForegroundColor Green}
Throw "$(get-date) [ERROR] $saved_error"
}
finally {
#add any last words here; this gets processed no matter what
}
}
While ($last -lt $total)
}
end
{
return $myvarResults
}
}
#endregion
#region prepwork
$HistoryText = @'
Maintenance Log
Date By Updates (newest updates at the top)
---------- ---- ---------------------------------------------------------------
06/21/2019 sb Initial release.
04/02/2020 sb Do over to include sbourdeaud module.
02/06/2021 sb Replaced username with get-credential
04/15/2022 sb Added functions and removed dependency to sbourdeaud module.
################################################################################
'@
$myvarScriptName = ".\get-AhvVmReport.ps1"
if ($help) {get-help $myvarScriptName; exit}
if ($History) {$HistoryText; exit}
#check PoSH version
if ($PSVersionTable.PSVersion.Major -lt 5) {throw "$(get-date) [ERROR] Please upgrade to Powershell v5 or above (https://www.microsoft.com/en-us/download/details.aspx?id=50395)"}
<# #region module sbourdeaud is used for facilitating Prism REST calls
$required_version = "3.0.8"
if (!(Get-Module -Name sbourdeaud))
{
Write-Host "$(get-date) [INFO] Importing module 'sbourdeaud'..." -ForegroundColor Green
try
{
Import-Module -Name sbourdeaud -MinimumVersion $required_version -ErrorAction Stop
Write-Host "$(get-date) [SUCCESS] Imported module 'sbourdeaud'!" -ForegroundColor Cyan
}#end try
catch #we couldn't import the module, so let's install it
{
Write-Host "$(get-date) [INFO] Installing module 'sbourdeaud' from the Powershell Gallery..." -ForegroundColor Green
try {Install-Module -Name sbourdeaud -Scope CurrentUser -Force -ErrorAction Stop}
catch {throw "$(get-date) [ERROR] Could not install module 'sbourdeaud': $($_.Exception.Message)"}
try
{
Import-Module -Name sbourdeaud -MinimumVersion $required_version -ErrorAction Stop
Write-Host "$(get-date) [SUCCESS] Imported module 'sbourdeaud'!" -ForegroundColor Cyan
}#end try
catch #we couldn't import the module
{
Write-Host "$(get-date) [ERROR] Unable to import the module sbourdeaud.psm1 : $($_.Exception.Message)" -ForegroundColor Red
Write-Host "$(get-date) [WARNING] Please download and install from https://www.powershellgallery.com/packages/sbourdeaud/1.1" -ForegroundColor Yellow
Exit
}#end catch
}#end catch
}#endif module sbourdeaud
$MyVarModuleVersion = Get-Module -Name sbourdeaud | Select-Object -Property Version
if (($MyVarModuleVersion.Version.Major -lt $($required_version.split('.')[0])) -or (($MyVarModuleVersion.Version.Major -eq $($required_version.split('.')[0])) -and ($MyVarModuleVersion.Version.Minor -eq $($required_version.split('.')[1])) -and ($MyVarModuleVersion.Version.Build -lt $($required_version.split('.')[2]))))
{
Write-Host "$(get-date) [INFO] Updating module 'sbourdeaud'..." -ForegroundColor Green
Remove-Module -Name sbourdeaud -ErrorAction SilentlyContinue
Uninstall-Module -Name sbourdeaud -ErrorAction SilentlyContinue
try
{
Update-Module -Name sbourdeaud -Scope CurrentUser -ErrorAction Stop
Import-Module -Name sbourdeaud -ErrorAction Stop
}
catch {throw "$(get-date) [ERROR] Could not update module 'sbourdeaud': $($_.Exception.Message)"}
}
#endregion #>
Set-PoSHSSLCerts
Set-PoshTls
#endregion
#region variables
$myvarElapsedTime = [System.Diagnostics.Stopwatch]::StartNew()
#prepare our overall results variable
[System.Collections.ArrayList]$myvarResults = New-Object System.Collections.ArrayList($null)
$length=100 #this specifies how many entities we want in the results of each API query
$api_server_port = "9440"
#endregion
#region parameters validation
if (!$prismCreds)
{#we are not using custom credentials, so let's ask for a username and password if they have not already been specified
$prismCredentials = Get-Credential -Message "Please enter Prism credentials"
}
else
{ #we are using custom credentials, so let's grab the username and password from that
try
{
$prismCredentials = Get-CustomCredentials -credname $prismCreds -ErrorAction Stop
}
catch
{
Set-CustomCredentials -credname $prismCreds
$prismCredentials = Get-CustomCredentials -credname $prismCreds -ErrorAction Stop
}
}
$username = $prismCredentials.UserName
$PrismSecurePassword = $prismCredentials.Password
$prismCredentials = New-Object PSCredential $username, $PrismSecurePassword
#endregion
#region main processing
#region make v3 api call for clusters (if -ngt)
if ($ngt)
{
[System.Collections.ArrayList]$myvarClusterResults = New-Object System.Collections.ArrayList($null)
$api_server_endpoint = "/api/nutanix/v3/clusters/list"
$url = "https://{0}:{1}{2}" -f $prismcentral,$api_server_port, $api_server_endpoint
$method = "POST"
# this is used to capture the content of the payload
$content = @{
kind="cluster";
offset=0;
length=$length
}
$payload = (ConvertTo-Json $content -Depth 4)
Write-Host "$(Get-Date) [INFO] Retrieving clusters information from Prism Central $($prismcentral)" -ForegroundColor Green
Do
{
try
{
$resp = Invoke-PrismAPICall -method $method -url $url -payload $payload -credential $prismCredentials
$listLength = 0
if ($resp.metadata.offset)
{
$firstItem = $resp.metadata.offset
}
else
{
$firstItem = 0
}
if (($resp.metadata.length -le $length) -and ($resp.metadata.length -ne 1))
{
$listLength = $resp.metadata.length
}
else
{
$listLength = $resp.metadata.total_matches
}
Write-Host "$(Get-Date) [INFO] Processing results from $($firstItem) to $($firstItem + $listLength) out of $($resp.metadata.total_matches)" -ForegroundColor Green
if ($debugme) {Write-Host "$(Get-Date) [DEBUG] Response Metadata: $($resp.metadata | ConvertTo-Json)" -ForegroundColor White}
#grab the information we need in each entity
ForEach ($entity in $resp.entities)
{
$myvarClusterInfo = [ordered]@{
"name" = $entity.spec.name;
"uuid" = $entity.metadata.uuid;
"external_ip" = $entity.spec.resources.network.external_ip;
"AOS" = $entity.spec.resources.config.software_map.NOS.version
}
#store the results for this entity in our overall result variable
$myvarClusterResults.Add((New-Object PSObject -Property $myvarClusterInfo)) | Out-Null
}
#prepare the json payload for the next batch of entities/response
$content = @{
kind="cluster";
offset=($resp.metadata.length + $resp.metadata.offset);
length=$length
}
$payload = (ConvertTo-Json $content -Depth 4)
}
catch
{
$saved_error = $_.Exception.Message
# Write-Host "$(Get-Date) [INFO] Headers: $($headers | ConvertTo-Json)"
Write-Host "$(Get-Date) [INFO] Payload: $payload" -ForegroundColor Green
Throw "$(get-date) [ERROR] $saved_error"
}
finally
{
#add any last words here; this gets processed no matter what
}
}
While ($resp.metadata.length -eq $length)
if ($debugme)
{
Write-Host "$(Get-Date) [DEBUG] Showing results:" -ForegroundColor White
$myvarClusterResults
}
}
#endregion
#region make v3 api call for vms
# this is used to capture the content of the payload
$content = @{
kind="vm";
offset=0;
length=$length
}
$payload = (ConvertTo-Json $content -Depth 4)
Write-Host "$(Get-Date) [INFO] Retrieving VM information from Prism Central $($prismcentral)" -ForegroundColor Green
Do
{
try
{
$api_server_endpoint = "/api/nutanix/v3/vms/list"
$url = "https://{0}:{1}{2}" -f $prismcentral,$api_server_port, $api_server_endpoint
$method = "POST"
$resp = Invoke-PrismAPICall -method $method -url $url -payload $payload -credential $prismCredentials
$listLength = 0
if ($resp.metadata.offset)
{
$firstItem = $resp.metadata.offset
}
else
{
$firstItem = 0
}
if (($resp.metadata.length -le $length) -and ($resp.metadata.length -ne 1))
{
$listLength = $resp.metadata.length
}
else
{
$listLength = $resp.metadata.total_matches,
}
Write-Host "$(Get-Date) [INFO] Processing results from $($firstItem) to $($firstItem + $listLength) out of $($resp.metadata.total_matches)" -ForegroundColor Green
if ($debugme) {Write-Host "$(Get-Date) [DEBUG] Response Metadata: $($resp.metadata | ConvertTo-Json)" -ForegroundColor White}
#grab the information we need in each entity
ForEach ($entity in $resp.entities)
{
if ($ngt)
{
$myvarClusterIp = ($myvarClusterResults | Where-Object {$_.uuid -eq $entity.spec.cluster_reference.uuid}).external_ip
if (!$myvarClusterIp) {throw "$(get-date) [ERROR] Could not find external ip address of cluster $($entity.spec.cluster_reference.name)"}
$api_server_endpoint = "/PrismGateway/services/rest/v1/vms/?filterCriteria=vm_uuid%3D%3D{0}" -f $entity.metadata.uuid
$url = "https://{0}:{1}{2}" -f $myvarClusterIp,$api_server_port, $api_server_endpoint
$method = "GET"
try
{
Write-Host "$(Get-Date) [INFO] Retrieving detailed VM information for $($entity.spec.name) from cluster $($entity.spec.cluster_reference.name)" -ForegroundColor Green
$myvarVmDetails = Invoke-PrismAPICall -method $method -url $url -credential $prismCredentials
Write-Host "$(Get-Date) [SUCCESS] Successfully retrieved detailed VM information for $($entity.spec.name) from cluster $($entity.spec.cluster_reference.name)" -ForegroundColor Cyan
}
catch
{
$saved_error = $_.Exception.Message
# Write-Host "$(Get-Date) [INFO] Headers: $($headers | ConvertTo-Json)"
#Write-Host "$(Get-Date) [INFO] Payload: $payload" -ForegroundColor Green
Throw "$(get-date) [ERROR] $saved_error"
}
$myvarVmInfo = [ordered]@{
"name" = $entity.spec.name;
#"os" = $myvarVmDetails.entities.guestOperatingSystem;
"ip_addresses" = $myvarVmDetails.entities.ipAddresses -join ',';
"virtual_disks" = $myvarVmDetails.entities.nutanixVirtualDisks -join ',';
"flash_mode" = $myvarVmDetails.entities.vmFeatures.FLASH_MODE;
"description" = $myvarVmDetails.entities.description;
"ngt_status" = $myvarVmDetails.entities.nutanixGuestTools.enabled;
"ngt_version" = $myvarVmDetails.entities.nutanixGuestTools.installedVersion;
"ngt_vss_snapshot" = $myvarVmDetails.entities.nutanixGuestTools.applications.vss_snapshot;
"ngt_vss_file_level_restore" = $myvarVmDetails.entities.nutanixGuestTools.applications.file_level_restore;
"ngt_iso_mounted" = $myvarVmDetails.entities.nutanixGuestTools.toolsMounted;
"ngt_communication_alive" = $myvarVmDetails.entities.nutanixGuestTools.communicationLinkActive;
"num_sockets" = $entity.spec.resources.num_sockets;
"memory_size_mib" = $entity.spec.resources.memory_size_mib;
"power_state" = $entity.spec.resources.power_state;
"cluster" = $entity.spec.cluster_reference.name;
"hypervisor" = $entity.status.resources.hypervisor_type;
"creation_time" = $entity.metadata.creation_time;
"owner" = $entity.metadata.owner_reference.name;
"protection_type" = $entity.status.resources.protection_type;
"vdisk_count" = ($entity.spec.resources.disk_list | where-object {$_.device_properties.device_type -eq "DISK"}).Count;
"vdisk_total_mib" = ($entity.spec.resources.disk_list | where-object {$_.device_properties.device_type -eq "DISK"} | Measure-Object disk_size_mib -Sum).Sum;
"vnic_count" = ($entity.spec.resources.nic_list).Count;
"vnic_vlans" = (($entity.spec.resources.nic_list | Select-Object -Property subnet_reference).subnet_reference.name) -join ',';
"vnic_macs" = (($entity.spec.resources.nic_list | Select-Object -Property mac_address).mac_address) -join ',';
"gpu" = $entity.status.resources.gpu_list | Select-Object -First 1;
"uuid" = $entity.metadata.uuid
}
}
else
{
$myvarVmInfo = [ordered]@{
"name" = $entity.spec.name;
"num_sockets" = $entity.spec.resources.num_sockets;
"memory_size_mib" = $entity.spec.resources.memory_size_mib;
"power_state" = $entity.spec.resources.power_state;
"cluster" = $entity.spec.cluster_reference.name;
"hypervisor" = $entity.status.resources.hypervisor_type;
"creation_time" = $entity.metadata.creation_time;
"owner" = $entity.metadata.owner_reference.name;
"protection_type" = $entity.status.resources.protection_type;
"vdisk_count" = ($entity.spec.resources.disk_list | where-object {$_.device_properties.device_type -eq "DISK"}).Count;
"vdisk_total_mib" = ($entity.spec.resources.disk_list | where-object {$_.device_properties.device_type -eq "DISK"} | Measure-Object disk_size_mib -Sum).Sum;
"vnic_count" = ($entity.spec.resources.nic_list).Count;
"vnic0_vlan" = $entity.spec.resources.nic_list[0].subnet_reference.name;
"vnic0_mac" = $entity.spec.resources.nic_list[0].mac_address;
"vnic1_vlan" = $entity.spec.resources.nic_list[1].subnet_reference.name;
"vnic1_mac" = $entity.spec.resources.nic_list[1].mac_address;
"gpu" = $entity.status.resources.gpu_list | Select-Object -First 1;
"uuid" = $entity.metadata.uuid
}
}
#store the results for this entity in our overall result variable
$myvarResults.Add((New-Object PSObject -Property $myvarVmInfo)) | Out-Null
}
#prepare the json payload for the next batch of entities/response
$content = @{
kind="vm";
offset=($resp.metadata.length + $resp.metadata.offset);
length=$length
}
$payload = (ConvertTo-Json $content -Depth 4)
}
catch
{
$saved_error = $_.Exception.Message
# Write-Host "$(Get-Date) [INFO] Headers: $($headers | ConvertTo-Json)"
Write-Host "$(Get-Date) [INFO] Payload: $payload" -ForegroundColor Green
Throw "$(get-date) [ERROR] $saved_error"
}
finally
{
#add any last words here; this gets processed no matter what
}
}
While ($resp.metadata.length -eq $length)
if ($debugme)
{
Write-Host "$(Get-Date) [DEBUG] Showing results:" -ForegroundColor White
$myvarResults