12 Replies Latest reply on Jan 4, 2019 10:08 AM by Maryland

    Select VMname by Guest OS Hostname

    TheVMinator Master

      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. Re: Select VMname by Guest OS Hostname
          nicholas1982 Hot Shot

          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

           

          1 person found this helpful
          • 2. Re: Select VMname by Guest OS Hostname
            LucD Guru
            User ModeratorsCommunity WarriorsvExpert

            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

            1 person found this helpful
            • 3. Re: Select VMname by Guest OS Hostname
              nicholas1982 Hot Shot

              Thanks Luc, that makes perfect sense.

              • 4. Re: Select VMname by Guest OS Hostname
                mattboren Master
                vExpert

                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.

                1 person found this helpful
                • 5. Re: Select VMname by Guest OS Hostname
                  TheVMinator Master

                  thanks again all - much appreciated

                  • 6. Re: Select VMname by Guest OS Hostname
                    Maryland Novice

                    @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

                    • 7. Re: Select VMname by Guest OS Hostname
                      LucD Guru
                      User ModeratorsCommunity WarriorsvExpert

                      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

                      • 8. Re: Select VMname by Guest OS Hostname
                        Maryland Novice

                        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

                        • 9. Re: Select VMname by Guest OS Hostname
                          LucD Guru
                          User ModeratorsCommunity WarriorsvExpert

                          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

                          • 10. Re: Select VMname by Guest OS Hostname
                            Maryland Novice

                            sweet, thanks man!

                            - DM

                            • 11. Re: Select VMname by Guest OS Hostname
                              Maryland Novice

                              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

                              • 12. Re: Select VMname by Guest OS Hostname
                                Maryland Novice

                                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