Catching Virtual Machine questions with PowerCLI

As part of our VM-template, template-VM conversion process at work (which I’ve automated via a webpage as discussed in my previous post “Executing Powershell using PHP and IIS“), I had to find a way to handle the VM question “This VM has questions that must be answered before the operation can continue” when attempting to power the VM on via Start-VM.

I found that handling the question was not possible using a simple try/catch block (the explanation can be found in this excellent post by Clint Bergman). I was however able to catch it using the following block:

try
{
	try
	{
		Start-VM -VM $serverName -ErrorAction Stop -ErrorVariable custErr
	}
	catch [System.Management.Automation.ActionPreferenceStopException]
	{
		throw $_.Exception
	}
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.VMBlockedByQuestionException]
{
	Write-Output &quot;Power on operation triggered a VMBlockedByQuestionException. Answering question with <code>&quot;I moved it</code>&quot;. &lt;br /&gt;&quot;
	Get-VMQuestion -VM $serverName | Set-VMQuestion –Option &quot;I moved it&quot; -Confirm:$false	
}

It may seem like a lot of work to convert between a VM and template (and vice versa) but in our environment there are a couple of critical steps to the conversation process that must be followed to ensure deploys from these templates don’t break. As there are users outside of the team who update various templates I wanted to provide an easy way for everyone to ensure consistency when doing this, and the best way to do this is of course, automation :)

2 thoughts on “Catching Virtual Machine questions with PowerCLI

  1. Just because they can, and VMWare appears to have adjusted the options within ESXi for this.

    The three options that appear in my 5.5 lab are “Cancel”, “button.uuid.movedTheVM”, and “button.uuid.copiedTheVM”.

    These changes make absolutely no sense, but whatever, nothing like confusing the IT crowd to all corners of the Earth by making unwanted changes to core technologies. Awesome sauce.

    When attempting to search the web for reasons why the VMs won’t power on through PowerCLI, this page is the top search on all of the engines. Much appreciated on the excellent write-up. I just thought I’d add the updated options that “Set-VMQuestion” is expecting if other people get hit by this as well.

Leave a Reply