Search code examples
powershellnetwork-programmingscriptingpowershell-3.0dhcp

How to Verify Reserved IP with Powershell


I am working on a little powershell 3.0 GWMI script that pulls computer information for use in an enterprise environment (ip, mac address, etc).

I'm trying to go through the WMI properties for NetworkAdapterConfiguration to see if there is a way to check for a reserved IP vs. a dynamically assigned one.

Would anyone have advice on how to pull this from WMI or elsewhere? Does (preferred) always indicate that an IP is reserved on the network?

I'm finding a lot of information for powershell and Azure but not a ton for figuring this out on a local box.


Solution

  • As Ron Maupin noted, Host computers will only know whether they were assigned an addressed from the DHCP, not if there was a reservation. But they will report which DHCP server they received their address from. So you can query that server (assuming you have read permissions).

    Here is a script that after retrieving the information from a computer over WMI will check with the DHCP server if a reservation exists.

    $ComputerName = "ExampleComputer"
    $NetAdapters = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName | ? {$_.DHCPEnabled -eq $True -and $null -ne $_.IPAddress}
    If ($NetAdapters) {
        Foreach ($Adapter in $NetAdapters) {
            foreach ($IP in $Adapter.IPAddress) {
                $Reservation = Get-DhcpServerv4Reservation -ScopeId $IP -ComputerName $Adapter.DHCPServer | ? {$_.ScopeId -eq $_.IPAddress}
                If ($Reservation) {
                    Write-Output "$IP is reserved on $($Adapter.DHCPServer)."
                } Else {
                    Write-Output "$IP does not have a reservation."
                }
            }
        }
    } Else {
        Write-Output "No DHCP Enabled NetAdapters with IPAddresses exist on host, likely Static"
    }