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 !
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 # 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
Thank you very much, worked like a charm!
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
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,
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
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?
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. :-(
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 ?
No problem. Here is what I ran, mostly from the original post above, with appropriate data filled with asterisks:
$esxname = "*************************"
$community = "***********"
$trapdestination = "188.8.131.52"
# 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
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.
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! :-)
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.
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?
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