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?â€.
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*
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)
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.