PowerCLI

 View Only
  • 1.  Inventory script for ESX host

    Posted May 08, 2020 05:41 AM

    Hi,

    im trying to make some simple inventory script for ESX host, which would give me all information i need, but im failing to get snmp community.

    report_hosts = Foreach($vc in $global:DefaultVIServers){

    Get-VMHost | Sort Name | %{$VMHost = $_; $VMHost | Get-View | Select @{N=”ESXi Host Name”;E={$VMHost.Name}},

    @{N=”name”;E={$VMHost.NetworkInfo.HostName}},

    @{N=”cluster”;E={$VMHost.Parent.Name}},

    @{N=”platform”;E={'VMware ESXi ' + $VMHost.Version}},

    @{N='manufacturer';E={$_.Summary.Hardware.Vendor}},

    @{N="model_name";E={$_.Hardware.SystemInfo.Model}},

    @{N="cf_cpu_model";E={$_.Hardware.CpuPkg[0].Description}},

    @{N="cf_cpu_mhz";E={[math]::round($_.Hardware.CpuInfo.Hz / 1000000, 0)}},

    @{N="cf_cpu_count";E={$_.Hardware.CpuInfo.NumCpuPackages}},

    @{N="cf_cpu_cores_per_cpu";E={($_.Hardware.CpuInfo.NumCpuCores) /($_.Hardware.CpuInfo.NumCpuPackages)}},

    @{N="cf_cpu_count_threads";E={$_.Hardware.CpuInfo.NumCpuThreads}},

    @{N="cf_mem_size";E={"" + [math]::round($_.Hardware.MemorySize / 1MB, 0)}},

    @{N="cf_srv_uptime";E={[math]::round($_.Summary.QuickStats.uptime / 86400, 0)}}, ## figure out how to attach also text "Days" to this

    @{N="serial";E={($VMHost | Get-VMHostHardware).SerialNumber}},

    @{N=”NTP”;E={Get-VMHostNtpServer $VMHost}},

    @{N="status"; E={$VMHost.State}},

    @{N=”cf_srv_timezone”;E={$VMHost.Timezone}},

    @{N=”cf_nic_count”;E={$_.Summary.Hardware.numNics}},

    @{N=”cf_srv_domain”;E={($VMHost | Get-VMHostNetwork).DomainName}},

    @{N="srv_uuid";E={$_.hardware.systeminfo.uuid}}

    @{N="device_role"; E={'ESXi'}},

    @{N="asset_tag"; E={'vcenter_sync'}},

    @{N=”Datacenter”;E={$VMHost.Parent.ParentFolder.Parent.Name}}

    $esxcli = Get-EsxCli -VMHost $VMHost

    $esxcli.system.snmp.get() | Select @{N="communities";E={[string]::Join(",",$_.communities)}}

    }

    }

    $report_hosts | Export-Csv C:\Temp\ESX_Devices.csv

    All is working, except the snmp community, i would welcome any help/hints how to include the snmp into this script :smileyhappy:



  • 2.  RE: Inventory script for ESX host
    Best Answer

    Posted May 08, 2020 06:49 AM

    I would be very much amazed if that script works at all.

    It looks like you took parts of scripts and threw them together, but there are several issues.

    In any case, for the SNMP Community, you could do

    @{N = ”Datacenter”; E = { $VMHost.Parent.ParentFolder.Parent.Name } },

    @{N = "communities"; E = {

         ((Get-EsxCli -VMHost $VMHost).system.snmp.get()).Communities -join ',' }}



  • 3.  RE: Inventory script for ESX host

    Posted May 12, 2020 01:40 PM

    LucD,

    I tried to pull the ESX inventory after modification but it throws an error.

    PS D:\user> .\ESX_Inventory.ps1

    At D:\user\ESX_Inventory.ps1:1 char:28

    + report_hosts = Foreach($vc in $global:DefaultVIServers){

    +                            ~~

    Unexpected token 'in' in expression or statement.

    At D:\user\ESX_Inventory.ps1:1 char:27

    + report_hosts = Foreach($vc in $global:DefaultVIServers){

    +                           ~

    Missing closing ')' in expression.

    At D:\user\ESX_Inventory.ps1:1 char:55

    + report_hosts = Foreach($vc in $global:DefaultVIServers){

    +                                                       ~

    Unexpected token ')' in expression or statement.

        + CategoryInfo          : ParserError: (:) [], ParseException

        + FullyQualifiedErrorId : UnexpectedToken

    Thanks

    V



  • 4.  RE: Inventory script for ESX host

    Posted May 12, 2020 01:51 PM

    Like I said earlier, there are several errors in that script, which made me seriously doubt it ever ran.



  • 5.  RE: Inventory script for ESX host

    Posted May 12, 2020 02:01 PM

    Thank you. LuCD



  • 6.  RE: Inventory script for ESX host

    Posted May 12, 2020 02:24 PM

    This should work

    & { Foreach ($vc in $global:DefaultVIServers) {

            Get-VMHost -Server $vc |

            Select-Object @{N = ”ESXi Host Name”; E = { $_.Name } },

                @{N = ”name”; E = { $_.ExtensionData.NetworkInfo.HostName } },

                @{N = ”cluster”; E = { (Get-Cluster -VMHost $_).Name } },

                @{N = ”platform”; E = { 'VMware ESXi ' + $_.Version } },

                @{N = 'manufacturer'; E = { $_.ExtensionData.Summary.Hardware.Vendor } },

                @{N = "model_name"; E = { $_.ExtensionData.Hardware.SystemInfo.Model } },

                @{N = "cf_cpu_model"; E = { $_.ExtensionData.Hardware.CpuPkg[0].Description } },

                @{N = "cf_cpu_mhz"; E = { [math]::round($_.ExtensionData.Hardware.CpuInfo.Hz / 1000000, 0) } },

                @{N = "cf_cpu_count"; E = { $_.ExtensionData.Hardware.CpuInfo.NumCpuPackages } },

                @{N = "cf_cpu_cores_per_cpu"; E = { ($_.ExtensionData.Hardware.CpuInfo.NumCpuCores) /($_.ExtensionData.Hardware.CpuInfo.NumCpuPackages) } },

                @{N = "cf_cpu_count_threads"; E = { $_.ExtensionData.Hardware.CpuInfo.NumCpuThreads } },

                @{N = "cf_mem_sizeMB"; E = { [math]::Round($_.MemoryTotalMB) } },

                @{N = "cf_srv_uptime"; E = { "$([math]::round($_.ExtensionData.Summary.QuickStats.uptime / 86400, 0)) days" } },

                @{N = "serial"; E = { ($VMHost | Get-VMHostHardware).SerialNumber } },

                @{N = ”NTP”; E = { (Get-VMHostNtpServer -VMHost $VMHost) -join ',' } },

                @{N = "status"; E = { $_.State } },

                @{N = ”cf_srv_timezone”; E = { $_.Timezone } },

                @{N = ”cf_nic_count”; E = { $_.ExtensionData.Summary.Hardware.numNics } },

                @{N = ”cf_srv_domain”; E = { (Get-VMHostNetwork -VMHost $_).DomainName } },

                @{N = "srv_uuid"; E = { $_.ExtensionData.hardware.systeminfo.uuid } },

                @{N = "device_role"; E = { 'ESXi' } },

                @{N = "asset_tag"; E = { 'vcenter_sync' } },

                @{N = ”Datacenter”; E = { (Get-Datacenter -VMHost $_).Name } },

                @{N = 'SNMP Communities'; E = {

                    $esxcli = Get-EsxCli -VMHost $_

                    ($esxcli.system.snmp.get()).communities -join ',' }}

    } } | Export-Csv -Path C:\Temp\ESX_Devices.csv -NoTypeInformation -UseCulture

      

       



  • 7.  RE: Inventory script for ESX host

    Posted May 13, 2020 07:31 PM

    Hi Luc,

    thx for correction and answer :smileyhappy: the initial script is actually working after some modification, but for sure yours looks way better :smileygrin:

    i have one more questions... how to get physical disk size of the esxi?

    @{N="cf_disk_size_gb"; E = {((Get-EsxCli -VMHost $VMHost).storage.core.device.list()) | where{$_.Model -eq 'LOGICAL VOLUME' -or $_.Model -eq 'Block Device' } | Select Size }}

    When i use the line above im getting output like:

    @{Size=1430351}

    @{Size=139979}

    How to write it correctly to get output just number and also then convert it to GB?

    Regards,
    Martin



  • 8.  RE: Inventory script for ESX host

    Posted May 13, 2020 07:57 PM

    I'm not sure if your Where-clause is correct, I normally use DeviceType to filter.

    @{N="cf_disk_size_gb"; E = {((Get-EsxCli -VMHost $_).storage.core.device.list() |

        where{$_.DeviceType -eq 'Direct-Access'} |

        %{[math]::Round($_.Size/1KB)}) -join ',' }}



  • 9.  RE: Inventory script for ESX host

    Posted May 13, 2020 08:12 PM

    Working fine now, thx :smileyhappy:

    i needed to use the Model filter as i found that on some ESX there is iSCI LUNs with Direct-Access



  • 10.  RE: Inventory script for ESX host

    Posted May 13, 2020 08:15 PM

    Ok, I see