Answering VM Questions With PowerShell

Because there is an answer for everything and for everything that answer is PowerShell. Sometimes in your Virtual Infrastructure, you will have a need to answer a question or two. Normally these questions are put to you by vCenter: “Did you copy or move this VM?”, “Is today your birthday?”, “Who shot Kennedy?”.

For Example:

image

So I had to stage this screenshot, but it’s something that will happen when you have VMs that have the same UUID in your vCenter. Straight forward, no? The right answer in this case, since I have two of these VMs is to “Create”. Two clicks, done.

What if you had to do this for 300 VMs? A bit more cumbersome then. 300 * 3 = 900 clicks (Changing VMs is a click too) and then heavens forbid you miss, and have duplicate UUIDs out there.

So, how do we do this in my automation tool of choice: PowerShell? First we take a look around… Courtsy of Mr Rottenberg:

PS > $a = Get-VM myvm | Get-View
PS > $a | gm *answer*
   TypeName: VMware.Vim.VirtualMachine
Name     MemberType Definition
----     ---------- ----------
AnswerVM Method     System.Void AnswerVM(String questionId, String answerChoice)

Cool, so how do we get the questionID? We search a bit, and find “QuestionInfo”

PS C:\> $ans.Runtime.Question

Id              : 0
Text            : msg.uuid.moved:The location of this virtual machine's configu
                  ration file has changed since it was last powered on.

                  If the virtual machine has been copied, you should create a n
                  ew unique identifier (UUID).  If it has been moved, you shoul
                  d keep its old identifier.

                  If you are not sure, create a new identifier.

                  What do you want to do?
Choice          : VMware.Vim.ChoiceOption
Message         :
DynamicType     :
DynamicProperty :

Cool… Getting closer. Choice Option? Digging deeper:

PS C:\> $ans.Runtime.Question.Choice.ChoiceInfo

Key             : 1
Label           : Create
Summary         : Create
DynamicType     :
DynamicProperty :

Key             : 2
Label           : Keep
Summary         : Keep
DynamicType     :
DynamicProperty :

Key             : 3
Label           : Always Create
Summary         : Always Create
DynamicType     :
DynamicProperty :

Key             : 4
Label           : Always Keep
Summary         : Always Keep
DynamicType     :
DynamicProperty :

Key             : 0
Label           : Cancel
Summary         : Cancel
DynamicType     :
DynamicProperty :

Looks like we now have enough to work with… So lets answer that question:

PS C:\> $ans.AnswerVM(0,1)

image

And with that the VM powered on. Building this into a larger script will be left as an exercise for the reader. What, don’t like to exercise? Fine, post a comment.

16 thoughts on “Answering VM Questions With PowerShell

Comments are closed.