Public/Set-IdracSNMP.ps1

Function Set-IdracSNMP{
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='None')]
    Param (
        [Parameter(Mandatory = $true)]
        [string]$iDRACName,

        [string]$AgentCommunity = $null,

        [string[]]$AlertServer = $null,

        [System.Management.Automation.PSCredential]$Credential
    )

    begin{
        $commandArray = @()

        if ($PSBoundParameters.ContainsKey('AgentCommunity')){
            $commandArray += "racadm set idrac.snmp.AgentCommunity $AgentCommunity"
        }

        if ($PSBoundParameters.ContainsKey('AlertServer')){
            $i = 1
            foreach ($item in $AlertServer){
                $commandArray += "racadm set idrac.snmp.alert.$i.DestAddr $item"
                $commandArray += "racadm set idrac.snmp.alert.$i.Enable Enabled"
                if ($i -ge 9){ # Valid range is 1 to 8
                    break
                }
                $i++
            }
        }
        $session = New-iDRACSession -iDRACName $iDRACName -Credential $Credential
    }

    process{

        foreach ($line in $commandArray){
            if($PSCmdlet.ShouldProcess( "Verbose Description !", "Set Resource " + "Caption")){
                $result = Invoke-SshCommand -Command $line -SSHSession $session
                $msg = $result.Host.ToUpper() + " - " + $result.Output
                Write-Verbose -Message $msg
            }
        }
    }

    End{
        $session | Remove-SSHSession | Out-Null
    }
}