12 Replies Latest reply on Sep 21, 2017 5:55 AM by jvm2016

    Finding serial number of esxi hosts_powercli

    jvm2016 Expert

      Hi all/Luc.D

       

      We have requirement to find serial number of esxihosts .

      We have been given list of vms and asked to find serial number of esxi hosts on which the vm is running.

      although DRS can move vm but i tried to find this info after Changing DRS modeof virtual machines  to manual.

       

      I am thinking to do following

      1:changing DRS mode of individual vm to manual.

      2:finding the esxi host of that vm

      3:and using esxli to get serial number of esxi host.

       

       

      created following script.

       

       

      $vc=Read-Host "provide the vcenter name"

       

      $cred=Get-Credential

      Connect-VIServer -Server $vc -Credential $cred

      $vm_list=Get-Content -path "C:\namesvm.txt"

      foreach ($vm in $vm_list)

      {

      $x=get-vm $vm

      $drs_manual=set-vm -vm $x -drsautomationlevel manual

      $esxiname=$vm.vmhost

      $esxcli=get-esxcli -vmhost $esxiname

      $esxcli.hardware.platform.get().SerialNumber

      }

      disconnect-viserver

       

      I  have following question.

      How to put sleep or wait function between the time script change drsmode of vm and finding the esxiname to feed to esxcli.

       

      or is there any other way to achieve  the above goal.

       

      Any help is much appreciated.

        • 1. Re: Finding serial number of esxi hosts_powercli
          Zsoldier Expert
          vExpert

          I would take the approach of setting up a vCenter alarm to run your script (with some modifications of course) after a successful vMotion event.  Probably best approach would be to do something like sending an smtp trap to orchestrator to have it run the workflow that you want.

           

          This way it triggers only when needed and doesn't have to be schedule based.

           

          Here are some resources:

          http://www.virtuallyghetto.com/2016/06/how-to-run-a-script-from-a-vcenter-alarm-action-in-the-vcsa.html

          Automatically Securing Virtual Machines Using vCenter Orchestrator - VMware vSphere Blog

          • 2. Re: Finding serial number of esxi hosts_powercli
            jvm2016 Expert

            thnaks for your response but currently we have not integrated vRO with vcenters.so right now looking suggestions in powercli code.

            could you check the code and suggest any modification.

            • 3. Re: Finding serial number of esxi hosts_powercli
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              Would this work?

               

              $vc=Read-Host "provide the vcenter name"

               

              $cred=Get-Credential

              Connect-VIServer -Server $vc -Credential $cred

               

              $vm_list=Get-Content -path "C:\namesvm.txt"

               

              foreach ($vm in $vm_list)

              {

                  $x=Get-VM $vm

                  $x=Set-VM -VM $x -DrsAutomationLevel Manual

                  while($x.DrsAutomationLevel -ne 'Manual'){

                      sleep 1

                      $x = Get-VM -Name $vm

                  }

               

                  $esxcli=Get-EsxCli -VMHost $x.VMHost.Name

                  $esxcli.hardware.platform.get().SerialNumber

              }

              Disconnect-VIServer

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Finding serial number of esxi hosts_powercli
                jvm2016 Expert

                Thanks for your reply .This is working but for for some reasons it loops for 3 to 4 times .

                 

                Also following things i want to avoid .

                1:passingvirtual machines names from notepad as it always gives space issues .is there any way to avoid or trim space.

                2:also output which iam getting  again not in presentable format.

                 

                could you suggest any other way of doing this to convert it into csv or excel format .

                 

                Thanks in advace.

                • 5. Re: Finding serial number of esxi hosts_powercli
                  jvm2016 Expert

                  Hi LUC.D,

                   

                  iamcreating separte script for disabling drs but for the time being assume drs is set to manual.

                   

                  i created following script to get serial number using importing csv file to avoid space issues in importing .txt.

                   

                   

                  $vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")

                   

                  #$cred=Get-Credential

                  Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""

                   

                  $csv_info = Import-Csv c:\namesvm.csv

                  $csv_info.count

                   

                   

                  foreach ($line in $csv_info)

                  {

                      $x=get-vm $line.vmname

                      $esxiname=$x.vmhost

                      $n=$esxiname|select name

                      $n

                   

                   

                  $esxcli=get-esxcli -vmhost $esxiname

                  $sn=$esxcli.hardware.platform.get().SerialNumber

                  $sn

                     

                    

                   

                  }

                   

                   

                  this is printing esxi name and corresponding serial number .could you modify this code so that i can create an empty array which can store info of vmname.esxiname and serial number .

                   

                   

                  Thanks.

                  • 6. Re: Finding serial number of esxi hosts_powercli
                    LucD Guru
                    User ModeratorsvExpertCommunity Warriors

                    Try like this

                     

                    $vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")

                     

                    #$cred=Get-Credential

                    Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""

                     

                    $csv_info = Import-Csv c:\namesvm.csv

                    $csv_info.count

                     

                     

                    foreach ($line in $csv_info)

                    {

                        $x=Get-VM $line.vmname

                     

                        $esxcli=Get-EsxCli -VMHost $x.VMHost

                        $esxcli.hardware.platform.get().SerialNumber |

                        select @{N='VMHost';E={$x.VMHost.Name}},

                            @{N='Serial';E={$_}}

                    }

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 7. Re: Finding serial number of esxi hosts_powercli
                      jvm2016 Expert

                      that does not work .

                       

                      however i modified again little as follows.

                      foreach ($line in $csv_info)

                      {

                          $x=get-vm $line.vmname

                          $x.name

                          $esxiname=$x.vmhost

                          #$esxiname

                          $n=$esxiname.name

                          $n

                         

                       

                       

                      $esxcli=get-esxcli -vmhost $esxiname

                      $sn=$esxcli.hardware.platform.get().SerialNumber

                      $sn

                      }

                       

                       

                      so $x.name prints vmname

                           $n prints esxiname

                           $sn  prints serial number

                       

                      following is the output

                       

                       

                      i have seen expressions wherein each of these variables can be passed to an array to genrate output in the form {vmname ,esxiname ,serialnumber}

                      could you suggest something like this.

                      • 8. Re: Finding serial number of esxi hosts_powercli
                        jvm2016 Expert

                        something like folowing in orange.

                         

                        $out = @()

                         

                        foreach ($line in $csv_info)

                        {

                            $x=get-vm $line.vmname

                            $v=$x.name

                            $v

                            $esxiname=$x.vmhost

                            #$esxiname

                            $n=$esxiname.name

                            $n

                           

                         

                         

                        $esxcli=get-esxcli -vmhost $esxiname

                        $sn=$esxcli.hardware.platform.get().SerialNumber

                        $sn

                         

                        $row = &quot|Select vmname,esxihost,serialnumber

                        $row.vmname = $vc

                        $row.esxihost = $n

                        $row.serialnumber = $sn

                        $out +=$row

                         

                           

                          

                         

                        }

                        • 9. Re: Finding serial number of esxi hosts_powercli
                          LucD Guru
                          User ModeratorsCommunity WarriorsvExpert

                          What exactly didn't work?
                          Did you get any errors?

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: Finding serial number of esxi hosts_powercli
                            jvm2016 Expert

                            well it worked this time .also i added another column vmname.

                             

                             

                            foreach ($line in $csv_info)

                             

                            {

                             

                                $x=Get-VM $line.vmname

                             

                             

                             

                                $esxcli=Get-EsxCli -VMHost $x.VMHost

                             

                                $esxcli.hardware.platform.get().SerialNumber |

                             

                                select @{N='VMHost';E={$x.VMHost.Name}},

                             

                                    @{N='Serial';E={$_}},@{N="vmname"; e={$x}}

                             

                            }

                             

                            output

                             

                             

                            how to convert this to csv or excel .can export-csv be put inside for loop. what shoud be edited to convert this to csv or excel.

                             

                            Thanks.

                            • 11. Re: Finding serial number of esxi hosts_powercli
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              Try like this

                               

                              $vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")

                               

                              Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""

                               

                              $csv_info = Import-Csv c:\namesvm.csv

                               

                              $report = foreach ($line in $csv_info)

                              {

                                  $x=Get-VM $line.vmname

                               

                                  $esxcli=Get-EsxCli -VMHost $x.VMHost

                                  $esxcli.hardware.platform.get().SerialNumber |

                                  select @{N='VMHost';E={$x.VMHost.Name}},

                                      @{N='Serial';E={$_}},

                                      @{N="vmname";E={$x.Name}}

                              }

                              $report | Export-Csv .\report.csv -NoTypeInformation -UseCulture

                               

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 12. Re: Finding serial number of esxi hosts_powercli
                                jvm2016 Expert

                                Thnak you very much .it worked .appreciate your help.