VMware Cloud Community
TechTimeWithTiM
Contributor
Contributor

PowerCLI Script for SNMP

I am looking for some help with a script that I can periodically run that would verify that an SNMP read community is set on a ESXi 6.7 host and if it is not set, set the read community to a predefined string.

Thank you,

Tim

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

You can use the Set-VMHostSnmp cmdlet, but that requires you connect to the ESXi node (not the vCenter).

I find it easier to do this with esxcli and only connect to the vCenter.

Something like this for example

$esxName = 'MyEsx'

$community = 'community1'


$esx = Get-VMHost -Name $esxName

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

$snmpGet = $esxcli.system.snmp.get.Invoke()

if($snmpGet.community -eq $null -or $snmpGet.communities.Split(',') -notcontains $community){

    $snmpSet = @{

        communities = if($snmpGet.communities -eq $null){$community}else{"$($snmpGet.communities -join ','),$community"}

    }

    $esxcli.system.snmp.set.Invoke($snmpSet)

}

Instead of doing this for 1 ESXi node, you can, of course, do this in a loop, and run the code for each ESXi node in for example a cluster.


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

Reply
0 Kudos
Uday1990
Contributor
Contributor

Hi,

 

How we can apply this at the cluster level.

 

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

You could do

$esx = Get-Cluster -Name cluster | Get-VMHost -Name $esxName


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

Reply
0 Kudos
Uday1990
Contributor
Contributor

Hi,

I have run the powercli command but it's not working I am getting below output.

esxcli system snmp get

Authentication:
Communities: 1234567
Enable: false
Engineid:
Hwsrc: indications
Largestorage: true
Loglevel: warning
Notraps:
Port: 161
Privacy:
Remoteusers:
Syscontact:
Syslocation:
Targets:
Users:
V3targets:

__________________________________________________________________________________________

but if I am trying to run the below esxcli command on a single ESXi host, it is working fine.

esxcli system snmp set --communities 1234567

esxcli system snmp set --enable true

esxcli system snmp get

______________________________________

Authentication:
Communities: 1234567
Enable: true
Engineid: 80001ADC059684893741649339717
Hwsrc: indications
Largestorage: true
Loglevel: warning
Notraps:
Port: 161
Privacy:
Remoteusers:
Syscontact:
Syslocation:
Targets:
Users:
V3targets:

Could you please help?

Thanks

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

How did you run "esxcli system snmp get"?

Did you use Get-EsxCli before?


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

Reply
0 Kudos
Uday1990
Contributor
Contributor

Hi,

No, I am using  "esxcli system snmp get" to validate the configuration.

 

Thanks

 

Reply
0 Kudos
LucD
Leadership
Leadership

Then I'll move your post to the ESXi Community, this is the PowerCLI community


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

Reply
0 Kudos