giovedì 29 maggio 2014

VMware: Automate support bundle collection using PowerCLI

When troubleshooting ESXi issues, especially when dealing with VMware technical support, you (almost) always will be asked to provide support bundles for ESXi host(s) and/or the managing vCenter Server.

Support Bundles can be easily generated by accessing vSphere Client -> File -> Export -> Export System Logs although the process is manual and potentially time consuming.


As almost everything in VMware environment even the support bundle collection process can be easily automated using PowerCLI.
The Get-Log PowerCLI cmdlet is used to retrieve support bundle either from vCenter Server either from ESXi hosts.

When connected to a vCenter Server running:

 Get-Log -Bundle -DestinationPath C:\Users\Paolo\Desktop  

will download in specified location support bundle for vCenter Server.

Support bundle will be named: vcsupport-<Generated_ID>.zip

To download support bundles for specific ESXi host(s) only:

 Get-Log -VMHost (Get-VMHost -Name 192.168.243.144) -Bundle -DestinationPath C:\Users\Paolo\Desktop  

where 192.168.243.144 is the IP address of ESXi host to download support bundle from.

This time file will be named: vmsupport-<Generated_ID>.tgz

To automate bundles retrieval for any ESXi host managed by a specific vCenter Server, including support bundle for vCenter itself, you can use the following script:



As usual you can also find the script above in my GitHub page: DownloadSupportBundles.ps1

Support bundle download is also achieved by simply connecting, using a web browser, to an ESXi host to the following address:

 https://<username>:<password>@<ESXi_host_IP_address>/cgi-bin/vm-support.cgi  

The previous step can be automated using PowerShell (not PowerCLI). This could be useful when in need to get support bundles rapidly using a PC/server where PowerCLI is not installed (only PowerShell is needed).



That's all!!

lunedì 19 maggio 2014

VMware: A PowerCLI module to massively backup/restore ESXi hosts configurations

Flexibility is one of the greatest advantages of ESXi. Almost every aspect can be customized and tuned using both basic and advanced configurations in order to achieve a custom tailored system.

Configuring settings is a time consuming process, networking configurations for virtual standard switches, iSCSI vmkernel, port binding, NTP configuration, etc.

In case of host reinstall you can save precious time by using a great PowerCLI cmdlet: Get-VMHostFirmware.

This cmdlet creates a tar compressed archive containing all ESXi host's configurations. Conversely, to recover a backupped configuration, Set-VMHostFirmware cmdlet is used.

In this blog post I provide a PowerCLI module that will allow you to backup, and eventually restore, ESXi hosts configurations.

This module uses Get-VMHostFirmware and Set-VMHostFirmware cmdlets introducing the possibility to pass more than a single host as source for backup or target for restore and automatically enters/exits each host into/from maintenance mode before and after a backup restore occurs.

Let's start by briefly explaining how to use the module:

Typically the first step is to connect to a vCenter Server via PowerCLI in order to be able to perform backup or restores of one or more vCenter registered hosts. PowerCLI connection to a single ESXi host is also supported but for obvious reasons you can backup/restore only that specific host.

Once downloaded the script provided below you will have a .psm1 file, which is the common extension for PowerShell modules, that must be imported into PowerCLI in order to use it.

 Import-Module C:\Users\Paolo\WindowsPowerShell\Modules\BackupRestore  

Where C:\Users\Paolo\WindowsPowerShell\Modules\ is the path of BackupRestore.psm1 file on your PC.

BackupRestore module introduces into PowerCLI two new functions: Backup-VMHost and Restore-VMHost.

Backup-VMHost requires as mandatory parameters:

-VMHost: backup source. IP address or FQDN of one or more ESXi hosts you want to backup configurations from.
-FilePath: location where configuration bundles will be saved.

The following example backups the configuration of host 192.168.243.143 and 192.168.243.144 then save their configurations into C:\Users\Paolo\Desktop.

 Backup-VMHost -VMHost 192.168.243.143,192.168.243.144 -FilePath C:\Users\Paolo\Desktop  


Restore-VMHost requires:

-VMHost: Restore destination. IP address or FQDN of one or more ESXi hosts you want to recover configurations to.

-FilePath: location where configuration bundles can be fetched in order to be restored on host(s)

-HostUsername: ESXi host username

-HostPassword: ESXi host password

The following command will first place each host into maintenance mode then restore configuration bundle on ESXi host 192.168.243.143 and 192.168.243.144 taking it from C:\Users\Paolo\Desktop folder.

By default configuration bundles are saved as configBundle-<ESXi_host_IP_address>.tar (for example: configBundle-192.168.243.143.tar) so Restore-VMHost function expects such named files to be present in source folder.
Finally it will wait a few minutes (3 by default), giving to each host time to perform a reboot, then removes host from maintenance mode.

 Restore-VMHost -VMHost 192.168.243.143,192.168.243.144 -FilePath C:\Users\Paolo\Desktop -HostUsername root -HostPassword vmware  


As usual this code is also available on my GitHub repository: BackupRestore.psm1



That's all!!

lunedì 12 maggio 2014

VMware: vCloud tenants reports using jQuery Mobile and PowerCLI

In this post we will create a simple HTML report using PowerCLI for Tenants and jQuery Mobile. jQuery Mobile provides an HTML5 user interface designed to allow the creation of web pages specifically suited for mobile devices with smaller screen compared to classic desktop/laptop PCs.

From PowerCLI perspective the following script is really simple, it retrieve some data from the vCloud environment then generates the HTML report by iterating HTML code (precisely <li> blocks) in order to create vApps list then finally it outputs the result as a single HTML by concatenating the composing elements.

As for jQuery Mobile perspective this is not complex at all, the resulting page structure is linear. In the header section there is the declaration of jQuery Mobile usage:

 <head>  
 <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />  
 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
 <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>  
 </head>  

The body is constructed in a block approach: several blocks with different functions while the body block is the main elements that is filled by different informations regarding cloud organization:

 <div data-role="page" id="home"> --Page  
   <div data-role="header">  
     HEADER BLOCK  
   </div>  
   <div data-role="content">  
     CONTENT BLOCK  
   </div>  
   <div data-role="footer">  
     FOOTER BLOCK  
   </div>  
 </div>  

The PowerCLI script is intended to be run by vCloud tenants, having enough privileges on the organization they manage, against a vCloud Director server. The expected output will be an HTML page that can be consumed by any HTML5 capable browser, but, as previously said, jQuery Mobile does its best when used on a mobile device like a tablet or a smartphone.

Results will be similar to these:



The PowerCLI code to create this report is the following. As usual you can also find it in my GitHub repository: Tenants Mobile Reports.ps1



That's all!!