VMware Cloud Community
dtracey
Expert
Expert

Retrieve hostname from fqdn

Hi again guys,

Is there a simple way to return a hosts' short hostname from fqdn?

For example If I:

Connect-VIServer host1.domain.com

then the value of ($vmhost = Get-VMHost) will be host1.domain.com and I would like to create a new variable containing just host1.

I think I need to use the Powershell split() method with "." as a delimeter, but as yet i've not figured out how exactly to do it.

Incidentally, are all Powershell methods (such as split() and toupper() ) available in PowerCLI be default, or can I invoke them somehow?

Thanks,


Dan

2 Replies
dtracey
Expert
Expert

Worked this one out:

$vmhost = Get-VMHost

$hostname = $vmhost.name.split(".")[0]

Cheers,

Dan

LucD
Leadership
Leadership

Great you found the solution yourself.

All these methods come with the type String, so yes, they are always available.


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

0 Kudos