I was asked by our IT department to provide serial numbers of our lab servers. Fortunately this can be done remotely with esxcfg-info CLI command and can be automated with William Lam (@lamw) PowerCLI function Get-Esxcfginfo. I just had to find the right entry in the xml file returned by the function.
Here is the script I used:
<# .SYNOPSIS Remoting collecting esxcfg-info from an ESXi host using vCenter Server .NOTES Author: William Lam .NOTES Site: www.virtuallyghetto.com .NOTES Reference: http://www.virtuallyghetto.com/2016/06/using-the-vsphere-api-to-remotely-collect-esxi-esxcfg-info.html .PARAMETER Vmhost ESXi host .EXAMPLE PS> Get-VMHost -Name "esxi-1" | Get-Esxcfginfo #> Function Get-Esxcfginfo { param( [Parameter( Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true) ] [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]]$VMHost ) $sessionManager = Get-View ($global:DefaultVIServer.ExtensionData.Content.sessionManager) # URL to the ESXi esxcfg-info info $url = "https://" + $vmhost.Name + "/cgi-bin/esxcfg-info.cgi?xml" $spec = New-Object VMware.Vim.SessionManagerHttpServiceRequestSpec $spec.Method = "httpGet" $spec.Url = $url $ticket = $sessionManager.AcquireGenericServiceTicket($spec) # Append the cookie generated from VC $websession = New-Object Microsoft.PowerShell.Commands.WebRequestSession $cookie = New-Object System.Net.Cookie $cookie.Name = "vmware_cgi_ticket" $cookie.Value = $ticket.id $cookie.Domain = $vmhost.name $websession.Cookies.Add($cookie) # Retrieve file $result = Invoke-WebRequest -Uri $url -WebSession $websession -ContentType "application/xml" # cast output as an XML object return [ xml]$result.content } Connect-VIServer -Server xxx.gcp.local -User administrator@vsphere.local -password VMware1! | Out-Null $hosts = Get-VMHost foreach ($ESXhost in $hosts) { $xmlResult = $ESXhost | Get-Esxcfginfo Write-Host $ESXhost.name ($xmlResult.host.'hardware-info'.value[3].'#text') } Disconnect-VIServer * -Confirm:$false
Advertisements
One thought on “Query ESXi Hosts Serial Numbers”