Search code examples
powershellip-addressmac-addressdhcp

Retrieve a client's IP address with its MAC address via the DHCP server in a PowerShell script


My question is this: I have, thanks to a PowerShell script retrieved the MAC address of my thin client. The script is executed on a client computer, and I want to contact, via my script, the DHCP server to recover the IP address assigned to my thin client. I am open to other solutions than contacting the DHCP server if you know others, but I need the current address and no possibility of failure and I would like to output the IP in a variable to display it.

I tried this but I think it only works from the DHCP server, but it's impossible in my case. https://thewayeye.net/2016/10/8/find-a-mac-microsoft-dhcp/

I have this right now:

$mac

With this output:

847BEBEE****

I therefore need the IP address of the thin client from its MAC address from the DHCP server preferably to avoid errors due to the ARP table which does not always work due to the very large number of workstations in my network (large enterprise).


Solution

  • I finally found a solution to simply retrieve the IP address via DHCP:

    Get-DhcpServerv4Scope -ComputerName $dhcpserver | Get-DhcpServerv4Lease -ComputerName $dhcpserver | where{$_.clientid -eq $macaddress}
    

    The mac address format must be xx-xx-xx-xx-xx-xx (with dashes instead of colon)

    However, you must install the RSAT microsoft administration tools and have the necessary rights on your account to query the DHCP server.

    For information, to run powershell under another user pin it to the taskbar, "SHIFT+Right Clic" "Run with another user"