Merge pull request #33 from lamw/master

Adding several vSphere 6.5 API examples
This commit is contained in:
Alan Renouf
2016-11-20 20:51:39 -08:00
committed by GitHub
5 changed files with 235 additions and 0 deletions

70
Scripts/AutomaticVMFSUnmap.ps1 Executable file
View File

@@ -0,0 +1,70 @@
<#
.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
)
$datastoreInfo = $Datastore.ExtensionData.Info
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)
}

30
Scripts/ESXInstallDate.ps1 Executable file
View File

@@ -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"
}
}

41
Scripts/ESXInstalledVIBs.ps1 Executable file
View File

@@ -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
}
}
}

65
Scripts/SecureBoot.ps1 Executable file
View File

@@ -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
}

29
Scripts/vSphereLogins.ps1 Executable file
View File

@@ -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
}
}
}