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

  • Sorry for the confusion, I'm getting these pop-up questions from nowhere. I have around 200 Windows XP VMs (not new), which are powered on for about 2-3 months now. And was just wondering if your issue was the same or newly created VM? And if you can assist with the script? Received error on the last command PS C:> $ans.AnswerVM(0,1). Thanks, and hope this does not confuse you more.

  • In the case of the VM's, were they restarted? vmotioned? Migrated?

    What error? PS C:> $ans.AnswerVM(0,1)
    In mid post I did a no-no with scripting and changed from one variable to another:

    $a = get-vm | get-view
    and then started with $ans… you may want to try the following to get from $a to $ans:
    $ans = $a | %{ $_.Runtime} | select -first 1

    That will at least get you the first one (what I worked with in the example) you can remove the select -first 1 and should get all of them.

  • thanks for this helpful posts and i think you can help many by this.keep it up.

  • Make the first layer of knots close to the knitted fabric, using 8 strands for each knot. Fudge the number of strands as you get close to the end if needed (your row count may differ from mine). You can see how the first bundle in the second row of knots is thicker than the rest

  • Make the first layer of knots close to the knitted fabric, using 8 strands for each knot. Fudge the number of strands as you get close to the end if needed (your row count may differ from mine). You can see how the first bundle in the second row of knots is thicker than the rest

Comments are closed.