Getting the MAC address using the Get-SNMP Cmdlet

Getting the MAC address of an SNMP agent can be accomplished using the Get-SNMP cmdlet.
By default the MAC address will automatically be encoded in the format “00:0C:29:C6:31:07″. If you are using an older version of the cmdlet that does not do this automatically, or you have set -Config “HexMACAddress=false” the code below can be used to manually convert the MAC address to hex.
The following code is a PowerShell script that uses our NetCmdlets product allowing for simple access to SNMP OIDs in PowerShell.

#This approach is only necessary if using an old version (V3 or earlier) or HexMACAddress=false
#In current versions this is done automatically by the cmdlet
$oid = Get-SNMP -OID -Agent $hostname -community public 
$Encode = new-object "System.Text.UTF8Encoding" 
$bytearray = $Encode.GetBytes($oid.OIDValue) 

$res = "" 

Foreach ($i in $bytearray) { 
	$res = $res + $i.ToString("X").PadLeft(2,"0") + ":" 

$res = $res.Substring(0,$res.Length - 1)