Hi,
I have the following code to secure SNMP with SNMPv3. Using esxcli directly on the host, everything works fine, however the converted commands to powershell are failing at steps 3 and 5 with the following error:
Update Daemon runtime state failed: Agent not responding, connect uds socket(/var/run/snmp.ctl) failed 2, err= No such file or directory
At line:1 char:5
+ $esxcli.system.snmp.set.Invoke(@{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ViError
+ FullyQualifiedErrorId : VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViError
Here's the code. Thanks for any ideas!
# SNMPv3 variables
$authProtocol = "SHA1" # Authentication protocol
$privProtocol = "AES128" # Privacy protocol
$snmpUser1 = "snmpv3" # SNMPv3 username
$authPassword = "authpassword1" # SNMPv3 auth password
$privPassword = "privpassword1" # SNMPv3 privacy password
# Array of ESXi hostnames
$vmHost = "host.domain.com"
Write-Host "=== Processing host: $vmHost ===" -ForegroundColor Cyan
# Connect directly to the ESXi host
$hostConnection = Connect-VIServer -Server $vmHost -User "root" -Password "myrootpassword"
# Get the VMHost object for this session
$vmhost = Get-VMHost -Server $hostConnection
# Create EsxCli object
$esxcli = Get-EsxCli -VMHost $vmhost -V2
# Step 1: Ensure SNMP service is enabled
Write-Host "Enabling SNMP service..."
$esxcli.system.snmp.set.Invoke(@{ "enable" = $true })
# Step 2: Reset old SNMP v2 configuration (optional)
Write-Host "Resetting old SNMP v2 configuration..."
$esxcli.system.snmp.set.Invoke(@{ "communities" = "reset"; "targets" = "reset" })
# Step 3: Authentication protocols
Write-Host "Setting authentication protocols"
$esxcli.system.snmp.set.Invoke(@{
"authentication" = "SHA1"
"privacy" = "AES128" })
# Step 4: Generate SNMPv3 password hashes
Write-Host "Generating SNMPv3 password hashes..."
$hashes = $esxcli.system.snmp.hash.Invoke(@{
authhash = $authPassword
privhash = $privPassword
rawsecret = $true
})
$authHashValue = $hashes.authhash
$privHashValue = $hashes.privhash
# Step 5: Configure SNMPv3 user
Write-Host "Configuring SNMPv3 user..."
$esxcli.system.snmp.set.Invoke(@{
"users" = "$snmpUser1/$authHashValue/$privHashValue/priv"
})
# Step 6: Verify SNMP configuration
Write-Host "Verifying SNMP configuration..."
$snmpStatus = $esxcli.system.snmp.get.Invoke()
$snmpStatus
# Step 7: Disconnect from the host
Disconnect-VIServer -Server $hostConnection -Confirm:$false
-------------------------------------------