Answering VM Questions With PowerShell

by bunchc on January 18, 2009

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.

  • LucD

    Were you able to get your hands on a list with the possible Qustion Ids ?
    See also my remark in http://communities.vmware.com/thread/180916?tst…

  • http://professionalvmware.com professionalvmware

    I'll keep looking

  • http://blogs.vmware.com/vipowershell Carter Shanklin

    I've never seen a list of these.

  • http://professionalvmware.com professionalvmware

    I opened an SR on it just to see. We'll see on Monday.

  • george

    Starting to see this pop-up more now. Are you getting message during power on?

  • http://professionalvmware.com professionalvmware

    Not sure I understand the question Goroge, but I'll take a stab at it. Yes, the questions discussed here happen during Power On.

  • george

    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.

  • http://professionalvmware.com professionalvmware

    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.

  • presscontrols

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

  • http://www.easymud.com Auctions

    i have never seen this type of list earlier..Thanks for sharing

  • Dating

    Very good article.Thanks a lot for sharing this Answering VM Questions With PowerShell

  • http://www.digbands.com/ bands

    This is a helpful information. It will be a less effort and time saving.

  • http://professionalvmware.com professionalvmware

    Glad I could help.

  • http://www.ekjodi.com/ Matrimony

    Thanks for this great article..please post more useful articles like this..Great sharing..

  • http://www.DigBands.com/gigs Gigs

    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

  • http://www.DigBands.com/gigs Gigs

    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

  • http://www.ishqballe.com/ singh

    Thanks for this demonstration and screen shot, answered all my questions!

Previous post:

Next post: