From e928f5b56abe0551f27fece91d95f17ec47def61 Mon Sep 17 00:00:00 2001 From: Alan Renouf Date: Thu, 10 Nov 2016 16:12:42 -0800 Subject: [PATCH 1/5] removed connection info and commented out examples removed connection info and commented out examples --- .../vRealize Operations Maintenance Mode.ps1 | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Scripts/vRealize Operations Maintenance Mode.ps1 b/Scripts/vRealize Operations Maintenance Mode.ps1 index a6ff9e5..d216e31 100644 --- a/Scripts/vRealize Operations Maintenance Mode.ps1 +++ b/Scripts/vRealize Operations Maintenance Mode.ps1 @@ -1,6 +1,3 @@ -Connect-VIServer vc-l-01a.corp.local -User administrator@vsphere.local -Password VMware1! -Connect-OMServer vrops.corp1.local -User admin -Password VMware1! - function Enable-OMMaintenance { <# .NOTES @@ -96,21 +93,21 @@ function Disable-OMMaintenance { } } -Write-Host "Enable a single host as being in maintenance mode for 1 minute" -Enable-OMMaintenance -Resource ESX-01a* -MaintenanceTime 1 +#Write-Host "Enable a single host as being in maintenance mode for 1 minute" +#Enable-OMMaintenance -Resource ESX-01a* -MaintenanceTime 1 -Write-Host "List All Host Resources and their state" -Get-OMResource ESX-* | Select Name, State | FT +#Write-Host "List All Host Resources and their state" +#Get-OMResource ESX-* | Select Name, State | FT -Write-Host "Set All VMs with a name as backup as being in maintenance mode for 20 minutes" -Get-VM backup* | Enable-OMMaintenance -MaintenanceTime 20 +#Write-Host "Set All VMs with a name as backup as being in maintenance mode for 20 minutes" +#Get-VM backup* | Enable-OMMaintenance -MaintenanceTime 20 -Write-Host "List All Backup VM Resources and their state" -Get-VM backup* | Get-OMResource | Select Name, State | FT +#Write-Host "List All Backup VM Resources and their state" +#Get-VM backup* | Get-OMResource | Select Name, State | FT -Write-Host "Disable maintenance mode for all VMs with a name as backup as we have completed our scheduled work" -Get-VM backup* | Disable-OMMaintenance +#Write-Host "Disable maintenance mode for all VMs with a name as backup as we have completed our scheduled work" +#Get-VM backup* | Disable-OMMaintenance -Write-Host "List All VM Resources and their state" -Get-VM backup* | Get-OMResource | Select Name, State | FT +#Write-Host "List All VM Resources and their state" +#Get-VM backup* | Get-OMResource | Select Name, State | FT From 5d475bbd1be456bd26ff9e67b4cf40b330602cae Mon Sep 17 00:00:00 2001 From: Brian Graf Date: Fri, 18 Nov 2016 08:33:35 -0700 Subject: [PATCH 2/5] Initial Commit Backup-VCSA 6.5 --- Modules/Backup-VCSA.psm1 | 201 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 Modules/Backup-VCSA.psm1 diff --git a/Modules/Backup-VCSA.psm1 b/Modules/Backup-VCSA.psm1 new file mode 100644 index 0000000..21e036d --- /dev/null +++ b/Modules/Backup-VCSA.psm1 @@ -0,0 +1,201 @@ +Function Backup-VCSAToFile { +<# + .NOTES + =========================================================================== + Created by: Brian Graf + Date: October 30, 2016 + Organization: VMware + Blog: www.vtagion.com + Twitter: @vBrianGraf + =========================================================================== + + .SYNOPSIS + This function will allow you to create a full or partial backup of your + VCSA appliance. (vSphere 6.5 and higher) + + .DESCRIPTION + Use this function to backup your VCSA to a remote location + + .EXAMPLE + [VMware.VimAutomation.Cis.Core.Types.V1.Secret]$BackupPassword = "VMw@re123" + $Comment = "First API Backup" + $LocationType = "FTP" + $location = "10.144.99.5/vcsabackup-$((Get-Date).ToString('yyyy-MM-dd-hh-mm'))" + $LocationUser = "admin" + [VMware.VimAutomation.Cis.Core.Types.V1.Secret]$locationPassword = "VMw@re123" + PS C:\> Backup-VCSAToFile -BackupPassword $BackupPassword -LocationType $LocationType -Location $location -LocationUser $LocationUser -LocationPassword $locationPassword -Comment "This is a demo" -ShowProgress -FullBackup + + + .NOTES + You must be connected to the CisService for this to work, if you are not connected, the function will prompt you for your credentials + If a -LocationType is not chosen, the function will default to FTP. + The destination location for a backup must be an empty folder (easiest to use the get-date cmdlet in the location) + -ShowProgress will give you a progressbar as well as updates in the console + -SeatBackup will only backup the config whereas -Fullbackup grabs the historical data as well +#> + param ( + [Parameter(ParameterSetName=’FullBackup’)] + [switch]$FullBackup, + [Parameter(ParameterSetName=’SeatBackup’)] + [switch]$SeatBackup, + [ValidateSet('FTPS', 'HTTP', 'SCP', 'HTTPS', 'FTP')] + $LocationType = "FTP", + $Location, + $LocationUser, + [VMware.VimAutomation.Cis.Core.Types.V1.Secret]$LocationPassword, + [VMware.VimAutomation.Cis.Core.Types.V1.Secret]$BackupPassword, + $Comment = "Backup job", + [switch]$ShowProgress + ) + Begin { + if (!($global:DefaultCisServers)){ + [System.Windows.Forms.MessageBox]::Show("It appears you have not created a connection to the CisServer. You will now be prompted to enter your vCenter credentials to continue" , "Connect to CisServer") | out-null + $Connection = Connect-CisServer $global:DefaultVIServer + } else { + $Connection = $global:DefaultCisServers + } + if ($FullBackup) {$parts = @("common","seat")} + if ($SeatBackup) {$parts = @("seat")} + } + Process{ + $BackupAPI = Get-CisService com.vmware.appliance.recovery.backup.job + $CreateSpec = $BackupAPI.Help.create.piece.CreateExample() + $CreateSpec.parts = $parts + $CreateSpec.backup_password = $BackupPassword + $CreateSpec.location_type = $LocationType + $CreateSpec.location = $Location + $CreateSpec.location_user = $LocationUser + $CreateSpec.location_password = $LocationPassword + $CreateSpec.comment = $Comment + try { + $BackupJob = $BackupAPI.create($CreateSpec) + } + catch { + Write-Error $Error[0].exception.Message + } + + + If ($ShowProgress){ + do { + $BackupAPI.get("$($BackupJob.ID)") | select id, progress, state + $progress = ($BackupAPI.get("$($BackupJob.ID)").progress) + Write-Progress -Activity "Backing up VCSA" -Status $BackupAPI.get("$($BackupJob.ID)").state -PercentComplete ($BackupAPI.get("$($BackupJob.ID)").progress) -CurrentOperation "$progress% Complete" + start-sleep -seconds 5 + } until ($BackupAPI.get("$($BackupJob.ID)").progress -eq 100 -or $BackupAPI.get("$($BackupJob.ID)").state -ne "INPROGRESS") + + $BackupAPI.get("$($BackupJob.ID)") | select id, progress, state + } + Else { + $BackupJob | select id, progress, state + } + } + End {} +} + +Function Get-VCSABackupJobs { +<# + .NOTES + =========================================================================== + Created by: Brian Graf + Date: October 30, 2016 + Organization: VMware + Blog: www.vtagion.com + Twitter: @vBrianGraf + =========================================================================== + + .SYNOPSIS + Get-VCSABackupJobs returns a list of all backup jobs VCSA has ever performed (vSphere 6.5 and higher) + + .DESCRIPTION + Get-VCSABackupJobs returns a list of all backup jobs VCSA has ever performed + + .EXAMPLE + PS C:\> Get-VCSABackupJobs + + .NOTES + The values returned are read as follows: + YYYYMMDD-hhmmss-vcsabuildnumber + You can pipe the results of this function into the Get-VCSABackupStatus function + Get-VCSABackupJobs | select -First 1 | Get-VCSABackupStatus <- Most recent backup +#> + param ( + [switch]$ShowNewest + ) + Begin { + if (!($global:DefaultCisServers)){ + [System.Windows.Forms.MessageBox]::Show("It appears you have not created a connection to the CisServer. You will now be prompted to enter your vCenter credentials to continue" , "Connect to CisServer") | out-null + $Connection = Connect-CisServer $global:DefaultVIServer + } else { + $Connection = $global:DefaultCisServers + } + } + Process{ + + $BackupAPI = Get-CisService com.vmware.appliance.recovery.backup.job + + try { + if ($ShowNewest) { + $results = $BackupAPI.list() + $results[0] + } else { + $BackupAPI.list() + } + } + catch { + Write-Error $Error[0].exception.Message + } + + } + + End {} +} + +Function Get-VCSABackupStatus { +<# + .NOTES + =========================================================================== + Created by: Brian Graf + Date: October 30, 2016 + Organization: VMware + Blog: www.vtagion.com + Twitter: @vBrianGraf + =========================================================================== + + .SYNOPSIS + Returns the ID, Progress, and State of a VCSA backup (vSphere 6.5 and higher) + + .DESCRIPTION + Returns the ID, Progress, and State of a VCSA backup + + .EXAMPLE + PS C:\> $backups = Get-VCSABackupJobs + $backups[0] | Get-VCSABackupStatus + + .NOTES + The BackupID can be piped in from the Get-VCSABackupJobs function and can return multiple job statuses +#> + Param ( + [parameter(ValueFromPipeline=$True)] + [string[]]$BackupID + ) + Begin { + if (!($global:DefaultCisServers)){ + [System.Windows.Forms.MessageBox]::Show("It appears you have not created a connection to the CisServer. You will now be prompted to enter your vCenter credentials to continue" , "Connect to CisServer") | out-null + $Connection = Connect-CisServer $global:DefaultVIServer + } else { + $Connection = $global:DefaultCisServers + } + + $BackupAPI = Get-CisService com.vmware.appliance.recovery.backup.job + } + Process{ + + foreach ($id in $BackupID) { + $BackupAPI.get("$id") | select id, progress, state + } + + + } + + End {} +} From 181add5bfd96db238a945642407bbbecdeba37e5 Mon Sep 17 00:00:00 2001 From: Brian Graf Date: Fri, 18 Nov 2016 09:15:43 -0700 Subject: [PATCH 3/5] Added Credit to Alan --- Modules/Backup-VCSA.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/Backup-VCSA.psm1 b/Modules/Backup-VCSA.psm1 index 21e036d..271a7ff 100644 --- a/Modules/Backup-VCSA.psm1 +++ b/Modules/Backup-VCSA.psm1 @@ -27,6 +27,7 @@ .NOTES + Credit goes to @AlanRenouf for sharing the base of this function with me which I was able to take and make more robust as well as add in progress indicators You must be connected to the CisService for this to work, if you are not connected, the function will prompt you for your credentials If a -LocationType is not chosen, the function will default to FTP. The destination location for a backup must be an empty folder (easiest to use the get-date cmdlet in the location) From 2d594cb60d328ed9144d4071ea37b1c3bb40ddba Mon Sep 17 00:00:00 2001 From: William Lam Date: Sun, 20 Nov 2016 09:26:52 -0800 Subject: [PATCH 4/5] 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 + } + } +} From fed74d9e02879f4854847b6f283bdbd477c7f309 Mon Sep 17 00:00:00 2001 From: William Lam Date: Sun, 20 Nov 2016 12:53:42 -0800 Subject: [PATCH 5/5] Fixing VMFS UNMAP sample --- Scripts/AutomaticVMFSUnmap.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Scripts/AutomaticVMFSUnmap.ps1 b/Scripts/AutomaticVMFSUnmap.ps1 index fa240d9..2f32617 100755 --- a/Scripts/AutomaticVMFSUnmap.ps1 +++ b/Scripts/AutomaticVMFSUnmap.ps1 @@ -19,6 +19,8 @@ Function Get-VMFSUnmap { [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 { @@ -65,4 +67,4 @@ Function Set-VMFSUnmap { Write-Host "$reconfigMessage ..." $storageSystem.UpdateVmfsUnmapPriority($uuid,$enableUNMAP) -} \ No newline at end of file +}