Public/Remove-specArpEntry.ps1

function Remove-specArpEntry {
    <#
    .SYNOPSIS
        Removes an ARP entry for a specified IP address, either from a specific interface or across all interfaces.
 
    .DESCRIPTION
        This function removes the ARP entry for a given IP address. If an InterfaceIndex is provided, it will attempt to remove the entry for that specific interface. If no InterfaceIndex is supplied, the function will remove the ARP entry for the IP address across all network interfaces.
 
    .PARAMETER IpAddress
        The IP address for which the ARP entry should be removed. This is a mandatory parameter and supports pipeline input.
 
    .PARAMETER InterfaceIndex
        The network interface index from which to remove the ARP entry. This is an optional parameter. If not specified, the ARP entry for the IP address will be removed across all interfaces.
 
    .EXAMPLE
        Remove-specArpEntry -IpAddress '192.168.1.112' -InterfaceIndex 14
        This example removes the ARP entry for IP address 192.168.1.112 on Interface Index 14.
 
    .EXAMPLE
        '192.168.1.112' | Remove-specArpEntry
        This example removes the ARP entry for IP address 192.168.1.112 across all interfaces, using pipeline input.
 
    .EXAMPLE
        $arpEntries = @(
            [PSCustomObject]@{ IpAddress = '192.168.1.112'; InterfaceIndex = 14 }
        )
        $arpEntries | Remove-specArpEntry
        This example demonstrates using pipeline input to remove an ARP entry for a specific IP address and Interface Index.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - Initial release
    #>


    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidatePattern('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')]
        [string]$IpAddress,

        [Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [int]$InterfaceIndex
    )

    process {
        try {
            if ($InterfaceIndex) {
                # Check if the interface exists before attempting removal
                $interfaceExists = Get-NetAdapter -InterfaceIndex $InterfaceIndex -ErrorAction stop
                if (-not $interfaceExists) {
                    Write-Error "InterfaceIndex $InterfaceIndex does not exist or is invalid."
                }

                # If InterfaceIndex is valid, proceed with removal for that specific interface
                if ($PSCmdlet.ShouldProcess("ARP entry for IP $IpAddress on Interface Index $InterfaceIndex", 'Remove')) {
                    try {
                        $removeResult = Remove-NetNeighbor -IPAddress $IpAddress -InterfaceIndex $InterfaceIndex -Confirm:$false -PassThru
                        if ($removeResult) {
                            Write-Verbose "Successfully removed ARP entry for IP $IpAddress on Interface Index $InterfaceIndex."
                        } else {
                            Write-Verbose "No ARP entry found for IP $IpAddress on Interface Index $InterfaceIndex."
                        }
                    } catch {
                        # Catch the error when no ARP entry is found and write the verbose message
                        Write-Verbose "No ARP entry found for IP $IpAddress on Interface Index $InterfaceIndex."
                    }
                }
            } else {
                # If InterfaceIndex is not supplied, remove ARP entry for the IP address across all interfaces
                if ($PSCmdlet.ShouldProcess("ARP entry for IP $IpAddress", 'Remove')) {
                    try {
                        $removeResult = Remove-NetNeighbor -IPAddress $IpAddress -Confirm:$false -ea Stop -PassThru
                        if ($null -eq $removeResult -or $removeResult.Count -eq 0) {
                            Write-Verbose "No ARP entry found for IP $IpAddress."
                        } else {
                            Write-Verbose "Successfully removed ARP entry for IP $IpAddress."
                        }
                    } catch {
                        # Catch the error when no ARP entry is found and write the verbose message
                        Write-Verbose "No ARP entry found for IP $IpAddress."
                    }
                }
            }
        } catch {
            Write-Warning "Error: $_"
            Write-Error "Failed to remove ARP entry: $_"
        }
    }
}