-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSet-SNMPConfig.ps1
43 lines (39 loc) · 1.61 KB
/
Set-SNMPConfig.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
param (
[Parameter(Mandatory=$true)] [String[]] $ROCommunity = "public", # String array of Read-Only communities
[Parameter(Mandatory=$true)] [Object[]] $VMHost, # VMHost(s) to be configured
[String] $SysContact, # System Contact, typically an email address or name
[String] $SysLocation # System Location
)
# For Each VMHost to be configured
foreach ($targetHost in $VMHost) {
Write-Output "($($targetHost.Name)) Configuring SNMP..."
## Invoke the ESXCLI object
$esxcli = Get-EsxCli -VMHost $targetHost -V2
## Create the snmp arguments
$arguments = $esxcli.system.snmp.set.CreateArgs()
## Enable the configuration
$arguments.enable = $true
## Set communities
$arguments.communities = $ROCommunity
## Set the port
$arguments.port = 161
## Set the System Contact
$arguments.syscontact = $SysContact
## Set the System Location
$arguments.syslocation = $SysLocation
## Apply the configuration
$esxcli.system.snmp.set.Invoke($arguments) | Write-Verbose
## Get the SNMP service
$snmpd = Get-VMHostService -VMHost $vmhost | where {$_.Key -eq "snmpd"}
## Start SNMP service - if not configured properly it will fail
$snmpd | Start-VMHostService | Write-Verbose
## Set the service to start at boot
$snmpd | Set-VMHostService -Policy "On" | Write-Verbose
## Check to see if the firewall is configured
$firewallRule = $targetHost | Get-VMHostFirewallException | Where-Object {$_.Name -eq "SNMP Server"}
### If the firewall exception is not enabled
If (-not $firewallRule.Enabled) {
### Enable it
$firewallRule | Set-VMHostFirewallException -Enabled | Write-Verbose
}
}