Posts RSS Comments RSS 253 Posts and 411 Comments till now

Setting SNMP configuration in VMWare using Powershell

I have a ton of respect for the VMWare guys and the incredibly useful set of cmdlets they provide (FOR FREE) found HERE, but man have I been banging my head against the wall trying to simply set the SNMP configuration on my ESX hosts.

To help ease the pain for the REST of the world here are some thoughts/gotchas.

– You must have the object return from Get-VMHostSNMP to use Set-VMHostSNMP or Test-VMHostSNMP. Simply providing the name will not work
– You cannot use the name with Get-VMHostSNMP. It appears you must connect to the actual ESX host (using connect-viserver) and then run Get-VMHostSNMP
– Once you “get” the VMHostSNMP object you can reuse without worrying about the connect-viserver command.
– You need to add a ReadOnlyCommunity to each host
– You need to Enable traps with the -enable:$true parameter
– TargetCommunity, TargetPort, TargetHost parameters require that you also pass -addtarget
– You need to enable SNMP on the ESX firewall

You may find the script below useful for getting a VMHostSNMP object for each ESXHost on your Virtual Center

Using the script below you can then do things like
$SNMPHostObjects = .\Get-SNMPHost.ps1 $VirtualCenter

To Add a Community
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -ReadOnlyCommunity “YourCommunity”}

To Enable
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -Enabled:$true }

To Add a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1” -TargetCommunity “YourCommunity” -AddTarget }

To Test
$SNMPHostObjects | %{ Test-VMHostSnmp $_ }

To Remove a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1” -removeTarget}

6 Responses to “Setting SNMP configuration in VMWare using Powershell”

  1. on 21 Mar 2010 at 12:33 pmaenagy

    Thanks for doing the heavy lifting. I was about to start writing something myself when I found this.

    Is there any way to limit the changes to a particular DataCenter or Cluster?

    Is there any way for the output to include the name of the host?

  2. on 22 Mar 2010 at 6:44 amtshell

    I suppose you could just get the cluster or datacenter and the do a foreach through each ESX/vSphere host and set it that way.

    What output specifically do you want the host in?

  3. on 05 Apr 2010 at 1:56 pmaenagy

    When I run the first line ‘$SNMPHostObjects = .\Get-SNMPHost.ps1 $VirtualCenter’ the script prompts me for my Windows credentials and connects to vCenter and then generates the following error for each ESXi 4 host. Please excuse the noob question, my PowerShell knowledge is still at the copy-and-paste stage.

    Connect-VIServer : Cannot complete login due to an incorrect user name or passw
    ord.
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:11 char:20
    + Connect-VIServer <<<< $esxhost.name -cred $ESXCreds | out-null
    Get-VMHostSnmp : The requested operation is only supported when connected direc
    tly to ESX host.
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:12 char:30
    + $SNMPHost = Get-VMHostSnmp <<<<
    Add-Member : Cannot bind argument to parameter 'InputObject' because it is null
    .
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:13 char:26
    + $SNMPHost | Add-Member <<<< -MemberType NoteProperty -Name ESXHost -Value
    $esxhost.name

  4. on 06 Apr 2010 at 12:49 pmaenagy

    When I run ‘$SNMPHostObjects = .\Get-SNMPHost.ps1 $VirtualCenter’ the script prompts me for my Windows credentials and connects to vCenter and then generates the following error for every ESXi 4 host.

    Connect-VIServer : Cannot complete login due to an incorrect user name or passw
    ord.
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:11 char:20
    + Connect-VIServer <<<< $esxhost.name -cred $ESXCreds | out-null
    Get-VMHostSnmp : The requested operation is only supported when connected direc
    tly to ESX host.
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:12 char:30
    + $SNMPHost = Get-VMHostSnmp <<<<
    Add-Member : Cannot bind argument to parameter 'InputObject' because it is null
    .
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:13 char:26
    + $SNMPHost | Add-Member <<<< -MemberType NoteProperty -Name ESXHost -Value
    $esxhost.name

  5. on 09 Apr 2010 at 3:45 amaenagy

    Oops. Sorry for the double-post. IE a little wonky.

  6. on 13 Apr 2010 at 3:16 pmaenagy

    OK, I figured out that the prompt is for the ESXi host credentials, not vCenter.

    Now my problem is that for each host in vCenter I get the following error:

    Get-VMHostSnmp : There are more than one default VC Server connections. Please
    use the Server parameter to specify the desired connections.
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:12 char:30
    + $SNMPHost = Get-VMHostSnmp <<<<
    Add-Member : Cannot bind argument to parameter 'InputObject' because it is null
    .
    At E:\data\CTC_Support\ctc\PowerCLI\Get-SNMPHost.ps1:13 char:26
    + $SNMPHost | Add-Member <<<< -MemberType NoteProperty -Name ESXHost -Value
    $esxhost.name

    Even if I remove all hosts, reset the configuration on one host and add it back to vCenter I get the error message.

    Any ideas?

Trackback this post | Feed on Comments to this post

Leave a Reply

You must be logged in to post a comment.