VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Select VMname by Guest OS Hostname

I have an input file with a set of operating system hostnames.  I need to take this OS hostname from the input file and do a get-vm against vCenter

(Assume VM tools are installed on each VM and it is powered on so that the OS hostname is available)

If I have these 3 hostnames in my input file:

windows1

Windows2

windows3

How would I pull these and find the VM  names for each as seen in vCenter? 

Thanks!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm afraid there are a couple of flaws in that script.

  • The -like operator will only return true if the list contains an exact match, i.e. FQDN vs FQDN or hostname vs hostname
  • There is an unnecessary number of calls to Get-VM

$ServerList = Get-Content C:\scripts\servers.txt

$Report = @()

$vms = Get-VM

foreach ($Server in $ServerList){

    $vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{

        $report += New-Object PSObject -Property @{

           VM_Name = $_.Name

           DNS_Name = $_.ExtensionData.Guest.Hostname

        }

    }

}

$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation


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

View solution in original post

12 Replies
nicholas1982
Hot Shot
Hot Shot
Jump to solution

This is basic but should do the job, if "VM _Name" is blank most likely tools isn't running

$ServerList = Get-Content C:\scripts\servers.txt

$Report = @()

foreach ($Server in $ServerList){

$VM = Get-VM | Where-Object {$_.ExtensionData.Guest.Hostname -like $server}

$Report += New-Object PSObject -Property @{

VM_Name = $VM

DNS_Name = $server}

}

$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation

Nicholas
LucD
Leadership
Leadership
Jump to solution

I'm afraid there are a couple of flaws in that script.

  • The -like operator will only return true if the list contains an exact match, i.e. FQDN vs FQDN or hostname vs hostname
  • There is an unnecessary number of calls to Get-VM

$ServerList = Get-Content C:\scripts\servers.txt

$Report = @()

$vms = Get-VM

foreach ($Server in $ServerList){

    $vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{

        $report += New-Object PSObject -Property @{

           VM_Name = $_.Name

           DNS_Name = $_.ExtensionData.Guest.Hostname

        }

    }

}

$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation


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

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Thanks Luc, that makes perfect sense. Smiley Happy

Nicholas
Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

This is already solved here by and , but I wanted to mention another option:  the vNugglets.Utility PowerShell module (of which I am an author) has a cmdlet for getting VMs by VM guest hostname:  Get-VNVMByAddress with the -GuestHostname parameter.

Example for a single guest OS hostname:

Get-VNVMByAddress -GuestHostname myguesthostname0

Name         GuestHostname     MoRef                   Client

----         -------------     -----                   ------

myVMName0    myguesthostname0  VirtualMachine-vm-18927 VMware.Vim.VimClientImpl

Or, for a list of guest OS hostnames:

## iterate through each server name in the file, returning corresponding VMs with given guest OS hostnames

Get-Content C:\scripts\servers.txt | Foreach-Object {Get-VNVMByAddress -GuestHostname $_}

Part of the benefit:  that Get-VNVMByAddress cmdlet is using the vCenter SearchIndex object, and can be quicker/more efficient, especially in a larger environment.

And, of course, that module is available in the PowerShellGallery, so getting it is easy with Install-Module or Save-Module.

TheVMinator
Expert
Expert
Jump to solution

thanks again all - much appreciated

Reply
0 Kudos
Maryland
Contributor
Contributor
Jump to solution

@LucD

If I just need to pull FQDN of VM's in vCenter, could I just do this?

$Report = @()

 

$vms = Get-VM

 

{$vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{

        $report += New-Object PSObject -Property @{

           VM_Name = $_.Name

           DNS_Name = $_.ExtensionData.Guest.Hostname

        }

    }

}

 

$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation

 

Thanks in advance for any help, kind sir 🙂

- DM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would do it like this.
Don't forget the put a value in the $server variable.

$server = 'name'

$Report = @()


Get-VM | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | % {

   $report += New-Object PSObject -Property @{

   VM_Name = $_.Name

   DNS_Name = $_.ExtensionData.Guest.Hostname

   }

}


$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation


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

Reply
0 Kudos
Maryland
Contributor
Contributor
Jump to solution

hmmm...so for $server, could I do like a gc -path c:\vmlist.txt, with a list of all the VM's in vCenter?  I'm sorry man, I'm new to PowerCLI scripting...

- DM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The current setup is that the VMs are filtered by their FQDN.
If the FQDN of a VM matches (in fact if the FQDN contains the string you sopecify in $server), it passes.
But if you want to have all VMs in your vCenter in the report, you can just leave out the Where-clause.
And If you only want to have the VMs in your text file in your report, you could do

$Report = @()

$vmNames = Get-Content -Path .\vmlist.txt

Get-VM -Name $vmNames | % {

   $report += New-Object PSObject -Property @{

   VM_Name = $_.Name

   DNS_Name = $_.ExtensionData.Guest.Hostname

   }

}

$Report | select VM_Name, DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation


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

Reply
0 Kudos
Maryland
Contributor
Contributor
Jump to solution

sweet, thanks man!

- DM

Reply
0 Kudos
Maryland
Contributor
Contributor
Jump to solution

I made one small adjustment and it ran perfect...thanks LucD!  I guess it wouldn't be a bad idea to pick up your PowerCLI book for VMware 😉

- DM

Reply
0 Kudos
Maryland
Contributor
Contributor
Jump to solution

I made one small adjustment and it ran perfect...thanks LucD!  I guess it wouldn't be a bad idea to pick up your PowerCLI book for VMware 😉

- DM

Reply
0 Kudos