PowerCLI

 View Only
Expand all | Collapse all

Select VMname by Guest OS Hostname

  • 1.  Select VMname by Guest OS Hostname

    Posted Nov 04, 2017 12:56 AM

    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!



  • 2.  RE: Select VMname by Guest OS Hostname

    Posted Nov 04, 2017 04:54 AM

    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



  • 3.  RE: Select VMname by Guest OS Hostname
    Best Answer

    Posted Nov 04, 2017 08:00 AM

    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



  • 4.  RE: Select VMname by Guest OS Hostname

    Posted Nov 04, 2017 11:38 AM

    Thanks Luc, that makes perfect sense. :smileyhappy:



  • 5.  RE: Select VMname by Guest OS Hostname

    Posted Nov 06, 2017 03:51 PM

    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.



  • 6.  RE: Select VMname by Guest OS Hostname

    Posted Nov 14, 2017 07:58 PM

    thanks again all - much appreciated



  • 7.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 03:08 PM

    @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



  • 8.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 03:29 PM

    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



  • 9.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 03:35 PM

    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



  • 10.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 03:48 PM

    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



  • 11.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 04:12 PM

    sweet, thanks man!

    - DM



  • 12.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 06:08 PM

    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



  • 13.  RE: Select VMname by Guest OS Hostname

    Posted Jan 04, 2019 06:08 PM

    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