PowerCLI

 View Only
  • 1.  How do I get hostname from fqdn

    Posted Nov 11, 2014 11:42 AM

    I'm sure this has to be reasonably easy, however I haven't had much luck in figuring it out myself.

    Basically I've got a simple script that gets all the hosts from a cluster, and rename's the first datastore, to ServerName_Boot, however when I this, I can't find a way truncate the fqdn to just hostname. In this instance my hostname IS exactly 14 characters if this helps.

    Thx in advance!



  • 2.  RE: How do I get hostname from fqdn

    Posted Nov 11, 2014 11:49 AM

    This should do the trick.

    $fqdn.Split('.')[0]



  • 3.  RE: How do I get hostname from fqdn

    Posted Nov 11, 2014 09:13 PM

    Hi LucD,

    Thanks for the feedback, i tried what you suggested, however im obviously doing something wrong.

    I put the syntax into a write-host loop to test it first, and get the following error:

    Method invocation failed because [VMware.VimAutomation.ViCore.Impl.V1.Inventory

    .ClusterImpl] doesn't contain a method named 'Split'.

    At line:3 char:31

    + $Shortname = $VMHostname.Split <<<< (1.1)[0]

        + CategoryInfo          : InvalidOperation: (Split:String) [], RuntimeExce

       ption

        + FullyQualifiedErrorId : MethodNotFound

    Here is my sample script.

    $ClusterHost = Get-Cluster "Test_G" | Get-VMHost - Name

    ForEach ($VMhostname in $ClusterHost)

    {

    $Shortname = $VMHostname.Split(1.1)[0]

    write-host $Shortname

    }

    # $null = Get-VMhost $VMhostname | Get-Datastore | Set-Datastore -Name {$Shortname.Split('.')[0]}boot;

    Thanks



  • 4.  RE: How do I get hostname from fqdn
    Best Answer

    Posted Nov 11, 2014 09:22 PM

    You're trying to run split on the host object, not on the name itself.

    Just update it to $Shortname = $VMHostname.Name.Split('.')[0] 



  • 5.  RE: How do I get hostname from fqdn

    Posted Nov 11, 2014 10:00 PM

    You should take the Name property, like this

    Get-Cluster "Test_G" | Get-VMHost |

    Select @{N="FQDN";E={$_.Name}},@{N="Shortname";E={$_.Name.Split('.')[0]}}



  • 6.  RE: How do I get hostname from fqdn

    Posted Nov 11, 2014 10:05 PM

    Thanks all,

    This is the end script that renames all Datastores with the name Datastore* to <hostname>_boot.

    $ClusterHost = Get-Cluster "Test_G" | Get-VMHost

    ForEach ($VMhostname in $ClusterHost)

    {

    $Shortname = $VMHostname.Name.Split('.')[0] + "_Boot"

    $null = Get-VMhost $VMhostname | Get-Datastore Datastore* | Set-Datastore -Name $Shortname

    }

    Write-Host -ForegroundColor Green "Finished Renaming Datastores"