Rescan VMware vSphere Hypervisor (ESXi) using PowerCLI simplified

While preparing some disaster recovery (DR) tests, I had to add and remove a couple of LUNS from several ESX hosts in different clusters. Doing so,  I had to rescan a lot of host bus adapters (HBA) several times. As I hate doing repetitive tasks in a graphical user interface (GUI), I always find myself quickly turning to the PowerCLI console. The PowerCLI command to rescan an ESX host is somewhere hidden in the Get-VMHostStorage cmdlet. This isn’t the most logical location and every time I have to perform a rescan operation I have to search for the correct command line syntax.

Today I decided to simplify that process and created a little PowerCLI function to simplify this task. The function I created is called Rescan-VMHost and accepts an ESX host name or host object as input parameter.

Rescan-VMHost ESX01

The function also accepts host names or host objects from the pipeline. The optional -IncludePeers switch rescans all hosts in the given ESX host’s cluster additionally.

Get-VMHost ESX01 | Rescan-VMHost -IncludePeers

As an extra bonus feature the function also accepts a cluster object as input parameter and will rescan all ESX hosts in the given cluster.

Get-Cluster CL01 | Rescan-VMHost

Pretty cool huh?
I’ve already used this little function a lot and hope you find a use case for it too.

One last thing. Although the title of this post might suggest that this function works on ESXi only, that is NOT the case. The function will work on both ESXi and ESX, but since VMware decided to go solely with ESXi and refers to it as the VMware vSphere Hypervisor, I decided to follow that path and hence the post’s title.

function Rescan-VMHost {
  Performs a rescan operation on a specified ESX host or cluster
  This function rescans an ESX host or it's complete cluster when the -IncludePeers parameter is specified.
  Author: Arnim van Lieshout,
  Specifies the hostname, host object or cluster object to perform the rescan operation on.
.PARAMETER IncludePeers
  Specifies that all the hosts in the host's cluster should be rescanned instead of only the host itself.
  PS> Rescan-VMHost ESX001
  PS> Get-VMHost ESX00* | Rescan-VMHost -IncludePeers
  PS> Get-Cluster CL01 | Rescan-VMHost

	Param (
		[parameter(valuefrompipeline = $true, mandatory = $true,
		HelpMessage = "Enter an ESX(i) entity")]

	process {
		switch ($Entity.gettype().name) {
      		"String" {
				if ($IncludePeers) {Get-VMHost -Name $Entity | Rescan-VMHost -IncludePeers}
				else {Get-VMHost -Name $Entity | Rescan-VMHost}
      		"VMHostImpl" {
				if ($IncludePeers) {
					foreach ($VMHost in Get-Cluster $Entity.Parent | Get-VMHost) {
						$VMHost | Get-VMHostStorage -RescanAllHba
				else {
					$Entity | Get-VMHostStorage -RescanAllHba
      		"ClusterImpl" {
				foreach ($VMHost in $Entity | Get-VMHost) {
					$VMHost | Get-VMHostStorage -RescanAllHba
      		default {throw "No valid object type for parameter -VMHost specified"}

Related posts:

  1. ESXi Tech Support Mode Tweet As a security recommendation you should always disable Tech Support Mode (TSM) on your ESXi servers, but sometimes it’s helpful if you’re able to connect to your ESXi server...
  2. PowerCLI 4.1 namespace changes Tweet Output type changes in PowerCLI 4.1In PowerCLI 4.1 VMware changed the namespaces of the output types. According to VMware, this was done to improve the internal structure and enable...
  3. Collect VMware ESX Host PCI Device Information Tweet Whenever you need to install a new box with ESX, there’s the struggle with matching physical ports to VMware devices. Which network adapter becomes vmnic0?, Which hostbus adapter becomes...
  4. ESXCLI the PowerCLI way Tweet When trying to perform a Round Robin (RR) test on my vSphere 4.1 environment, I needed to know the number of paths available to the luns of the test...
  5. List HBA WWPNs and LUNs using Powershell Tweet Lately I’m moving around my VMs and storage luns between my ESX clusters a lot to accomplish a complete redesign of my Virtual Infrastructure. At some point I got...

2 Comments on “Rescan VMware vSphere Hypervisor (ESXi) using PowerCLI simplified”

  1. #1 marc
    on Apr 17th, 2011 at 11:17 am

    Hi Arnim,

    Quick question for you. Based on your experience, how long should a simple “get-vm” command take to run on its FIRST launch? Would more than 20 seconds typically be excessive for a small environment with less than 10 VMs in the vCenter inventory?

    What can be done to optimise the speed of specifically the very first time “get-vm” is invoked? (further get-vms are quick, just the very first one is slow on my system).

    I’m looking for insight around how to construct a high performing environment so that get-vm runs very fast, even the very first time its executed?


  2. #2 Arnim van Lieshout
    on Apr 17th, 2011 at 1:03 pm

    The speed of the Get-VM cmdlet totally depends on your environment. In larger environments the Get-VM cmdlet can be rather slow. If you want to optimize for speed, you’ll need to use the SDK directly using the Get-View cmdlet. Remember that the Get-View cmdlet returns SDK objects. For a VM it will return a “VirtualMachine” object whereas Get-VM will return a “VirtualMachineImpl” object.
    The “VirtualMachine” SDK object is normally accesible under the .ExtensionData property of the “VirtualMachineImpl” object.

    Instead of:
    $vms = Get-VM
    $vms = Get-View -ViewType VirtualMachine

    Instead of:
    $vm = Get-VM MyVM
    $vm = Get-View -ViewType VirtualMachine -Filter @{“Name” = “MyVM”}

Leave a Comment