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.

{ 16 comments }

LucD January 18, 2009 at 12:40 pm

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...

professionalvmware January 18, 2009 at 2:21 pm

I'll keep looking

Carter Shanklin January 18, 2009 at 7:48 pm

I've never seen a list of these.

professionalvmware January 18, 2009 at 11:00 pm

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

george March 9, 2009 at 3:01 pm

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

professionalvmware March 9, 2009 at 6:43 pm

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 March 9, 2009 at 8:01 pm

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.

professionalvmware March 10, 2009 at 6:22 am

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 June 19, 2009 at 7:19 am

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

Auctions September 16, 2009 at 4:07 am

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

Dating September 16, 2009 at 4:12 am

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

bands September 17, 2009 at 10:52 pm

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

professionalvmware September 18, 2009 at 8:19 am

Glad I could help.

Matrimony September 26, 2009 at 11:19 am

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

Gigs October 2, 2009 at 5:54 pm

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

Gigs October 2, 2009 at 12:54 pm

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 on this entry are closed.

blog comments powered by Disqus

Previous post:

Next post: