Public/New-specArpEntry.ps1

function New-specArpEntry {
    <#
    .SYNOPSIS
        Creates a new ARP entry for a specified IP address, MAC address, and interface index.
 
    .DESCRIPTION
        This function sets a new ARP entry for a specified IP address and MAC address on a given network interface. The ARP entry is added as a permanent entry, and the function supports pipeline input for the MAC address, IP address, and interface index.
 
    .PARAMETER MacAddress
        The MAC address to associate with the specified IP address. This is a mandatory parameter and supports pipeline input. The MAC address should be in the format "XX-XX-XX-XX-XX-XX" or "XX:XX:XX:XX:XX:XX", where X is a hexadecimal digit.
 
    .PARAMETER IpAddress
        The IP address for which the ARP entry should be created. This is a mandatory parameter and supports pipeline input.
 
    .PARAMETER InterfaceIndex
        The network interface index on which the ARP entry should be set. This is a mandatory parameter and supports pipeline input.
 
    .EXAMPLE
        New-specArpEntry -MacAddress '00:1A:2B:3C:4D:5E' -IpAddress '192.168.1.112' -InterfaceIndex 14
        This example creates a new ARP entry for IP address 192.168.1.112, associated with the MAC address 00:1A:2B:3C:4D:5E, on Interface Index 14.
 
    .EXAMPLE
        '00:1A:2B:3C:4D:5E' | '192.168.1.112' | Remove-specArpEntry
        This example demonstrates using pipeline input to set an ARP entry for a specific IP address and MAC address.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - Initial release
    #>


    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidatePattern('^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$')]
        [string]$MacAddress,

        [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 = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [int]$InterfaceIndex
    )

    BEGIN {}

    PROCESS {
        if ($PSCmdlet.ShouldProcess("Setting ARP entry for IP $IpAddress, MAC $MacAddress, InterfaceIndex $InterfaceIndex")) {
            try {
                New-NetNeighbor -IPAddress $IpAddress -LinkLayerAddress $MacAddress -InterfaceIndex $InterfaceIndex -State Permanent -Confirm:$false -ea stop
                Write-Host "ARP entry set for IP $IpAddress, MAC $MacAddress, Interface Index $InterfaceIndex."
            } catch {
                Write-Error "Error setting ARP entry: $_"
            }
        }
    }

    END {}
}