1 2 Previous Next 22 Replies Latest reply on Apr 7, 2012 5:27 AM by lalitpathak1504

    Set SNMP thru PowerCLI

    BastiaanvanH Novice

      Hi,

       

      I want to set the SNMP parameters on a ESX host thru PowerCLI. Problem is that it generates an error on the last line.

       

      Error:

      Exception calling "ReconfigureSnmpAgent" with "1" argument(s): "A general syste

      m error occurred: Set one community string or trap target for agent to listen o

      n first."

      At line:1 char:33

      + $SNMPsystem.ReconfigureSnmpAgent <<<< ($SNMPconfig)

          + CategoryInfo          : NotSpecified: ( [], MethodInvocationException

          + FullyQualifiedErrorId : DotNetMethodException

       

      The script:

      $ESXConnect=Connect-VIServer -Server "srv-esxi01.mydomain.local"

      $ESXHost=Get-VMHost -Name "srv-esxi01.mydomain.local"

      $SNMPhs = Get-View $ESXHost.ID

      $SNMPsys = $SNMPhs.ConfigManager.SNMPSystem

      $SNMPsystem = Get-View $SNMPhs.ConfigManager.SNMPSystem

      $SNMPconfig = New-Object VMWare.Vim.HostSnmpConfigSpec

      $SNMPtrapHost = New-Object vmware.Vim.HostSnmpDestination

      $SNMPtrapHost.hostName = "1.1.1.1"

      $SNMPtrapHost.community = "public"

      $SNMPtrapHost.port = 162

      $SNMPconfig.readOnlyCommunities = "ReadOnly"

      $SNMPconfig.trapTargets = $SNMPtrapHost

      $SNMPconfig.Enabled = 1

      $SNMPsystem.ReconfigureSnmpAgent($SNMPconfig)

      $SNMPsystem.SendTestNotification

       

      I think i'm setting al the correct mandatory properties. But i'm lost. Any help is very welcome!

       

      Regards,

       

      Bastiaan

        • 1. Re: Set SNMP thru PowerCLI
          LucD Guru
          Community WarriorsvExpertUser Moderators

          You need to connect to the ESX(i) server not the vCenter.

           

          Why don't you use the Set-VMHostSnmp and Test-VMHostSnmp cmdlets ?

           

           

           

           

           

          ____________

          Blog: LucD notes

          Twitter: lucd22

          • 2. Re: Set SNMP thru PowerCLI
            BastiaanvanH Novice

            Ok, didn't knew thoose.

             

            I'm not an expert on writing PowerCLI/Powershell scripts. So do you perhaps have an example to enable SNMP, set read community, and send traps to host.

             

            would be greatly appreciated !

             

            Bastiaan

            • 3. Re: Set SNMP thru PowerCLI
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              Sure, try this

              $esxName = <ESX-hostname>
              $esxImpl = Get-VMHost -Name $esxName
              $trapDestination = <mgmt-hostname>
              $trapCommunity = "public"
              
              # Connect to the ESX server
              $esxConnect = Connect-VIServer -Server $esxName -Credential (Get-Credential)
              
              # Get snmp object
              $snmpObj = Get-VMHostSnmp -Server $defaultViServer
              
              # Clear snmp configuration
              Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @() -RemoveTarget $snmpObj.TrapTargets[0]
              
              # Enable snmp
              Set-VMHostSnmp -HostSnmp $snmpObj -Enabled:$true
              
              # Set read-only community
              Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity "public"
              
              # Define trap target
              Set-VMHostSnmp -HostSnmp $snmpObj -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination
              
              # Test snmp
              Test-VMHostSnmp -HostSnmp $snmpObj
              
              # Disconnect from ESX server
              Disconnect-VIServer -Server $esxConnect -Confirm:$fals
              

              This is just a sample, comment out the lines you don't need.

               

               

               

              ____________

              Blog: LucD notes

              Twitter: lucd22

              • 4. Re: Set SNMP thru PowerCLI
                BastiaanvanH Novice

                Thank you very much, worked like a charm!

                • 5. Re: Set SNMP thru PowerCLI
                  dwcrist Enthusiast

                  I can get the initial commands to run, but whenever I try to use Set-VMHostSnmp, I get an error.  Here's my input that is supposed to clear the SNMP info:

                   

                  [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @
                  () -RemoveTarget $snmpObj.TrapTargets[0]

                   

                  Here's the error:

                   

                  Set-VMHostSnmp : Parameter set cannot be resolved using the specified named parameters.
                  At line:1 char:15

                   

                  And I get this for every set-vmhostsnmp cmdlet.  The get-vmhostsnmp works OK using the same variables.

                   

                  Thanks for any help,

                  Doug

                  • 6. Re: Set SNMP thru PowerCLI
                    LucD Guru
                    vExpertCommunity WarriorsUser Moderators

                    The Get-VMHostSnmp cmdlet has no specific parameters.

                    The Set-VMHostSnmp cmdlet has 3 parameter sets, you want to use the 3th parameterset.

                    And in there you have to use the -TrapTargetToRemove parameter to remove a specific trap target.

                     

                    You should do something like this

                     

                    Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @() -RemoveTarget -TrapTargetToRemove $snmpObj.TrapTargets[0]


                    • 7. Re: Set SNMP thru PowerCLI
                      dwcrist Enthusiast

                      Thanks for the quick response.   Unfortunately, I got the same error.

                       

                      What confused me is that character 15 is the very first parameter, -HostSnmp.  Or is this just PowerCLI or PowerShell in general not being very specific?

                       

                      -Doug

                      • 8. Re: Set SNMP thru PowerCLI
                        dwcrist Enthusiast

                        Here's what's interesting:  It will run error-free if I put only 'Enabled' or '-ReadOnlyCommunity'.  But I get the char 15 error whenever I try any other parameter, singly or in multiples.  :-(

                        • 9. Re: Set SNMP thru PowerCLI
                          LucD Guru
                          Community WarriorsvExpertUser Moderators

                          I assume you obtained the object like this

                          $snmpObj = Get-VMHostSnmp

                          Normally the PS error messages are quite accurate.

                          It seems to indicate that PS doesn't recognise the parameterset it should use.

                          Normally you get this when you mix parameters form 2 parametersets together or when a key parameter for a specific parameterset is not present.

                           

                          Perhaps you can include the complete sequence of cmdlets you are using before you come to this one ?

                          • 10. Re: Set SNMP thru PowerCLI
                            dwcrist Enthusiast

                            No problem.  Here is what I ran, mostly from the original post above, with appropriate data filled with asterisks:

                             

                            $esxname = "*************************"
                            $community = "***********"
                            $trapdestination = "99.99.99.99"

                             

                            # Connect to the ESX server
                            $esxConnect = Connect-VIServer -Server $esxname -Credential (Get-Credential)

                             

                            # Get snmp object
                            $snmpObj = Get-VMHostSnmp -Server $esxConnect

                             

                            # Clear snmp configuration
                            Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @() -RemoveTarget $snmpObj.TrapTargets[0]

                             

                            When asked for credentials I gave it the root credentials for the ESXi host.

                            The last cmdlet seemed to just hang for 5-10 minutes.  Eventually I pressed <Enter> and it came back with the char-15 error.

                             

                            Thanks,

                            Doug

                            • 11. Re: Set SNMP thru PowerCLI
                              dwcrist Enthusiast

                              Here's something I found on another site that helped me add a target:

                               

                              - TargetCommunity, TargetPort, TargetHost parameters require that you also pass -addtarget

                               

                              So I can now add a target.  Unfortunately, I now have two identical targets, but don't seem to be able to remove one or all of them.  But progress is being made!  :-)

                               

                              Thanks,

                              Doug

                              • 12. Re: Set SNMP thru PowerCLI
                                dwcrist Enthusiast

                                Here is a good link:  http://bsonposh.com/archives/809

                                 

                                Using those commands I was able to remove a particular target, which was what I needed to do.  I wasn't able to get Luc's command to remove all targets to work in my environment.

                                 

                                -Doug

                                • 13. Re: Set SNMP thru PowerCLI
                                  vLifeOfBrian Novice

                                  How is this supposed to work with VMware ESXi 4.1 in lockdown mode? Do you know of a workaround to pull the results of Get-VMHostSnmp through vCenter since you are not able to connect directly to the host in lockdown mode?

                                  • 14. Re: Set SNMP thru PowerCLI
                                    LucD Guru
                                    vExpertUser ModeratorsCommunity Warriors

                                    Only the vpxuser will be able to connect, but since that is internal to vCenter it's a non-option (and you don't know the password either).

                                    See The New Lockdown Mode in ESXi 4.1 for a good overview.

                                     

                                    You can of course use the Set-TkeVMHostLockdown function from the VI Toolkit for Windows Community Extensions module to disable the lockdown mode temporarily

                                    1 2 Previous Next