Get the DHCP option set on your NIC

Sometimes I’d like to get rid of the brave old ipconfig.exe but there are still some of its features that are not implemented in PowerShell cmdlets by default.

You can for example define a DHCPv4 option on a network interface with ipconfig like this:

ipconfig --% /setclassid "Local Area Connection" test-dhcp

As far as I know, there isn’t any PowerShell cmdlet that allows natively to retrieve these dhcp options set on a Nic.


Ok, I’ll do it 😀

A Procmon trace reveals that ipconfig.exe just reads the registry to retrieve the value of these dhcp options.

Here’s what I propose to “extend” on the fly the Get-NetAdapter, Get-NetIPConfiguration or Get-NetIPInterface cmdlets.

# Get-NetIPConfiguration
Get-NetIPConfiguration -All | ForEach-Object -Process { 
    $_ | Add-Member -MemberType ScriptProperty -Name "DHCPv4 Class ID" -Value {
        # Ipv4
        try {
            (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$($_.NetAdapter.DeviceID)" -Name DhcpClassId -ErrorAction Stop).DhcpClassId
        } catch {
            [string]::Empty
        }
    } -PassThru -Force | 
    Add-Member -MemberType ScriptProperty -Name "DHCPv6 Class ID" -Value {
        # Ipv6
        try {
            (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP6\Parameters\Interfaces\$($_.NetAdapter.DeviceID)" -Name Dhcpv6ClassId -ErrorAction Stop).Dhcpv6ClassId
        } catch {
            [string]::Empty
        }
    } -PassThru -Force
} | fl Interface*,DHCPv*

# Get-NetIPInterface
Get-NetIPInterface | ForEach-Object -Process {
    $_ | Add-Member -MemberType ScriptProperty -Name DhcpClassId -Value {
        switch ($_.AddressFamily) {
            "IPv4" {
                try {
                (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$((Get-NetAdapter -InterfaceIndex $this.InterfaceIndex).InterfaceGUID)" -Name DhcpClassId -ErrorAction Stop).DhcpClassId
                } catch {
                    [string]::Empty
                }
                break
            }
            "IPv6" {
                try {
                (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$((Get-NetAdapter -InterfaceIndex $this.InterfaceIndex).InterfaceGUID)" -Name Dhcpv6ClassId -ErrorAction Stop).Dhcpv6ClassId
                } catch {
                    [string]::Empty
                }
                break
            }
            default {}
        }
    } -Force -PassThru
} | ft ifIndex,InterfaceAlias,AddressFamily,DhcpClassId

# Get-NetAdapter
Get-NetAdapter | ForEach-Object -Process {
    $_ | Add-Member -MemberType ScriptProperty -Name "DHCPv4 Class ID" -Value {
        try {
            (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$($this.InterfaceGUID)" -Name DHCPClassId -ErrorAction Stop).DhcpClassId
        } catch {
            [string]::Empty
        }
    } -Force -PassThru  | 
    Add-Member -MemberType ScriptProperty -Name "DHCPv6 Class ID" -Value {
        try {
            (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP6\Parameters\Interfaces\$($this.InterfaceGUID)" -Name Dhcpv6ClassId -ErrorAction Stop).Dhcpv6ClassId
        } catch {
            [string]::Empty
        }
    } -PassThru -Force
} | ft Name,InterfaceDescription,IfIndex,Status,MacAddress,LinkSpeed,DHCPv*Class*

Advertisements

2 thoughts on “Get the DHCP option set on your NIC

    • Well, that was not my intention. PowerShell is for sure the future but not 100% is currently achievable with PowerShell.
      Ipconfig.exe does other things. For example, it can display and purge the local DNS cache of your computer.
      As of Windows 8, you have Clear-DNSClientCache and Get-DNSClientCache cmdlets to cover these features of the brave old ipconfig.exe 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s