none
arp -a <ip_address> RRS feed

  • Question

  • This is a follow-up of my previous post, where I was asking if there is a cmdlet like "arp -a" in PowerShell which could provide an object as an output and not simply text.

    The answer was very useful and it worked. But the static method Get() cannot be used without specifyng a target, single IP address. If I correctly understood the source code in the linked post, the method Get() looks for the system ARP table, so it will anyway provide multiple outputs.

    But what if I wanted to run Get() for a single, known IP?

    Or: is there another (built-in or custom) cmdlet which can run a command like "arp -a <ip_address>" and return an object?

    Wednesday, March 11, 2015 8:28 PM

Answers

  • Hi Henry,

    in that case you could use the function I built to make using it more comfortable (use together with c# code from previous post):

    function Invoke-AddressResolutionProtocol
    {
    	<#
    		.SYNOPSIS
    			A function that implements the functionality of ARP.exe
    		
    		.DESCRIPTION
    			A function that implements the functionality of ARP.exe
    		
    		.PARAMETER Clear
    			Alias:  c
    			ParSet: Clear
    			Clears all locally stored ARP entries.
    		
    		.PARAMETER Delete
    			Alias:  d
    			ParSet: Delete
    			Deletes specific ARP entries. Either enter a string to match IP, or enter an ArpEntry to delete a specific entry.
    		
    		.PARAMETER Get
    			Alias:  all, a, g
    			ParSet: Get
    			Retrieves all local ARP entries.
    	
    		.PARAMETER Filter
    			Default: *
    			ParSet:  Get
    			Filters the output which entries are returned (-like comparison of IP Address)
    		
    		.PARAMETER NewAdapterID
    			ParSet: New
    			The ID of the adapter to use for the newly created ARP entry.
    			Note: This is not the WMI adapter id. Check the adapter ID of other ARP entries to get the correct adapter.
    		
    		.PARAMETER NewIP
    			Alias:  ip
    			ParSet: New
    			The IP Address of the newly created ARP entry.
    		
    		.PARAMETER NewMAC
    			Alias:  mac
    			ParSet: New
    			The MAC Address of the newly created ARP entry.
    		
    		.EXAMPLE
    			PS C:\> arp -a
    	
    			Retrieves all ARP entries.
    	
    		.EXAMPLE
    			PS C:\> arp "8.8.8.8" "00-FF-00-FF-00-FF" 11
    	
    			Creates a new ARP entry, that sets the IP 8.8.8.8 to to the MAC 00-FF-00-FF-00-FF on Adapter 11
    		
    		.NOTES
    			Supported Interfaces:
    			------------------------
    			
    			Author:       Friedrich Weinmann
    			Company:      die netzwerker Computernetze GmbH
    			Created:      30.10.2014
    			LastChanged:  30.10.2014
    			Version:      1.0
    	#>
    	[CmdletBinding(DefaultParameterSetName = "Get")]
    	Param (
    		[Parameter(ParameterSetName = "Get")]
    		[Alias('all', 'a', 'g')]
    		[switch]
    		$Get,
    		
    		[Parameter(ParameterSetName = "Get", Position = 0)]
    		[string]
    		$Filter = "*",
    		
    		[Parameter(ParameterSetName = "Delete", Mandatory = $true)]
    		[Alias('d')]
    		[object]
    		$Delete,
    		
    		[Parameter(ParameterSetName = "Clear")]
    		[Alias('c')]
    		[switch]
    		$Clear,
    		
    		[Parameter(ParameterSetName = "New", Mandatory = $true, Position = 0)]
    		[Alias('ip')]
    		[string]
    		$NewIP,
    		
    		[Parameter(ParameterSetName = "New", Mandatory = $true, Position = 1)]
    		[Alias('mac')]
    		[Netzwerker.Network.Diagnostics.PhysicalAddress]
    		$NewMAC,
    		
    		[Parameter(ParameterSetName = "New", Position = 2)]
    		[int]
    		$NewAdapterID
    	)
    	
    	Begin
    	{
    		Write-Debug "[Start] [Performing ARP Actions]"
    		
    		# Get active ParameterSet
    		$ParSet = $PSCmdlet.ParameterSetName
    		Write-Debug "Active Parameterset: $ParSet"
    	}
    	Process
    	{
    		switch ($ParSet)
    		{
    			"Get" { [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Where-Object { $_.Address.ToString() -like $Filter } }
    			"Delete"
    			{
    				foreach ($Item in $Delete)
    				{
    					$Type = $Item.GetType().FullName
    					switch ($Type)
    					{
    						"System.String" { [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Where-Object { $_.Address.ToString() -like $Item } | %{ $_.Delete() } }
    						"Netzwerker.Network.Diagnostics.ArpEntry" { $Item.Delete() }
    					}
    					
    				}
    			}
    			"Clear"
    			{
    				$Adapters = [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Select-Object -ExpandProperty IndexAdapter -Unique
    				$Adapters | %{ [Netzwerker.Network.Diagnostics.ArpHost]::Clear($_) }
    			}
    			"New"
    			{
    				if (!$PSBoundParameters["NewAdapterID"]) { $NewAdapterID = [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Group-Object IndexAdapter -NoElement | Sort-Object Count | Select-Object -Last 1 -ExpandProperty Name }
    				[Netzwerker.Network.Diagnostics.ArpHost]::New($NewIP, $NewMAC, $NewAdapterID)
    			}
    		}
    	}
    	End
    	{
    		# Write closing line
    		Write-Debug "[End] [Performing ARP Actions]"
    	}
    }
    New-Alias -Name "arp" -Value "Invoke-AddressResolutionProtocol" -Force -Option 'AllScope'

    Cheers,
    Fred

    Edit: I just went ahead, packaged the whole thing into a single script file and published it in the gallery.


    There's no place like 127.0.0.1



    • Edited by FWN Friday, March 13, 2015 11:29 AM
    • Marked as answer by Henry_8198 Monday, March 16, 2015 2:38 PM
    Thursday, March 12, 2015 1:38 PM

All replies

  • Hi Henry,

    in that case you could use the function I built to make using it more comfortable (use together with c# code from previous post):

    function Invoke-AddressResolutionProtocol
    {
    	<#
    		.SYNOPSIS
    			A function that implements the functionality of ARP.exe
    		
    		.DESCRIPTION
    			A function that implements the functionality of ARP.exe
    		
    		.PARAMETER Clear
    			Alias:  c
    			ParSet: Clear
    			Clears all locally stored ARP entries.
    		
    		.PARAMETER Delete
    			Alias:  d
    			ParSet: Delete
    			Deletes specific ARP entries. Either enter a string to match IP, or enter an ArpEntry to delete a specific entry.
    		
    		.PARAMETER Get
    			Alias:  all, a, g
    			ParSet: Get
    			Retrieves all local ARP entries.
    	
    		.PARAMETER Filter
    			Default: *
    			ParSet:  Get
    			Filters the output which entries are returned (-like comparison of IP Address)
    		
    		.PARAMETER NewAdapterID
    			ParSet: New
    			The ID of the adapter to use for the newly created ARP entry.
    			Note: This is not the WMI adapter id. Check the adapter ID of other ARP entries to get the correct adapter.
    		
    		.PARAMETER NewIP
    			Alias:  ip
    			ParSet: New
    			The IP Address of the newly created ARP entry.
    		
    		.PARAMETER NewMAC
    			Alias:  mac
    			ParSet: New
    			The MAC Address of the newly created ARP entry.
    		
    		.EXAMPLE
    			PS C:\> arp -a
    	
    			Retrieves all ARP entries.
    	
    		.EXAMPLE
    			PS C:\> arp "8.8.8.8" "00-FF-00-FF-00-FF" 11
    	
    			Creates a new ARP entry, that sets the IP 8.8.8.8 to to the MAC 00-FF-00-FF-00-FF on Adapter 11
    		
    		.NOTES
    			Supported Interfaces:
    			------------------------
    			
    			Author:       Friedrich Weinmann
    			Company:      die netzwerker Computernetze GmbH
    			Created:      30.10.2014
    			LastChanged:  30.10.2014
    			Version:      1.0
    	#>
    	[CmdletBinding(DefaultParameterSetName = "Get")]
    	Param (
    		[Parameter(ParameterSetName = "Get")]
    		[Alias('all', 'a', 'g')]
    		[switch]
    		$Get,
    		
    		[Parameter(ParameterSetName = "Get", Position = 0)]
    		[string]
    		$Filter = "*",
    		
    		[Parameter(ParameterSetName = "Delete", Mandatory = $true)]
    		[Alias('d')]
    		[object]
    		$Delete,
    		
    		[Parameter(ParameterSetName = "Clear")]
    		[Alias('c')]
    		[switch]
    		$Clear,
    		
    		[Parameter(ParameterSetName = "New", Mandatory = $true, Position = 0)]
    		[Alias('ip')]
    		[string]
    		$NewIP,
    		
    		[Parameter(ParameterSetName = "New", Mandatory = $true, Position = 1)]
    		[Alias('mac')]
    		[Netzwerker.Network.Diagnostics.PhysicalAddress]
    		$NewMAC,
    		
    		[Parameter(ParameterSetName = "New", Position = 2)]
    		[int]
    		$NewAdapterID
    	)
    	
    	Begin
    	{
    		Write-Debug "[Start] [Performing ARP Actions]"
    		
    		# Get active ParameterSet
    		$ParSet = $PSCmdlet.ParameterSetName
    		Write-Debug "Active Parameterset: $ParSet"
    	}
    	Process
    	{
    		switch ($ParSet)
    		{
    			"Get" { [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Where-Object { $_.Address.ToString() -like $Filter } }
    			"Delete"
    			{
    				foreach ($Item in $Delete)
    				{
    					$Type = $Item.GetType().FullName
    					switch ($Type)
    					{
    						"System.String" { [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Where-Object { $_.Address.ToString() -like $Item } | %{ $_.Delete() } }
    						"Netzwerker.Network.Diagnostics.ArpEntry" { $Item.Delete() }
    					}
    					
    				}
    			}
    			"Clear"
    			{
    				$Adapters = [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Select-Object -ExpandProperty IndexAdapter -Unique
    				$Adapters | %{ [Netzwerker.Network.Diagnostics.ArpHost]::Clear($_) }
    			}
    			"New"
    			{
    				if (!$PSBoundParameters["NewAdapterID"]) { $NewAdapterID = [Netzwerker.Network.Diagnostics.ArpHost]::Get() | Group-Object IndexAdapter -NoElement | Sort-Object Count | Select-Object -Last 1 -ExpandProperty Name }
    				[Netzwerker.Network.Diagnostics.ArpHost]::New($NewIP, $NewMAC, $NewAdapterID)
    			}
    		}
    	}
    	End
    	{
    		# Write closing line
    		Write-Debug "[End] [Performing ARP Actions]"
    	}
    }
    New-Alias -Name "arp" -Value "Invoke-AddressResolutionProtocol" -Force -Option 'AllScope'

    Cheers,
    Fred

    Edit: I just went ahead, packaged the whole thing into a single script file and published it in the gallery.


    There's no place like 127.0.0.1



    • Edited by FWN Friday, March 13, 2015 11:29 AM
    • Marked as answer by Henry_8198 Monday, March 16, 2015 2:38 PM
    Thursday, March 12, 2015 1:38 PM
  • Thank you! It works and it will be very useful in the gallery!

    Henry

    Monday, March 16, 2015 2:39 PM