private/transport/Invoke-SnmpGet.ps1

function Invoke-SnmpGet {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.Net.IPEndPoint]$Endpoint,

        [Parameter(Mandatory)]
        [string[]]$Oid,

        [Parameter(Mandatory)]
        [SnmpVersion]$Version,

        [Parameter(Mandatory)]
        [int]$Timeout,

        [string]$Community,

        [string]$Username,

        [string]$AuthenticationProtocol,

        [securestring]$AuthenticationPassword,

        [string]$PrivacyProtocol,

        [securestring]$PrivacyPassword
    )

    # Resolve SNMP version code
    $VersionCode = Resolve-SnmpVersion $Version

    # Create variable list
    $Variables = [System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]]::new()

    foreach ($id in $Oid) {
        $obj = [Lextm.SharpSnmpLib.Variable]::new([Lextm.SharpSnmpLib.ObjectIdentifier]::new($id))
        $Variables.Add($obj)
    }

    switch ($Version) {
        # SNMP v1 and v2c
        { $_ -in ([SnmpVersion]::V1, [SnmpVersion]::V2C) } {
            try {
                return [Lextm.SharpSnmpLib.Messaging.Messenger]::Get(
                    $VersionCode,
                    $Endpoint,
                    $Community,
                    $Variables,
                    $Timeout
                )
            }
            catch {
                throw "SNMP $Version error: $($_.Exception.Message)"
            }
        }

        # SNMP v3
        ([SnmpVersion]::V3) {
            # Build auth and privacy providers
            $AuthProvider = Resolve-SnmpAuthenticationProvider `
                -AuthenticationProtocol $AuthenticationProtocol `
                -AuthenticationPassword $AuthenticationPassword

            $PrivacyProvider = Resolve-SnmpPrivacyProvider `
                -AuthenticationProvider $AuthProvider `
                -PrivacyProtocol $PrivacyProtocol `
                -PrivacyPassword $PrivacyPassword

            # SNMP Discovery
            try {
                $Discovery = [Lextm.SharpSnmpLib.Messaging.Messenger]::GetNextDiscovery([Lextm.SharpSnmpLib.SnmpType]::GetRequestPdu)
                $Report = $Discovery.GetResponse($Timeout, $Endpoint)
            }
            catch {
                throw "SNMP $Version discovery failed: $($_.Exception.Message)"
            }

            # SNMP Request
            try {
                $Request = [Lextm.SharpSnmpLib.Messaging.GetRequestMessage]::new(
                    $VersionCode,
                    [Lextm.SharpSnmpLib.Messaging.Messenger]::NextMessageId,
                    [Lextm.SharpSnmpLib.Messaging.Messenger]::NextRequestId,
                    [Lextm.SharpSnmpLib.OctetString]::new($Username),
                    $Variables,
                    $PrivacyProvider,
                    $Report
                )
                $Reply = [Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension]::GetResponse(
                    $Request,
                    $Timeout,
                    $Endpoint
                )
            }
            catch {
                throw "SNMP $Version request failed $($_.Exception.Message)"
            }

            # Check for agent errors
            if ($Reply.Scope.Pdu.ErrorStatus.ToInt32() -ne 0) {
                throw (
                    "SNMP {0} error returned by device {1}. ErrorStatus: {2}, ErrorIndex: {3}" -f
                    $Version,
                    $Endpoint.Address,
                    $Reply.Scope.Pdu.ErrorStatus,
                    $Reply.Scope.Pdu.ErrorIndex
                )
            }
            return $Reply.Scope.Pdu.Variables | ConvertFrom-SnmpVariable `
                -ComputerName $Endpoint.Address.ToString() `
                -Version $Version
        }

        default {
            throw "Unsupported SNMP version: $Version"
        }
    }
}