forked from vmware-archive/PowerCLI-Example-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVi-Module.psm1
More file actions
1241 lines (1084 loc) · 38.9 KB
/
Vi-Module.psm1
File metadata and controls
1241 lines (1084 loc) · 38.9 KB
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
Function Get-RDM {
<#
.SYNOPSIS
Get all RDMs.
.DESCRIPTION
This function reports all VMs with their RDM disks.
.PARAMETER VM
VM's collection, returned by Get-VM cmdlet.
.EXAMPLE
C:\PS> Get-VM -Server VC1 |Get-RDM
.EXAMPLE
C:\PS> Get-VM |? {$_.Name -like 'linux*'} |Get-RDM |sort-object VM,Datastore,HDLabel |ft -au
.EXAMPLE
C:\PS> Get-Datacenter 'North' |Get-VM |Get-RDM |? {$_.HDSizeGB -gt 1} |Export-Csv -NoTypeInformation 'C:\reports\North_RDMs.csv'
.EXAMPLE
C:\PS> $res = Get-Cluster prod |Get-VM |Get-ViMRDM
C:\PS> $res |Export-Csv -NoTypeInformation 'C:\reports\ProdCluster_RDMs.csv'
Save the results in variable and than export them to a file.
.INPUTS
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] Get-VM collection.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author: Roman Gelman.
Version 1.0 :: 16-Oct-2015 :: Release
Version 1.1 :: 03-Dec-2015 :: Bugfix :: Error message appear while VML mismatch,
when the VML identifier does not match for an RDM on two or more ESXi hosts.
VMware [KB2097287].
Version 1.2 :: 03-Aug-2016 :: Improvement :: GetType() method replaced by -is for type determine.
.LINK
http://www.ps1code.com/single-post/2015/10/16/How-to-get-RDM-Raw-Device-Mappings-disks-using-PowerCLi
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false,Position=1,ValueFromPipeline=$true,HelpMessage="VM's collection, returned by Get-VM cmdlet")]
[ValidateNotNullorEmpty()]
[Alias("VM")]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]$VMs = (Get-VM)
)
Begin {
$Object = @()
$regxVMDK = '^\[(?<Datastore>.+)\]\s(?<Filename>.+)$'
$regxLUNID = ':L(?<LUNID>\d+)$'
}
Process {
Foreach ($vm in ($VMs |Get-View)) {
Foreach ($dev in $vm.Config.Hardware.Device) {
If ($dev -is [VMware.Vim.VirtualDisk]) {
If ("physicalMode","virtualMode" -contains $dev.Backing.CompatibilityMode) {
Write-Progress -Activity "Gathering RDM ..." -CurrentOperation "Hard disk - [$($dev.DeviceInfo.Label)]" -Status "VM - $($vm.Name)"
$esx = Get-View $vm.Runtime.Host
$esxScsiLun = $esx.Config.StorageDevice.ScsiLun |? {$_.Uuid -eq $dev.Backing.LunUuid}
### Expand 'LUNID' from device runtime name (vmhba2:C0:T0:L12) ###
$lunCN = $esxScsiLun.CanonicalName
$Matches = $null
If ($lunCN) {
$null = (Get-ScsiLun -VmHost $esx.Name -CanonicalName $lunCN -ErrorAction SilentlyContinue).RuntimeName -match $regxLUNID
$lunID = $Matches.LUNID
} Else {$lunID = ''}
### Expand 'Datastore' and 'VMDK' from file path ###
$null = $dev.Backing.FileName -match $regxVMDK
$Properties = [ordered]@{
VM = $vm.Name
VMHost = $esx.Name
Datastore = $Matches.Datastore
VMDK = $Matches.Filename
HDLabel = $dev.DeviceInfo.Label
HDSizeGB = [math]::Round(($dev.CapacityInKB / 1MB), 3)
HDMode = $dev.Backing.CompatibilityMode
DeviceName = $dev.Backing.DeviceName
Vendor = $esxScsiLun.Vendor
CanonicalName = $lunCN
LUNID = $lunID
}
$Object = New-Object PSObject -Property $Properties
$Object
}
}
}
}
}
End {
Write-Progress -Completed $true -Status "Please wait"
}
} #EndFunction Get-RDM
New-Alias -Name Get-ViMRDM -Value Get-RDM -Force:$true
Function Convert-VmdkThin2EZThick {
<#
.SYNOPSIS
Inflate thin virtual disks.
.DESCRIPTION
This function converts all Thin Provisioned VM' disks to type 'Thick Provision Eager Zeroed'.
.PARAMETER VM
Virtual Machine(s).
.EXAMPLE
C:\PS> Get-VM VM1 |Convert-VmdkThin2EZThick
.EXAMPLE
C:\PS> Get-VM VM1,VM2 |Convert-VmdkThin2EZThick -Confirm:$false |sort-object VM,Datastore,VMDK |ft -au
.INPUTS
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] Objects, returned by Get-VM cmdlet.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author: Roman Gelman.
Version 1.0 :: 05-Nov-2015 :: Release.
Version 1.1 :: 03-Aug-2016 :: Improvements ::
[1] GetType() method replaced by -is for type determine.
[2] Parameter 'VMs' renamed to 'VM', parameter alias renamed from 'VM' to 'VMs'.
.LINK
http://www.ps1code.com/single-post/2015/11/05/How-to-convert-Thin-Provision-VMDK-disks-to-Eager-Zeroed-Thick-using-PowerCLi
#>
[CmdletBinding(ConfirmImpact='High',SupportsShouldProcess=$true)]
Param (
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true,HelpMessage="VM's collection, returned by Get-VM cmdlet")]
[ValidateNotNullorEmpty()]
[Alias("VMs")]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]$VM
)
Begin {
$Object = @()
$regxVMDK = '^\[(?<Datastore>.+)\]\s(?<Filename>.+)$'
}
Process {
Foreach ($vmv in ($VM |Get-View)) {
### Ask confirmation to proceed if VM is PoweredOff ###
If ($vmv.Runtime.PowerState -eq 'poweredOff' -and $PSCmdlet.ShouldProcess("VM [$($vmv.Name)]","Convert all Thin Provisioned VMDK to Type: 'Thick Provision Eager Zeroed'")) {
### Get ESXi object where $vmv is registered ###
$esx = Get-View $vmv.Runtime.Host
### Get Datacenter object where $vmv is registered ###
$parentObj = Get-View $vmv.Parent
While ($parentObj -isnot [VMware.Vim.Datacenter]) {$parentObj = Get-View $parentObj.Parent}
$datacenter = New-Object VMware.Vim.ManagedObjectReference
$datacenter.Type = 'Datacenter'
$datacenter.Value = $parentObj.MoRef.Value
Foreach ($dev in $vmv.Config.Hardware.Device) {
If ($dev -is [VMware.Vim.VirtualDisk]) {
If ($dev.Backing.ThinProvisioned -and $dev.Backing.Parent -eq $null) {
$sizeGB = [math]::Round(($dev.CapacityInKB / 1MB), 1)
### Invoke 'Inflate virtual disk' task ###
$ViDM = Get-View -Id 'VirtualDiskManager-virtualDiskManager'
$taskMoRef = $ViDM.InflateVirtualDisk_Task($dev.Backing.FileName, $datacenter)
$task = Get-View $taskMoRef
### Show task progress ###
For ($i=1; $i -lt [int32]::MaxValue; $i++) {
If ("running","queued" -contains $task.Info.State) {
$task.UpdateViewData("Info")
If ($task.Info.Progress -ne $null) {
Write-Progress -Activity "Inflate virtual disk task is in progress ..." -Status "VM - $($vmv.Name)" `
-CurrentOperation "$($dev.DeviceInfo.Label) - $($dev.Backing.FileName) - $sizeGB GB" `
-PercentComplete $task.Info.Progress -ErrorAction SilentlyContinue
Start-Sleep -Seconds 3
}
}
Else {Break}
}
### Get task completion results ###
$tResult = $task.Info.State
$tStart = $task.Info.StartTime
$tEnd = $task.Info.CompleteTime
$tCompleteTime = [math]::Round((New-TimeSpan -Start $tStart -End $tEnd).TotalMinutes, 1)
### Expand 'Datastore' and 'VMDK' from file path ###
$null = $dev.Backing.FileName -match $regxVMDK
$Properties = [ordered]@{
VM = $vmv.Name
VMHost = $esx.Name
Datastore = $Matches.Datastore
VMDK = $Matches.Filename
HDLabel = $dev.DeviceInfo.Label
HDSizeGB = $sizeGB
Result = $tResult
StartTime = $tStart
CompleteTime = $tEnd
TimeMin = $tCompleteTime
}
$Object = New-Object PSObject -Property $Properties
$Object
}
}
}
$vmv.Reload()
}
}
}
End {
Write-Progress -Completed $true -Status "Please wait"
}
} #EndFunction Convert-VmdkThin2EZThick
New-Alias -Name Convert-ViMVmdkThin2EZThick -Value Convert-VmdkThin2EZThick -Force:$true
Function Find-VcVm {
<#
.SYNOPSIS
Search VC's VM throw direct connection to group of ESXi Hosts.
.DESCRIPTION
This script generates a list of ESXi Hosts with common suffix in a name,
e.g. (esxprod1,esxprod2, ...) or (esxdev01,esxdev02, ...) etc. and
searches VCenter's VM throw direct connection to this group of ESXi Hosts.
.PARAMETER VC
VC's VM Name.
.PARAMETER HostSuffix
ESXi Hosts' common suffix.
.PARAMETER PostfixStart
ESXi Hosts' postfix number start.
.PARAMETER PostfixEnd
ESXi Hosts' postfix number end.
.PARAMETER AddZero
Add ESXi Hosts' postfix leading zero to one-digit postfix (from 01 to 09).
.EXAMPLE
PS C:\> Find-VcVm vc1 esxprod 1 20 -AddZero
.EXAMPLE
PS C:\> Find-VcVm -VC vc1 -HostSuffix esxdev -PostfixEnd 6
.EXAMPLE
PS C:\> Find-VcVm vc1 esxprod |fl
.NOTES
Author :: Roman Gelman.
Limitation :: [1] The function uses common credentials for all ESXi hosts.
[2] The hosts' Lockdown mode should be disabled.
Version 1.0 :: 03-Sep-2015 :: Release.
Version 1.1 :: 03-Aug-2016 :: Improvement :: Returned object properties changed.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.LINK
http://ps1code.com
#>
Param (
[Parameter(Mandatory=$true,Position=1,HelpMessage="vCenter's VM Name")]
[Alias("vCenter","VcVm")]
[string]$VC
,
[Parameter(Mandatory=$true,Position=2,HelpMessage="ESXi Hosts' common suffix")]
[Alias("VMHostSuffix","ESXiSuffix")]
[string]$HostSuffix
,
[Parameter(Mandatory=$false,Position=3,HelpMessage="ESXi Hosts' postfix number start")]
[ValidateRange(1,98)]
[Alias("PostfixFirst","Start")]
[int]$PostfixStart = 1
,
[Parameter(Mandatory=$false,Position=4,HelpMessage="ESXi Hosts' postfix number end")]
[ValidateRange(2,99)]
[Alias("PostfixLast","End")]
[int]$PostfixEnd = 9
,
[Parameter(Mandatory=$false,Position=5,HelpMessage="Add ESXi Hosts' postfix leading zero")]
[switch]$AddZero = $false
)
Begin {
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session -Confirm:$false |Out-Null
If ($PostfixEnd -le $PostfixStart) {Throw "PostfixEnd must be greater than PostfixStart"}
}
Process {
$cred = Get-Credential -UserName root -Message "Common VMHost Credentials"
If ($cred) {
$hosts = @()
For ($i=$PostfixStart; $i -le $PostfixEnd; $i++) {
If ($AddZero -and $i -match '^\d{1}$') {
$hosts += $HostSuffix + '0' + $i
} Else {
$hosts += $HostSuffix + $i
}
}
Connect-VIServer $hosts -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -Credential $cred `
|select @{N='VMHost';E={$_.Name}},IsConnected |ft -AutoSize
If ($global:DefaultVIServers.Length -ne 0) {
$TargetVM = Get-VM -ErrorAction SilentlyContinue |? {$_.Name -eq $VC}
$VCHostname = $TargetVM.Guest.HostName
$PowerState = $TargetVM.PowerState
$VMHostHostname = $TargetVM.VMHost.Name
Disconnect-VIServer -Server '*' -Force -Confirm:$false
}
}
}
End {
If ($TargetVM) {
$Properties = [ordered]@{
VC = $VC
Hostname = $VCHostname
PowerState = $PowerState
VMHost = $VMHostHostname
}
$Object = New-Object PSObject -Property $Properties
$Object
}
}
} #EndFunction Find-VcVm
New-Alias -Name Find-ViMVcVm -Value Find-VcVm -Force:$true
Function Set-PowerCLiTitle {
<#
.SYNOPSIS
Write connected VI servers info to PowerCLi window title bar.
.DESCRIPTION
This function write connected VI servers info to PowerCLi window/console title bar [Name :: Product (VCenter/ESXi) ProductVersion].
.EXAMPLE
C:\PS> Set-PowerCLiTitle
.NOTES
Author: Roman Gelman.
.LINK
http://www.ps1code.com/single-post/2015/11/17/ConnectVIServer-deep-dive-or-%C2%ABWhere-am-I-connected-%C2%BB
#>
$VIS = $global:DefaultVIServers |sort-object -Descending ProductLine,Name
If ($VIS) {
Foreach ($VIObj in $VIS) {
If ($VIObj.IsConnected) {
Switch -exact ($VIObj.ProductLine) {
vpx {$VIProduct = 'VCenter'; Break}
embeddedEsx {$VIProduct = 'ESXi'; Break}
Default {$VIProduct = $VIObj.ProductLine; Break}
}
$Header += "[$($VIObj.Name) :: $VIProduct$($VIObj.Version)] "
}
}
} Else {
$Header = ':: Not connected to Virtual Infra Services ::'
}
$Host.UI.RawUI.WindowTitle = $Header
} #EndFunction Set-PowerCLiTitle
New-Alias -Name Set-ViMPowerCLiTitle -Value Set-PowerCLiTitle -Force:$true
Filter Get-VMHostFirmwareVersion {
<#
.SYNOPSIS
Get ESXi host BIOS version.
.DESCRIPTION
This filter returns ESXi host BIOS/UEFI Version and Release Date as a single string.
.EXAMPLE
PS C:\> Get-VMHost 'esxprd1.*' |Get-VMHostFirmwareVersion
Get single ESXi host's Firmware version.
.EXAMPLE
PS C:\> Get-Cluster PROD |Get-VMHost |select Name,@{N='BIOS';E={$_ |Get-VMHostFirmwareVersion}}
Get ESXi Name and Firmware version for single cluster.
.EXAMPLE
PS C:\> Get-VMHost |sort Name |select Name,Version,Manufacturer,Model,@{N='BIOS';E={$_ |Get-VMHostFirmwareVersion}} |ft -au
Add calculated property, that will contain Firmware version for all registered ESXi hosts.
.EXAMPLE
PS C:\> Get-View -ViewType 'HostSystem' |select Name,@{N='BIOS';E={$_ |Get-VMHostFirmwareVersion}}
.EXAMPLE
PS C:\> 'esxprd1.domain.com','esxdev2' |Get-VMHostFirmwareVersion
.INPUTS
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]] Objects, returned by Get-VMHost cmdlet.
[VMware.Vim.HostSystem[]] Objects, returned by Get-View cmdlet.
[System.String[]] ESXi hostname or FQDN.
.OUTPUTS
[System.String[]] BIOS/UEFI version and release date.
.NOTES
Author: Roman Gelman.
Version 1.0 :: 09-Jan-2016 :: Release.
Version 1.1 :: 03-Aug-2016 :: Improvement :: GetType() method replaced by -is for type determine.
.LINK
http://www.ps1code.com/single-post/2016/1/9/How-to-know-ESXi-servers%E2%80%99-BIOSFirmware-version-using-PowerCLi
#>
Try
{
If ($_ -is [VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]) {$BiosInfo = ($_ |Get-View).Hardware.BiosInfo}
ElseIf ($_ -is [VMware.Vim.HostSystem]) {$BiosInfo = $_.Hardware.BiosInfo}
ElseIf ($_ -is [string]) {$BiosInfo = (Get-View -ViewType HostSystem -Filter @{"Name" = $_}).Hardware.BiosInfo}
Else {Throw "Not supported data type as pipeline"}
$fVersion = $BiosInfo.BiosVersion -replace ('^-\[|\]-$', $null)
$fDate = [Regex]::Match($BiosInfo.ReleaseDate, '(\d{1,2}/){2}\d+').Value
If ($fVersion) {return "$fVersion [$fDate]"} Else {return $null}
}
Catch
{}
} #EndFilter Get-VMHostFirmwareVersion
New-Alias -Name Get-ViMVMHostFirmwareVersion -Value Get-VMHostFirmwareVersion -Force:$true
Function Compare-VMHostSoftwareVib {
<#
.SYNOPSIS
Compares the installed VIB packages between VMware ESXi Hosts.
.DESCRIPTION
This function compares the installed VIB packages between reference ESXi Host and
group of difference/target ESXi Hosts or single ESXi Host.
.PARAMETER ReferenceVMHost
Reference VMHost.
.PARAMETER DifferenceVMHosts
Target VMHosts to compare them with the reference VMHost.
.EXAMPLE
PS C:\> Compare-VMHostSoftwareVib -ReferenceVMHost (Get-VMHost 'esxprd1.*') -DifferenceVMHosts (Get-VMHost 'esxprd2.*')
Compare two ESXi hosts.
.EXAMPLE
PS C:\> Get-VMHost 'esxdev2.*','esxdev3.*' |Compare-VMHostSoftwareVib -ReferenceVMHost (Get-VMHost 'esxdev1.*')
Compare two target ESXi Hosts with the reference Host.
.EXAMPLE
PS C:\> Get-Cluster DEV |Get-VMHost |Compare-VMHostSoftwareVib -ReferenceVMHost (Get-VMHost 'esxdev1.*')
Compare all HA/DRS cluster members with the reference ESXi Host.
.EXAMPLE
PS C:\> Get-Cluster PRD |Get-VMHost |Compare-VMHostSoftwareVib -ReferenceVMHost (Get-VMHost 'esxhai1.*') |Export-Csv -NoTypeInformation -Path '.\VibCompare.csv'
Export the comparison report to the file.
.INPUTS
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]] Objects, returned by Get-VMHost cmdlet.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author :: Roman Gelman.
Dependencies :: ESXCLI V2 works on vCenter 5.0/ESXi 5.0 and later.
Version 1.0 :: 10-Jan-2016 :: Release.
Version 1.1 :: 01-May-2016 :: Improvement :: Added support for PowerCLi 6.3R1 and ESXCLI V2 interface.
.LINK
http://www.ps1code.com/single-post/2016/1/10/How-to-compare-installed-VIB-packages-between-two-or-more-ESXi-hosts
#>
Param (
[Parameter(Mandatory,Position=1,HelpMessage="Reference VMHost")]
[Alias("ReferenceESXi")]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]$ReferenceVMHost
,
[Parameter(Mandatory,Position=2,ValueFromPipeline,HelpMessage="Difference VMHosts collection")]
[Alias("DifferenceESXi","DifferenceVMHosts")]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]]$DifferenceVMHost
)
Begin {
$PcliVer = Get-PowerCLIVersion -ErrorAction SilentlyContinue
$PcliMM = ($PcliVer.Major.ToString() + $PcliVer.Minor.ToString()) -as [int]
}
Process {
Try
{
If ($PcliMM -ge 63) {
$esxcliRef = Get-EsxCli -V2 -VMHost $ReferenceVMHost -ErrorAction Stop
$refVibId = ($esxcliRef.software.vib.list.Invoke()).ID
}
Else {
$esxcliRef = Get-EsxCli -VMHost $ReferenceVMHost -ErrorAction Stop
$refVibId = ($esxcliRef.software.vib.list()).ID
}
}
Catch
{
"{0}" -f $Error.Exception.Message
}
Foreach ($esx in $DifferenceVMHosts) {
Try
{
If ($PcliMM -ge 63) {
$esxcliDif = Get-EsxCli -V2 -VMHost $esx -ErrorAction Stop
$difVibId = ($esxcliDif.software.vib.list.Invoke()).ID
}
Else {
$esxcliDif = Get-EsxCli -VMHost $esx -ErrorAction Stop
$difVibId = ($esxcliDif.software.vib.list()).ID
}
$diffObj = Compare-Object -ReferenceObject $refVibId -DifferenceObject $difVibId -IncludeEqual:$false
Foreach ($diff in $diffObj) {
If ($diff.SideIndicator -eq '=>') {$diffOwner = $esx} Else {$diffOwner = $ReferenceVMHost}
$Properties = [ordered]@{
VIB = $diff.InputObject
VMHost = $diffOwner
}
$Object = New-Object PSObject -Property $Properties
$Object
}
}
Catch
{
"{0}" -f $Error.Exception.Message
}
}
}
} #EndFunction Compare-VMHostSoftwareVib
New-Alias -Name Compare-ViMVMHostSoftwareVib -Value Compare-VMHostSoftwareVib -Force:$true
Filter Get-VMHostBirthday {
<#
.SYNOPSIS
Get ESXi host installation date (Birthday).
.DESCRIPTION
This filter returns ESXi host installation date.
.EXAMPLE
PS C:\> Get-VMHost 'esxprd1.*' |Get-VMHostBirthday
Get single ESXi host's Birthday.
.EXAMPLE
PS C:\> Get-Cluster DEV |Get-VMHost |select Name,Version,@{N='Birthday';E={$_ |Get-VMHostBirthday}} |sort Name
Get ESXi Name and Birthday for single cluster.
.EXAMPLE
PS C:\> 'esxprd1.domain.com','esxprd2' |select @{N='ESXi';E={$_}},@{N='Birthday';E={$_ |Get-VMHostBirthday}}
Pipe hostnames (strings) to the function.
.EXAMPLE
PS C:\> Get-VMHost |select Name,@{N='Birthday';E={($_ |Get-VMHostBirthday).ToString('yyyy-MM-dd HH:mm:ss')}} |sort Name |ft -au
Format output using ToString() method.
http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/22/formatting-date-strings-with-powershell.aspx
.INPUTS
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]] Objects, returned by Get-VMHost cmdlet.
[System.String[]] ESXi hostname or FQDN.
.OUTPUTS
[System.DateTime[]] ESXi installation date/time.
.NOTES
Original idea: Magnus Andersson
Author: Roman Gelman
Requirements: vSphere 5.x or above
.LINK
http://vcdx56.com/2016/01/05/find-esxi-installation-date/
#>
Try
{
$EsxCli = Get-EsxCli -VMHost $_ -ErrorAction Stop
$Uuid = $EsxCli.system.uuid.get()
$bdHexa = [Regex]::Match($Uuid, '^(\w{8,})-').Groups[1].Value
$bdDeci = [Convert]::ToInt64($bdHexa, 16)
$bdDate = [TimeZone]::CurrentTimeZone.ToLocalTime(([DateTime]'1/1/1970').AddSeconds($bdDeci))
If ($bdDate) {return $bdDate} Else {return $null}
}
Catch
{ }
} #EndFilter Get-VMHostBirthday
New-Alias -Name Get-ViMVMHostBirthday -Value Get-VMHostBirthday -Force:$true
Function Enable-VMHostSSH {
<#
.SYNOPSIS
Enable SSH on all ESXi hosts in a cluster.
.DESCRIPTION
This function enables SSH on all ESXi hosts in a cluster.
It starts the SSH daemon and opens incoming TCP connections on port 22.
.EXAMPLE
PS C:\> Get-Cluster PROD |Enable-VMHostSSH
.EXAMPLE
PS C:\> Get-Cluster DEV,TEST |Enable-VMHostSSH |sort Cluster,VMHost |Format-Table -AutoSize
.INPUTS
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl[]] Clusters collection, returtned by Get-Cluster cmdlet.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author :: Roman Gelman.
Version 1.0 :: 07-Feb-2016 :: Release.
Version 1.1 :: 02-Aug-2016 :: -Cluster parameter data type changed to the portable type.
.LINK
http://www.ps1code.com/single-post/2016/02/07/How-to-enabledisable-SSH-on-all-ESXi-hosts-in-a-cluster-using-PowerCLi
#>
Param (
[Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$true)]
[ValidateNotNullorEmpty()]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster[]]$Cluster = (Get-Cluster)
)
Process {
Foreach ($container in $Cluster) {
Foreach ($esx in Get-VMHost -Location $container) {
If ('Connected','Maintenance' -contains $esx.ConnectionState -and $esx.PowerState -eq 'PoweredOn') {
$sshSvc = Get-VMHostService -VMHost $esx |? {$_.Key -eq 'TSM-SSH'} |Start-VMHostService -Confirm:$false -ErrorAction Stop
If ($sshSvc.Running) {$sshStatus = 'Running'} Else {$sshStatus = 'NotRunning'}
$fwRule = Get-VMHostFirewallException -VMHost $esx -Name 'SSH Server' |Set-VMHostFirewallException -Enabled $true -ErrorAction Stop
$Properties = [ordered]@{
Cluster = $container.Name
VMHost = $esx.Name
State = $esx.ConnectionState
PowerState = $esx.PowerState
SSHDaemon = $sshStatus
SSHEnabled = $fwRule.Enabled
}
}
Else {
$Properties = [ordered]@{
Cluster = $container.Name
VMHost = $esx.Name
State = $esx.ConnectionState
PowerState = $esx.PowerState
SSHDaemon = 'Unknown'
SSHEnabled = 'Unknown'
}
}
$Object = New-Object PSObject -Property $Properties
$Object
}
}
}
} #EndFunction Enable-VMHostSSH
New-Alias -Name Enable-ViMVMHostSSH -Value Enable-VMHostSSH -Force:$true
Function Disable-VMHostSSH {
<#
.SYNOPSIS
Disable SSH on all ESXi hosts in a cluster.
.DESCRIPTION
This function disables SSH on all ESXi hosts in a cluster.
It stops the SSH daemon and (optionally) blocks incoming TCP connections on port 22.
.PARAMETER BlockFirewall
Try to disable "SSH Server" firewall exception rule.
It might fail if this rule categorized as "Required Services" (VMware KB2037544).
.EXAMPLE
PS C:\> Get-Cluster PROD |Disable-VMHostSSH -BlockFirewall
.EXAMPLE
PS C:\> Get-Cluster DEV,TEST |Disable-VMHostSSH |sort Cluster,VMHost |Format-Table -AutoSize
.INPUTS
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl[]] Clusters collection, returtned by Get-Cluster cmdlet.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author :: Roman Gelman.
Version 1.0 :: 07-Feb-2016 :: Release.
Version 1.1 :: 02-Aug-2016 :: -Cluster parameter data type changed to the portable type.
.LINK
http://www.ps1code.com/single-post/2016/02/07/How-to-enabledisable-SSH-on-all-ESXi-hosts-in-a-cluster-using-PowerCLi
#>
Param (
[Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$true)]
[ValidateNotNullorEmpty()]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster[]]$Cluster = (Get-Cluster)
,
[Parameter(Mandatory=$false,Position=1)]
[Switch]$BlockFirewall
)
Process {
Foreach ($container in $Cluster) {
Foreach ($esx in Get-VMHost -Location $container) {
If ('Connected','Maintenance' -contains $esx.ConnectionState -and $esx.PowerState -eq 'PoweredOn') {
$sshSvc = Get-VMHostService -VMHost $esx |? {$_.Key -eq 'TSM-SSH'} |Stop-VMHostService -Confirm:$false -ErrorAction Stop
If ($sshSvc.Running) {$sshStatus = 'Running'} Else {$sshStatus = 'NotRunning'}
$fwRule = Get-VMHostFirewallException -VMHost $esx -Name 'SSH Server'
If ($BlockFirewall) {
Try {$fwRule = Set-VMHostFirewallException -Exception $fwRule -Enabled:$false -Confirm:$false -ErrorAction Stop}
Catch {}
}
$Properties = [ordered]@{
Cluster = $container.Name
VMHost = $esx.Name
State = $esx.ConnectionState
PowerState = $esx.PowerState
SSHDaemon = $sshStatus
SSHEnabled = $fwRule.Enabled
}
}
Else {
$Properties = [ordered]@{
Cluster = $container.Name
VMHost = $esx.Name
State = $esx.ConnectionState
PowerState = $esx.PowerState
SSHDaemon = 'Unknown'
SSHEnabled = 'Unknown'
}
}
$Object = New-Object PSObject -Property $Properties
$Object
}
}
}
} #EndFunction Disable-VMHostSSH
New-Alias -Name Disable-ViMVMHostSSH -Value Disable-VMHostSSH -Force:$true
Function Set-VMHostNtpServer {
<#
.SYNOPSIS
Set NTP server settings on a group of ESXi hosts.
.DESCRIPTION
This cmdlet sets NTP server settings on a group of ESXi hosts
and restarts the NTP daemon to apply these settings.
.PARAMETER VMHost
ESXi hosts.
.PARAMETER NewNtp
NTP servers (IP/Hostname).
.EXAMPLE
PS C:\> Set-VMHostNtpServer -NewNtp 'ntp1','ntp2'
Set two NTP servers to all hosts in inventory.
.EXAMPLE
PS C:\> Get-VMHost 'esx1.*','esx2.*' |Set-VMHostNtpServer -NewNtp 'ntp1','ntp2'
.EXAMPLE
PS C:\> Get-Cluster DEV,TEST |Get-VMHost |sort Parent,Name |Set-VMHostNtpServer -NewNtp 'ntp1','10.1.2.200' |ft -au
.EXAMPLE
PS C:\> Get-VMHost -Location Datacenter1 |sort Name |Set-VMHostNtpServer -NewNtp 'ntp1','ntp2' |epcsv -notype -Path '.\Ntp_report.csv'
Export the results to Excel.
.INPUTS
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]] VMHost collection returned by Get-VMHost cmdlet.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author :: Roman Gelman.
Version 1.0 :: 10-Mar-2016 :: Release.
.LINK
http://www.ps1code.com/single-post/2016/03/10/How-to-configure-NTP-servers-setting-on-ESXi-hosts-using-PowerCLi
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false,Position=1,ValueFromPipeline=$true)]
[ValidateNotNullorEmpty()]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]]$VMHost = (Get-VMHost)
,
[Parameter(Mandatory,Position=2)]
[System.String[]]$NewNtp
)
Begin {
$ErrorActionPreference = 'Stop'
}
Process {
Foreach ($esx in $VMHost) {
If ('Connected','Maintenance' -contains $esx.ConnectionState -and $esx.PowerState -eq 'PoweredOn') {
### Get current Ntp ###
$Ntps = Get-VMHostNtpServer -VMHost $esx
### Remove previously configured Ntp ###
$removed = $false
Try
{
Remove-VMHostNtpServer -NtpServer $Ntps -VMHost $esx -Confirm:$false
$removed = $true
}
Catch { }
### Add new Ntp ###
$added = $null
Try
{
$added = Add-VMHostNtpServer -NtpServer $NewNtp -VMHost $esx -Confirm:$false
}
Catch { }
### Restart NTP Daemon ###
$restarted = $false
Try
{
If ($added) {Get-VMHostService -VMHost $esx |? {$_.Key -eq 'ntpd'} |Restart-VMHostService -Confirm:$false |Out-Null}
$restarted = $true
}
Catch {}
### Return results ###
$Properties = [ordered]@{
VMHost = $esx
OldNtp = $Ntps
IsOldRemoved = $removed
NewNtp = $added
IsDaemonRestarted = $restarted
}
$Object = New-Object PSObject -Property $Properties
$Object
}
Else {Write-Warning "VMHost '$($esx.Name)' is in unsupported state"}
}
}
} #EndFunction Set-VMHostNtpServer
New-Alias -Name Set-ViMVMHostNtpServer -Value Set-VMHostNtpServer -Force:$true
Function Get-Version {
<#
.SYNOPSIS
Get VMware Virtual Infrastructure objects' version info.
.DESCRIPTION
This cmdlet gets VMware Virtual Infrastructure objects' version info.
.PARAMETER VIObject
Vitual Infrastructure objects (VM, VMHosts, DVSwitches, Datastores).
.PARAMETER VCenter
Get versions for all connected VCenter servers/ESXi hosts and PowerCLi version on the localhost.
.PARAMETER LicenseKey
Get versions of license keys.
.EXAMPLE
PS C:\> Get-VMHost |Get-Version |? {$_.Version -ge 5.5 -and $_.Version.Revision -lt 2456374}
Get all ESXi v5.5 hosts that have Revision less than 2456374.
.EXAMPLE
PS C:\> Get-View -ViewType HostSystem |Get-Version |select ProductName,Version |sort Version |group Version |sort Count |select Count,@{N='Version';E={$_.Name}},@{N='VMHost';E={($_.Group |select -expand ProductName) -join ','}} |epcsv -notype 'C:\reports\ESXi_Version.csv'
Group all ESXi hosts by Version and export the list to CSV.
.EXAMPLE
PS C:\> Get-VM |Get-Version |? {$_.FullVersion -match 'v10' -and $_.Version -gt 9.1}
Get all VM with Virtual Hardware v10 and VMTools version above v9.1.0.
.EXAMPLE
PS C:\> Get-Version -VCenter |Format-Table -AutoSize
Get all connected VCenter servers/ESXi hosts versions and PowerCLi version.
.EXAMPLE
PS C:\> Get-VDSwitch |Get-Version |sort Version |? {$_.Version -lt 5.5}
Get all DVSwitches that have version below 5.5.
.EXAMPLE
PS C:\> Get-Datastore |Get-Version |? {$_.Version.Major -eq 3}
Get all VMFS3 datastores.
.EXAMPLE
PS C:\> Get-Version -LicenseKey
Get license keys version info.
.INPUTS
Output objects from the following cmdlets:
Get-VMHost, Get-VM, Get-DistributedSwitch, Get-Datastore and Get-View -ViewType HostSystem.
.OUTPUTS
[System.Management.Automation.PSCustomObject] PSObject collection.
.NOTES
Author :: Roman Gelman.
Version 1.0 :: 23-May-2016 :: Release.
Version 1.1 :: 03-Aug-2016 :: Bugfix ::
[1] VDSwitch data type changed from [VMware.Vim.VmwareDistributedVirtualSwitch] to [VMware.VimAutomation.Vds.Types.V1.VmwareVDSwitch].
[2] Function Get-VersionVDSwitch edited to support data type change.
.LINK
http://www.ps1code.com/single-post/2016/05/25/How-to-know-any-VMware-object%E2%80%99s-version-Use-GetVersion
#>
[CmdletBinding(DefaultParameterSetName='VIO')]
Param (
[Parameter(Mandatory,Position=1,ValueFromPipeline=$true,ParameterSetName='VIO')]
$VIObject
,
[Parameter(Mandatory,Position=1,ParameterSetName='VC')]
[switch]$VCenter
,
[Parameter(Mandatory,Position=1,ParameterSetName='LIC')]
[switch]$LicenseKey
)
Begin {
$ErrorActionPreference = 'SilentlyContinue'
Function Get-VersionVMHostImpl {
Param ([Parameter(Mandatory,Position=1)]$InputObject)
$ErrorActionPreference = 'Stop'
Try
{
If ('Connected','Maintenance' -contains $InputObject.ConnectionState -and $InputObject.PowerState -eq 'PoweredOn') {
$ProductInfo = $InputObject.ExtensionData.Config.Product
$ProductVersion = [version]($ProductInfo.Version + '.' + $ProductInfo.Build)
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = $ProductInfo.Name
FullVersion = $ProductInfo.FullName
Version = $ProductVersion
}
}
Else {
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = 'VMware ESXi'
FullVersion = 'Unknown'
Version = [version]'0.0.0.0'
}
}
}
Catch
{
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = 'VMware ESXi'
FullVersion = 'Unknown'
Version = [version]'0.0.0.0'
}
}
Finally
{
$Object = New-Object PSObject -Property $Properties
$Object
}
} #EndFunction Get-VersionVMHostImpl
Function Get-VersionVMHostView {
Param ([Parameter(Mandatory,Position=1)]$InputObject)
$ErrorActionPreference = 'Stop'
Try
{
$ProductRuntime = $InputObject.Runtime
If ('connected','maintenance' -contains $ProductRuntime.ConnectionState -and $ProductRuntime.PowerState -eq 'poweredOn') {
$ProductInfo = $InputObject.Config.Product
$ProductVersion = [version]($ProductInfo.Version + '.' + $ProductInfo.Build)
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = $ProductInfo.Name
FullVersion = $ProductInfo.FullName
Version = $ProductVersion
}
}
Else {
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = 'VMware ESXi'
FullVersion = 'Unknown'
Version = [version]'0.0.0.0'
}
}
}
Catch
{
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = 'VMware ESXi'
FullVersion = 'Unknown'
Version = [version]'0.0.0.0'
}
}
Finally
{
$Object = New-Object PSObject -Property $Properties
$Object
}
} #EndFunction Get-VersionVMHostView
Function Get-VersionVM {
Param ([Parameter(Mandatory,Position=1)]$InputObject)
$ErrorActionPreference = 'Stop'
Try
{
$ProductInfo = $InputObject.Guest
If ($InputObject.ExtensionData.Guest.ToolsStatus -ne 'toolsNotInstalled' -and $ProductInfo) {
$ProductVersion = [version]$ProductInfo.ToolsVersion
$Properties = [ordered]@{
ProductName = $InputObject.Name
ProductType = $InputObject.ExtensionData.Config.GuestFullName #$ProductInfo.OSFullName
FullVersion = "VMware VM " + $InputObject.Version