From 2d594cb60d328ed9144d4071ea37b1c3bb40ddba Mon Sep 17 00:00:00 2001 From: William Lam Date: Sun, 20 Nov 2016 09:26:52 -0800 Subject: [PATCH] Adding several vSphere 6.5 API examples --- Scripts/AutomaticVMFSUnmap.ps1 | 68 ++++++++++++++++++++++++++++++++++ Scripts/ESXInstallDate.ps1 | 30 +++++++++++++++ Scripts/ESXInstalledVIBs.ps1 | 41 ++++++++++++++++++++ Scripts/SecureBoot.ps1 | 65 ++++++++++++++++++++++++++++++++ Scripts/vSphereLogins.ps1 | 29 +++++++++++++++ 5 files changed, 233 insertions(+) create mode 100755 Scripts/AutomaticVMFSUnmap.ps1 create mode 100755 Scripts/ESXInstallDate.ps1 create mode 100755 Scripts/ESXInstalledVIBs.ps1 create mode 100755 Scripts/SecureBoot.ps1 create mode 100755 Scripts/vSphereLogins.ps1 diff --git a/Scripts/AutomaticVMFSUnmap.ps1 b/Scripts/AutomaticVMFSUnmap.ps1 new file mode 100755 index 0000000..fa240d9 --- /dev/null +++ b/Scripts/AutomaticVMFSUnmap.ps1 @@ -0,0 +1,68 @@ +<# +.SYNOPSIS Retrieve the current VMFS Unmap priority for VMFS 6 datastore +.NOTES Author: William Lam +.NOTES Site: www.virtuallyghetto.com +.NOTES Reference: http://www.virtuallyghetto.com/2016/10/configure-new-automatic-space-reclamation-vmfs-unmap-using-vsphere-6-5-apis.html +.PARAMETER Datastore + VMFS 6 Datastore to enable or disable VMFS Unamp +.EXAMPLE + Get-Datastore "mini-local-datastore-hdd" | Get-VMFSUnmap +#> + +Function Get-VMFSUnmap { + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl]$Datastore + ) + + if($datastoreInfo -is [VMware.Vim.VmfsDatastoreInfo] -and $datastoreInfo.Vmfs.MajorVersion -eq 6) { + $datastoreInfo.Vmfs | select Name, UnmapPriority, UnmapGranularity + } else { + Write-Host "Not a VMFS Datastore and/or VMFS version is not 6.0" + } +} + +<# +.SYNOPSIS Configure the VMFS Unmap priority for VMFS 6 datastore +.NOTES Author: William Lam +.NOTES Site: www.virtuallyghetto.com +.NOTES Reference: http://www.virtuallyghetto.com/2016/10/configure-new-automatic-space-reclamation-vmfs-unmap-using-vsphere-6-5-apis.html +.PARAMETER Datastore + VMFS 6 Datastore to enable or disable VMFS Unamp +.EXAMPLE + Get-Datastore "mini-local-datastore-hdd" | Set-VMFSUnmap -Enabled $true +.EXAMPLE + Get-Datastore "mini-local-datastore-hdd" | Set-VMFSUnmap -Enabled $false +#> + +Function Set-VMFSUnmap { + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl]$Datastore, + [String]$Enabled + ) + + $vmhostView = ($Datastore | Get-VMHost).ExtensionData + $storageSystem = Get-View $vmhostView.ConfigManager.StorageSystem + + if($Enabled -eq $true) { + $enableUNMAP = "low" + $reconfigMessage = "Enabling Automatic VMFS Unmap for $Datastore" + } else { + $enableUNMAP = "none" + $reconfigMessage = "Disabling Automatic VMFS Unmap for $Datastore" + } + + $uuid = $datastore.ExtensionData.Info.Vmfs.Uuid + + Write-Host "$reconfigMessage ..." + $storageSystem.UpdateVmfsUnmapPriority($uuid,$enableUNMAP) +} \ No newline at end of file diff --git a/Scripts/ESXInstallDate.ps1 b/Scripts/ESXInstallDate.ps1 new file mode 100755 index 0000000..26a3edf --- /dev/null +++ b/Scripts/ESXInstallDate.ps1 @@ -0,0 +1,30 @@ +<# +.SYNOPSIS Retrieve the installation date of an ESXi host +.NOTES Author: William Lam +.NOTES Site: www.virtuallyghetto.com +.NOTES Reference: http://www.virtuallyghetto.com/2016/10/super-easy-way-of-getting-esxi-installation-date-in-vsphere-6-5.html +.PARAMETER Vmhost + ESXi host to query installation date +.EXAMPLE + Get-Vmhost "mini" | Get-ESXInstallDate +#> + +Function Get-ESXInstallDate { + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl]$Vmhost + ) + + if($Vmhost.Version -eq "6.5.0") { + $imageManager = Get-View ($Vmhost.ExtensionData.ConfigManager.ImageConfigManager) + $installDate = $imageManager.installDate() + + Write-Host "$Vmhost was installed on $installDate" + } else { + Write-Host "ESXi must be running 6.5" + } +} diff --git a/Scripts/ESXInstalledVIBs.ps1 b/Scripts/ESXInstalledVIBs.ps1 new file mode 100755 index 0000000..c759f73 --- /dev/null +++ b/Scripts/ESXInstalledVIBs.ps1 @@ -0,0 +1,41 @@ +<# +.SYNOPSIS Retrieve the installation date of an ESXi host +.NOTES Author: William Lam +.NOTES Site: www.virtuallyghetto.com +.PARAMETER Vmhost + ESXi host to query installed ESXi VIBs +.EXAMPLE + Get-ESXInstalledVibs -Vmhost (Get-Vmhost "mini") +.EXAMPLE + Get-ESXInstalledVibs -Vmhost (Get-Vmhost "mini") -vibname vsan +#> + +Function Get-ESXInstalledVibs { + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl]$Vmhost, + [Parameter( + Mandatory=$false, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [String]$vibname="" + ) + + $imageManager = Get-View ($Vmhost.ExtensionData.ConfigManager.ImageConfigManager) + $vibs = $imageManager.fetchSoftwarePackages() + + foreach ($vib in $vibs) { + if($vibname -ne "") { + if($vib.name -eq $vibname) { + return $vib | select Name, Version, Vendor, CreationDate, Summary + } + } else { + $vib | select Name, Version, Vendor, CreationDate, Summary + } + } +} diff --git a/Scripts/SecureBoot.ps1 b/Scripts/SecureBoot.ps1 new file mode 100755 index 0000000..dac71b8 --- /dev/null +++ b/Scripts/SecureBoot.ps1 @@ -0,0 +1,65 @@ +Function Get-SecureBoot { + <# + .SYNOPSIS Query Seure Boot setting for a VM in vSphere 6.5 + .NOTES Author: William Lam + .NOTES Site: www.virtuallyghetto.com + .PARAMETER Vm + VM to query Secure Boot setting + .EXAMPLE + Get-VM -Name Windows10 | Get-SecureBoot + #> + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl]$Vm + ) + + $secureBootSetting = if ($vm.ExtensionData.Config.BootOptions.EfiSecureBootEnabled) { "enabled" } else { "disabled" } + Write-Host "Secure Boot is" $secureBootSetting +} + +Function Set-SecureBoot { + <# + .SYNOPSIS Enable/Disable Seure Boot setting for a VM in vSphere 6.5 + .NOTES Author: William Lam + .NOTES Site: www.virtuallyghetto.com + .PARAMETER Vm + VM to enable/disable Secure Boot + .EXAMPLE + Get-VM -Name Windows10 | Set-SecureBoot -Enabled + .EXAMPLE + Get-VM -Name Windows10 | Set-SecureBoot -Disabled + #> + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl]$Vm, + [Switch]$Enabled, + [Switch]$Disabled + ) + + if($Enabled) { + $secureBootSetting = $true + $reconfigMessage = "Enabling Secure Boot for $Vm" + } + if($Disabled) { + $secureBootSetting = $false + $reconfigMessage = "Disabling Secure Boot for $Vm" + } + + $spec = New-Object VMware.Vim.VirtualMachineConfigSpec + $bootOptions = New-Object VMware.Vim.VirtualMachineBootOptions + $bootOptions.EfiSecureBootEnabled = $secureBootSetting + $spec.BootOptions = $bootOptions + + Write-Host "`n$reconfigMessage ..." + $task = $vm.ExtensionData.ReconfigVM_Task($spec) + $task1 = Get-Task -Id ("Task-$($task.value)") + $task1 | Wait-Task | Out-Null +} diff --git a/Scripts/vSphereLogins.ps1 b/Scripts/vSphereLogins.ps1 new file mode 100755 index 0000000..516e4c7 --- /dev/null +++ b/Scripts/vSphereLogins.ps1 @@ -0,0 +1,29 @@ +Function Get-vSphereLogins { + <# + .SYNOPSIS Retrieve information for all currently logged in vSphere Sessions (excluding current session) + .NOTES Author: William Lam + .NOTES Site: www.virtuallyghetto.com + .REFERENCE Blog: http://www.virtuallyghetto.com/2016/11/an-update-on-how-to-retrieve-useful-information-from-a-vsphere-login.html + .EXAMPLE + Get-vSphereLogins + #> + if($DefaultVIServers -eq $null) { + Write-Host "Error: Please connect to your vSphere environment" + exit + } + + # Using the first connection + $VCConnection = $DefaultVIServers[0] + + $sessionManager = Get-View ($VCConnection.ExtensionData.Content.SessionManager) + + # Store current session key + $currentSessionKey = $sessionManager.CurrentSession.Key + + foreach ($session in $sessionManager.SessionList) { + # Ignore vpxd-extension logins as well as the current session + if($session.UserName -notmatch "vpxd-extension" -and $session.key -ne $currentSessionKey) { + $session | Select Username, IpAddress, UserAgent, @{"Name"="APICount";Expression={$Session.CallCount}}, LoginTime + } + } +}