VMware Cloud Community
rgb99
Enthusiast
Enthusiast
Jump to solution

Get SNMP Config for All Hosts to CSV

I've been trying to figure this one out for quite some time. I have a script where I want to gather the SNMP settings for each host. The data is gathered and is exported to a csv file, but the file doesn't have the hostname of the ESXi host. I would like to add the hostname in the first column but I don't know how to do that. I'm sure it's a simple fix. Thanks, everyone!

$step = 1

$currentDir = (Get-Location).Path

$currentDir = $currentDir.TrimEnd('\')

$savefile = Read-Host -Prompt "`nEnter the name of the file where results will be saved (will be saved as $currentDir\<filename>.csv)"

$vmhosts = Get-VMHost | where {$_.ConnectionState -eq "Connected"}

$total = $vmhosts.count

foreach ($vmhost in $vmhosts) {

    #Get SNMP settings using new V2 esxcli interface

    $esxcli = Get-EsxCli -VMHost $vmhost -V2

    Write-Host "Gathering data from" $vmhost.Name "($step/$total)... " -ForegroundColor Cyan -NoNewline

    $esxcli.system.snmp.get.Invoke() | Select-Object `

        @{N="communities";E={[string]::Join(",",$_.communities)}},`

        enable, engineid, hwsrc, largestorage, loglevel, notraps, port, privacy, remoteusers, syscontact,`

        @{N="targets";E={[string]::Join(",",$_.targets)}},`

        users, v3targets | Export-Csv ".\$savefile.csv" -Append -NoTypeInformation -UseCulture

    $step += 1

    Write-Host "DONE!" -ForegroundColor Green

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$step = 1

$currentDir = (Get-Location).Path

$currentDir = $currentDir.TrimEnd('\')

$savefile = Read-Host -Prompt "`nEnter the name of the file where results will be saved (will be saved as $currentDir\<filename>.csv)"

$vmhosts = Get-VMHost | where {$_.ConnectionState -eq "Connected"}

$total = $vmhosts.count

foreach ($vmhost in $vmhosts) {

    #Get SNMP settings using new V2 esxcli interface

    $esxcli = Get-EsxCli -VMHost $vmhost -V2

    Write-Host "Gathering data from" $vmhost.Name "($step/$total)... " -ForegroundColor Cyan -NoNewline

    $esxcli.system.snmp.get.Invoke() | Select-Object `

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

        @{N="communities";E={[string]::Join(",",$_.communities)}},`

        enable, engineid, hwsrc, largestorage, loglevel, notraps, port, privacy, remoteusers, syscontact,`

        @{N="targets";E={[string]::Join(",",$_.targets)}},`

        users, v3targets | Export-Csv ".\$savefile.csv" -Append -NoTypeInformation -UseCulture

    $step += 1

    Write-Host "DONE!" -ForegroundColor Green

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$step = 1

$currentDir = (Get-Location).Path

$currentDir = $currentDir.TrimEnd('\')

$savefile = Read-Host -Prompt "`nEnter the name of the file where results will be saved (will be saved as $currentDir\<filename>.csv)"

$vmhosts = Get-VMHost | where {$_.ConnectionState -eq "Connected"}

$total = $vmhosts.count

foreach ($vmhost in $vmhosts) {

    #Get SNMP settings using new V2 esxcli interface

    $esxcli = Get-EsxCli -VMHost $vmhost -V2

    Write-Host "Gathering data from" $vmhost.Name "($step/$total)... " -ForegroundColor Cyan -NoNewline

    $esxcli.system.snmp.get.Invoke() | Select-Object `

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

        @{N="communities";E={[string]::Join(",",$_.communities)}},`

        enable, engineid, hwsrc, largestorage, loglevel, notraps, port, privacy, remoteusers, syscontact,`

        @{N="targets";E={[string]::Join(",",$_.targets)}},`

        users, v3targets | Export-Csv ".\$savefile.csv" -Append -NoTypeInformation -UseCulture

    $step += 1

    Write-Host "DONE!" -ForegroundColor Green

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

rgb99
Enthusiast
Enthusiast
Jump to solution

Yup, an easy fix! Thanks for this. I did see that the hostname was shown when I viewed the $esxcli variable, but I didn't know how to grab it. Thanks, LucD​!!

Reply
0 Kudos