Automation

 View Only
  • 1.  PowerCLI Script for SNMP

    Posted Apr 09, 2020 07:11 PM

    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



  • 2.  RE: PowerCLI Script for SNMP

    Posted Apr 09, 2020 07:47 PM

    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.



  • 3.  RE: PowerCLI Script for SNMP

    Posted Apr 07, 2022 01:22 PM

    Hi,

     

    How we can apply this at the cluster level.

     

    Thanks



  • 4.  RE: PowerCLI Script for SNMP

    Posted Apr 07, 2022 02:11 PM

    You could do

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


  • 5.  RE: PowerCLI Script for SNMP

    Posted Apr 08, 2022 09:27 AM

    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

     

     



  • 6.  RE: PowerCLI Script for SNMP

    Posted Apr 08, 2022 09:38 AM

    How did you run "esxcli system snmp get"?

    Did you use Get-EsxCli before?



  • 7.  RE: PowerCLI Script for SNMP

    Posted Apr 08, 2022 09:42 AM

    Hi,

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

     

    Thanks

     



  • 8.  RE: PowerCLI Script for SNMP

    Posted Apr 08, 2022 10:15 AM

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