3

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.

Kyle M
  • 33
  • 1
  • 8
  • 1
    `(Get-WmiObject Win32_NetworkAdapterConfiguration).DHCPEnabled` – Mathias R. Jessen Jan 24 '17 at 14:06
  • 1
    You would need to check on the DHCP server to see if the address is reserved or dynamic. A host cannot know if a DHCP-assigned address was reserved on the DHCP server, or if the address was simply assigned from the dynamic scope. – Ron Maupin Jan 24 '17 at 14:17

2 Answers2

5

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"
}
BenH
  • 9,766
  • 1
  • 22
  • 35
1

Using the above script provided by BenH I was able to cobble together a script that worked across my fleet of AWS servers from SSM using the AWS-RunPowerShellScript run document which would give me a fail status if any of my instances weren't configured for DHCP. We had a few lingering out there which were using our DHCP Options Set in our VCP and this helped uncover them. Thanks!

$NetAdapters = Get-WmiObject Win32_NetworkAdapterConfiguration | ? {$_.DHCPEnabled -eq $True -and $null -ne $_.IPAddress} | Select Description
If ($NetAdapters) {
    Foreach ($Adapter in $NetAdapters.Description) {
        Write-Output "DHCP is enabled on $Adapter"
        exit 0
    }
} Else {
    Write-Output "DHCP is not enabled on any adapters on host, likely Static"
    exit 1
}