1 2 Previous Next 29 Replies Latest reply on Jun 26, 2015 2:26 AM by MKguy

    Script to get serial number of all hosts

    peter79 Enthusiast

      Guys,

       

      I have a mixture od Dell and IBM hosts and need a script that will get the sercvice tag/serial number of the hosts and export them to a CSV file (along with the host name).  Can someone help me out?

       

      Thanks. 

        • 1. Re: Script to get serial number of all hosts
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Does this return anything ?

           

          Get-View -ViewType HostSystem | Select Name,@{N="Tag";E={$_.Summary.Hardware.OtherIdentifyingInfo[1].IdentifierValue}}

           

          It depends on the type of HW and on the vendor if this field is populated.

          • 2. Re: Script to get serial number of all hosts
            RvdNieuwendijk Virtuoso
            User ModeratorsvExpert

            The next script will export all the host names and corresponding serial numbers to a csv file c:\Temp\EsxSerialNumbers.csv. The serial numbers are the same as seen in the vCenter Server client Hardware Status tab System Summary of an ESX server.

             

            I have to give some credits to others. The function Get-VMHostWSManInstance comes from the blogpost Monitoring ESX hardware with PowerShell. I made a little change to this function to use the new VMHostImpl typename. The way to extract the serial number comes from a perl script made by bhoriuchi and found in this thread: ESX Host Serial Number and is my Host part of Virtual Center...?.

             

            function Get-VMHostWSManInstance {
                param (
                [Parameter(Mandatory=$TRUE,HelpMessage="VMHosts to probe")]
                [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]
                $VMHost,
            
                [Parameter(Mandatory=$TRUE,HelpMessage="Class Name")]
                [string]
                $class,
            
                [switch]
                $ignoreCertFailures,
            
                [System.Management.Automation.PSCredential]
                $credential=$null
                )
            
                $omcBase = "http://schema.omc-project.org/wbem/wscim/1/cim-schema/2/"
                $dmtfBase = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
                $vmwareBase = "http://schemas.vmware.com/wbem/wscim/1/cim-schema/2/"
            
                if ($ignoreCertFailures) {
                    $option = New-WSManSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
                } else {
                    $option = New-WSManSessionOption
                }
                foreach ($H in $VMHost) {
                    if ($credential -eq $null) {
                        $hView = $H | Get-View -property Value
                        $ticket = $hView.AcquireCimServicesTicket()
                        $password = convertto-securestring $ticket.SessionId -asplaintext -force
                        $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $ticket.SessionId, $password
                    }
                    $uri = "https`://" + $h.Name + "/wsman"
                    if ($class -cmatch "^CIM") {
                        $baseUrl = $dmtfBase
                    } elseif ($class -cmatch "^OMC") {
                        $baseUrl = $omcBase
                    } elseif ($class -cmatch "^VMware") {
                        $baseUrl = $vmwareBase
                    } else {
                        throw "Unrecognized class"
                    }
                    Get-WSManInstance -Authentication basic -ConnectionURI $uri -Credential $credential -Enumerate -Port 443 -UseSSL -SessionOption $option -ResourceURI "$baseUrl/$class"
                }
            }
            
            Get-VMHost | ForEach-Object {
              $VMHost = $_
              $PhysicalPackage = Get-VMHostWSManInstance -VMHost $_ -class CIM_PhysicalPackage -ignoreCertFailures | Where-Object {$_.ElementName -eq "Chassis"}
              $Report = "" | Select-Object -Property VMHost,SerialNumber
              $Report.VMHost = $VMHost.Name
              $Report.SerialNumber = $PhysicalPackage.SerialNumber
              $Report
            } | Export-Csv -NoTypeInformation -UseCulture -Path c:\Temp\EsxSerialNumbers.csv 
            
            

             

            Regards, Robert

            • 3. Re: Script to get serial number of all hosts
              bhoriuchi Novice

              Nice work! Although I have run into an issue when using the  CIM_PhysicalPackage on HP blade servers. It seems that the serial number returned is the serial number of the enclosure they are in. I have verified in the hardware status tab in the vSphere Client that the same is true. I have asked our TAM to check with development to see if there is another class that will provide the serial number for blades in an enclosure.

              • 4. Re: Script to get serial number of all hosts
                RvdNieuwendijk Virtuoso
                vExpertUser Moderators

                You are right. For HP blade servers the script, and the vSphere client, return the serial number of the blade enclosure instead of the serial number of the blade server. Which makes sense because the CIM_PhysicalPackage object is filtered for "chassis".

                 

                For HP servers the serial number is in a CIM object called SMX_ComputerSystem. I found the SMX_ComputerSystem object and all other HP CIM objects for HP ESXi servers in this document:

                HP Insight Management WBEM Providers for HP integrated VMware ESXi

                 

                The HP CIM objects have an alias "root/hpq". But this alias is not know by the Get-WSManInstance cmdlet.

                 

                If you want to explore the standard VMware CIM objects, the documentation can be found here:

                VMware CIM APIs Documentation

                 

                Unfortunately the serialnumber is not in the CIM_ComputerSystem object.

                 

                The problem that I now have is that the PowerShell Get-WSManInstance cmdlet needs a -ResourceURI parameter that contains a URL to the HP base wbem schema. And I can't find the right URL. What I mean is the equivalent of:

                http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem

                for SMX_ComputerSystem.

                 

                I couldn't find it via Google. And I tried a lot of obvious URL's but they all didn't work. If anyone knows this URL, it would be great. In that case we are able to explore all the HP ESXi CIM objects via PowerShell and PowerCLI.

                • 5. Re: Script to get serial number of all hosts
                  RvdNieuwendijk Virtuoso
                  vExpertUser Moderators

                  In the PowerScripting Podcast Episode 148, I heard about Using Powershell to get info out of HP ILO and OBA. I used this information to get the serial number of an HP ESX(i) server from the ILO board.

                   

                  The script asumes that the DNS name of your ILO board is the servername with a prefix or suffix. In the code example I used "_ilo" as a suffix for the servername. E.g. if your servername is "ESX1" than your ILO DNS name is "ESX1_ilo". Change the $RiloePrefix and RiloeSuffix variables in the script according to your needs.

                   

                  $RiloePrefix = ""
                  $RiloeSuffix = "_ilo"
                  Get-VMHost | `
                  Where-Object {$_.Manufacturer -eq "HP"} | `
                  Sort-Object -Property Name | ForEach-Object {
                    $VMHost = $_
                    $xml = New-Object System.Xml.XmlDocument
                    $xml.Load("https://$RiloePrefix$($VMHost.Name.Split('.')[0])$Riloesuffix/xmldata?item=All")
                    New-Object PSObject -Property  @{
                      "Name" = $VMHost.Name
                      "Serial Number" = $xml.RIMP.HSI.SBSN
                      "ILO Serial Number" = $xml.RIMP.MP.SN
                      "ILO Type" = $xml.RIMP.MP.PN
                      "ILO Firmware" = $xml.RIMP.MP.FWRI    
                    }
                  }
                  
                  

                   

                  Note: This script gives only output for HP ESX(i) servers with an Integrated Lights-Out board.

                  • 6. Re: Script to get serial number of all hosts
                    halr9000 Master

                    Thanks for posting this script! I really should listen to my podcast more often.

                    • 7. Re: Script to get serial number of all hosts
                      AlbertWT Master

                      Hi Rob,

                      Why I got error in executing the script especially when it contains "Get-WSManInstance"

                       

                      here's the details:

                       

                      Get-WSManInstance : Access is denied.
                      At \\FIleServer01-VM\homedir\albertwt\Downloads\Monitor ESX with WS-MAN.ps1:44 char:20
                      +         Get-WSManInstance <<<<  -Authentication basic -ConnectionURI $uri -Credential $credential -Enumerate -Port 443 -UseSSL -SessionOption $option -R
                      esourceURI "$baseUrl/$class"
                          + CategoryInfo          : InvalidOperation: (:) [Get-WSManInstance], UnauthorizedAccessException
                          + FullyQualifiedErrorId : Exception,Microsoft.WSMan.Management.GetWSManInstanceCommand
                      • 8. Re: Script to get serial number of all hosts
                        georgep Novice

                        I Get this error too

                         

                         

                        <f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="5" Machine="SAL25712.europe.jacobs.com"><f:Message>Access is denied. </f:Message></f:WSManFault> (Get-WSManInstance)At line: 44 char: 26


                        Please can you check why?

                        • 9. Re: Script to get serial number of all hosts
                          RvdNieuwendijk Virtuoso
                          User ModeratorsvExpert

                          You probably get the "Access is denied" message because you are running this script using an account that does not have enough privileges. You can try to specify alternate credentials by changing the last part of the script into:

                           

                          $Credential = Get-Credential
                          Get-VMHost | ForEach-Object {
                            $VMHost = $_
                            $PhysicalPackage = Get-VMHostWSManInstance -VMHost $_ -class CIM_PhysicalPackage -ignoreCertFailures -credential $Credential | `
                              Where-Object {$_.ElementName -eq "Chassis"}
                            $Report = "" | Select-Object -Property VMHost,SerialNumber
                            $Report.VMHost = $VMHost.Name
                            $Report.SerialNumber = $PhysicalPackage.SerialNumber
                            $Report
                          } | Export-Csv -NoTypeInformation -UseCulture -Path c:\Temp\EsxSerialNumbers.csv 
                          
                          

                          • 10. Re: Script to get serial number of all hosts
                            LucD Guru
                            Community WarriorsUser ModeratorsvExpert

                            The Access Denied message is mostly caused by the bug described in Get-WSManInstance prepends username with a backslash "\" .

                            This is solved in Win7 Sp1, but hasn't been backported to other OSs yet (afaik).

                            • 11. Re: Script to get serial number of all hosts
                              bse1969 Novice

                              Has this been fixed yet?

                              • 12. Re: Script to get serial number of all hosts
                                Fjorko Novice

                                Hi Mr R

                                 

                                I Still get the "Access Denied" even after changing the credentials. Should these Credentials be the HOST credentials ( root for example ) or the VC credentals - which in my case is on a separate domain.

                                 

                                I have already made a connection to the VC via "Connect-Viserver"

                                 

                                Thanks

                                • 13. Re: Script to get serial number of all hosts
                                  RvdNieuwendijk Virtuoso
                                  vExpertUser Moderators

                                  Hi Fjorko,

                                   

                                  you should enter the host credentials in the "Windows PowerShell Credential Request" box.

                                   

                                  Before the script starts you should already be connected to your vCenter server.

                                   

                                  Regards, Robert

                                  • 14. Re: Script to get serial number of all hosts
                                    Fjorko Novice

                                    Thanks

                                     

                                    I'm probably doing something wrong - even if I enter the root password in the windows credential popup box - I still get an Access Denied...

                                     

                                    Any other Ideas ?

                                     

                                    I'm just looking to extract the Serial numbers of our hosts using a POWERCLI script like yours.

                                     

                                    Thanks very much

                                    1 2 Previous Next