Hi folks. I am trying to write a script which can deploy VM's from templates in VMware. I'm new to this, so trying to keep it as simple as possible. I'm using VMware PowerCLI for this, and am stuck with how to compare input from a user with the result of a Get-Cluster cmdlet.

If the user types Cluster2, how can I compare that typed input with this list of Clusters? I want it to continue with the rest of the script if there is a match, or prompt again for the Cluster if there is no match (also providing the user with an explanation for why they need to enter it again.

If you want to implement that yourself, you can store a list of valid values in a variable and use either the -contains or -in operators to detect whether the user typed a valid option. That's not a particularly great user experience, though (having to type out a whole name instead of just entering a number or clicking a button.) If you don't mind a tiny bit of GUI interaction, here's an easy way to give people a list to select from in PowerShell 3.0 and onward:

If the user presses the Cancel button on that dialog, $selection will be $null; you'll probably want some code to detect and handle that case.

If you want to have a text menu rather than GUI in powershell.exe, you can use the $host.UI.PromptForChoice() method, but it's a little bit more involved to set up your selections. There's an example of this over on TechNet: http://technet.microsoft.com/en-us/library/ff730939.aspx