VMware Cloud Community
markdjones82
Expert
Expert

Switch condition question

All,

I am trying to take a hostname without FQDN and then say if get-vmhost works then set a variable to $vmhost, if it fails then do the next.  I can do an if statement, but was trying to understand the switch statement as I may have multiple values beyond a couple as far as domains go.  I am unsure if this syntax of logic is correct.  Any help?

switch ($hostlist.hostname)

  {

  {Get-VMHost $_+"test.com"} { $vmhost = Get-VMHost $_+"test.com"}

  {Get-VMHost $_+"test2.com"} { $vmhost = Get-VMHost $_+"test2.com"}

  }

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
2 Replies
LucD
Leadership
Leadership

if I understand correctly what you are trying to do, you wouldn't need a switch statement for this.

You could use a meta-character (*), something like this

$vmhost = Get-VMHost -Name "$($hostlist.hostname)*"

This will of course not produce a single value if you have the same hostname in multiple domains, for example

esx..test.com

esx.test2.com

In that case you would need additional logic to handle these cases


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

markdjones82
Expert
Expert

Well the host list does not actually have the host name attached.  I am pulling the information from UCS where the service profiles do not have FQDN.  So, i want it to try and test the get-vmhost for success and if it is then append that domain to hostname and run the get-vmhost.  I did get it working with an if /elseif statement and think I can convert it to switch.  Thanks for responding Luc!

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos