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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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[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
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]
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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 ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for the response.
Hi Luc,
How do we store the value for the commandlet inside the variable. Just need to verify if snmp is enabled or not
$value = x
if ($value = "true") { do nothing } else { enable snmp }
Thx,
Lalit
Well..i though I got it but it is not the correct logic..
$a = get-vmhostsnmp | select Enabled | ft -Autosize # ft i used just for the display though it is not required
$b = $a.IsReadOnly
This returned 'false' and then it was simple to perform a check
if ($a -eq "true"){ do nothing} else {enable snmp}
That time snmp was disabled. So thot it returned the correct output. But its just returning whether it can be modified or not (at least thats what i think) .
Any ideas how do i achieve this.
Thx,
Lalit
Try this:
if ( !((get-vmhostsnmp).Enabled) ) { #if not true enable vmhostsnmp
write-host "Enabling vmhostsnmp with commands: "
# insert enable commands here
# insert enable commands here
# insert enable commands here
}
@CSI it dint work..
I have got into this unpleasant scenario where this script seems to fail in one or the other logic.
Just wnt the following:
1. Check if snmp is enabled,if enable do nothing and if not, enable snmp(i could have simply enabled it but with the lists of hosts, if it finds enabled for anyone it will fail).
2. Clear snmp targets.
Clear snmp targets using :
Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @() -RemoveTarget -TrapTargetToRemove $snmpObj.TrapTargets[0]
If set then clear them.
If they are not set then the script does not moves further to set them and throw up an error stating the targets should be set first. So need logic something like above in "Point 1"
My fairly simple script is as follows:
Param($esxcred=(Get-Credential))
#Defining variables
$SNMPCommunity = "a"
$trapCommunity = "public"
$trapDestination = "b"
$targetport = "162"
$ESXHosts = Get-Content -Path "path\snmp.txt"
foreach ($esxhost in $ESXHosts)
{
Write-Host "Connecting to $esxhost"
Connect-VIServer $esxhost -cred $esxcred
#Get SNMP Object
$snmpObj = Get-VMHostSnmp
# Verify and Enable snmp
$Enableval = (Get-VMHostSnmp).Enabled
if ($Enableval -eq "True")
{
Write "SNMP is already enabled" } else
{
Write "Enabling SNMP"
Set-VMHostSnmp -HostSnmp $snmpObj -Enabled:$true
}
#Clearing SNMP Targets
#Need a condition here to check if the targets are present or not, if not then just go ahead and set snmp and disconnect. I could have done that via a #function in shell script but not sure how to do it here
Write "Clearing existing snmp targets"
Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @() -RemoveTarget -TrapTargetToRemove $snmpObj.TrapTargets[0]
#Setting the SNMP Target
Write "Setting the new snmp targets"
Set-VMHostSnmp -HostSnmp $snmpObj -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination -TargetPort $targetport
#Setting SNMP Configuration
Write "Setting SNMP Configuration"
Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity $SNMPCommunity
#Testing the applied settings
#This will throw an error if the targent agents are not configured. We can ignore this as of now. Uncomment the hash marks when required.
Write "Testing SNMP Configuration"
Test-VMHostSnmp -HostSnmp $snmpObj
#Disconnecting from the server
Write "Disconnecting from the server $esxhost.."
Disconnect-VIServer $esxhost -Confirm:$false
}
Also I am not sure if its checking 'foreach'. Because while clearing snmp targets it fails with the error:
Clearing existing snmp targets
Cannot index into a null array.
At :line:47 char:112
+ Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity @() -RemoveTarget -TrapTargetToRemove $snmpObj.TrapTargets[ <<<< 0]
which should not happen.
Any pointers/suggestions appreciated.
Thx,
Lalit