Static IP from Ethe...
 
Notifications
Clear all

[Solved] Static IP from Ethernet adapter IPv4 properties

9 Posts
2 Users
1 Reactions
243 Views
(@no_on)
Active Member Registered
Joined: 4 weeks ago
Posts: 4
Topic starter  

Is it possible to read the IP address that is entered on the ethernet adapter properties using WMI?

I would like to have it display the IP address even when the ethernet cable is not plugged in.

I have tried using "NETWORKINTERFACES=interval:10, align:1, style:b, color:%ComColor%, font-size:80%, iftype:6 71, operstatus:2, row-text: , display: %2 - %8" but if the computer gets rebooted the IP address changes to the 169.256... address.

This is a 2nd ethernet port on my computer that I use to connect to devices and instead of having to open properties to check what IP address is entered I would like to just show it on the desktop.

I did find a workaround using the following powershell script:

$adapter = "Ethernet 2"
$ip = (Get-NetIPAddress -InterfaceAlias $adapter | Where-Object { $_.PrefixLength -eq 24 }).IPAddress

if ($ip) {
# Add equal sign before the IP address
("=" + $ip) | Out-File "D:\DesktopInfo\static-ip.txt" -Encoding utf8
} else {
# If no static IP, output "No Static IP" with equal sign
"= No Static IP" | Out-File "D:\DesktopInfo\static-ip.txt" -Encoding utf8

And then using the following to display on the desktop:

CMD=file:cmd.exe, parameters:/c "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -File "D:\DesktopInfo\get-static-ip.ps1",interval:10, hidden:1
FILE2TEXT=file:\static-ip.txt,text:,interval:10,color:%ComColor%,style:b,font-size:80%,align:1,display:Ethernet 2 IP: %2

But I am wondering if there is any easier way of doing this.

This topic was modified 2 weeks ago by Glenn

   
Quote
Glenn
(@glenn)
Member Admin
Joined: 6 years ago
Posts: 1369
 

If you set a static IP in the adapter settings, does windows revert it after a reboot?


   
ReplyQuote
(@no_on)
Active Member Registered
Joined: 4 weeks ago
Posts: 4
Topic starter  

No, the static IP is still in the adapter settings after the reboot.

If you do a "ipconfig -all" in the command prompt it shows Media Disconnected and it doesn't show an IP.

In powershell if I do " Get-NetIPAddress -InterfaceAlias "Ethernet 2" ", it will show me the IP address that is in adapter settings but it also shows the 169.254... IP address that Desktop info displays after the reboot.


   
ReplyQuote
Glenn
(@glenn)
Member Admin
Joined: 6 years ago
Posts: 1369
 

so it feels like DTI might be showing the default 169 ip when an interface is disconnected.  instead, if an interface is disconnected it should look to see if there is a static ip configured and show that instead ???


   
ReplyQuote
(@no_on)
Active Member Registered
Joined: 4 weeks ago
Posts: 4
Topic starter  

Yes, knowing what static ip address was lasted entered and knowing if I have to change it to be able to talk to a device would help.


   
ReplyQuote
Glenn
(@glenn)
Member Admin
Joined: 6 years ago
Posts: 1369
 

I haven't found a WMI object that contains what we need.

I set up a usb ethernet adapter on my machine so i have a connected ethernet and an unconnected ethernet which is named "Ethernet 2". i ran the following in powershell ise:

$adapter = "Ethernet 2"
Get-NetIPAddress -InterfaceAlias $adapter

it returned three results for the one interface

IPAddress         : fe80::374:73c4:3f88:33fe%31
InterfaceIndex    : 31
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv6
Type              : Unicast
PrefixLength      : 64
PrefixOrigin      : WellKnown
SuffixOrigin      : Link
AddressState      : Deprecated
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 169.254.226.174
InterfaceIndex    : 31
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 16
PrefixOrigin      : WellKnown
SuffixOrigin      : Link
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 10.0.1.222
InterfaceIndex    : 31
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

I see each has a different PrefixLength but I would suggest that's not a good property to filter on. According to https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.prefixorigin?view=net-9.0 , the PrefixOrigin property will tell you if the IP is manually entered, ie a static IP.

So the core powershell command would be

Get-NetIPAddress -InterfaceAlias "Ethernet 2" | Where-Object { $_.PrefixOrigin -eq "Manual" }

 


   
ReplyQuote
Glenn
(@glenn)
Member Admin
Joined: 6 years ago
Posts: 1369
 

You can skip the intermediate text file by putting the powershell script in your ini file.  You'll see some examples in the advanced ini in the DTI zip file.

 

add a new section at the very end of the Desktop Info ini to contain the powershell script:

[get-static-ip.ps1]
$adapter = 'Ethernet 2' 
$ip = (Get-NetIPAddress -InterfaceAlias $adapter | Where-Object { $_.PrefixOrigin -eq 'Manual' }).IPAddress

if (!$ip) {
    # If no static IP, output "No Static IP"
    $ip = 'No Static IP'
    }

Write-Host $ip

Note the use of single quotes.

 

Now add 2 items to the ini where you want the entry to display:

# load the powershell code into a variable
SET-SECTION=key:staticip,value:get-static-ip.ps1

# insert the variable text into the CMD parameters
CMD=interval:3600, text:Ethernet 2 IP, trim:1, file:powershell.exe, parameters:%staticip%, code-page:437, no-wait:1

 

 

 

This post was modified 2 weeks ago 2 times by Glenn

   
ReplyQuote
Glenn
(@glenn)
Member Admin
Joined: 6 years ago
Posts: 1369
 

While I was at it, I came up with a more general procedure that will display all network interfaces with a static IP.

[get-static-ip.ps1]
Get-NetIPAddress | Where-Object { $_.PrefixOrigin -eq 'Manual' } | Select-Object InterfaceAlias, IPAddress | ConvertTo-Csv -NoTypeInformation

 

[items]
comment=display:Static IPs, style:b

# load the powershell code into a variable
SET-SECTION=key:staticip,value:get-static-ip.ps1

# insert the variable text into the CMD parameters
CMD=interval:3600, trim:1, file:powershell.exe, parameters:%staticip%, code-page:437, no-wait:1, read-as-csv:1, csv-header:1,\
  row-text:%InterfaceAlias%, display:%IPAddress%

   
ReplyQuote
(@no_on)
Active Member Registered
Joined: 4 weeks ago
Posts: 4
Topic starter  

Yes, this works perfectly. Thanks.


   
Glenn reacted
ReplyQuote
Glenn's Page