I’m working on a script which I’m trying to determine the IP Address based on matching the gateway IP. I want to gather info for an IP Scan I’m planning to use in my script but in cases which the computer has multiple IP addresses I need to capture the correct one. In most cases there is only one gateway so wanting to use this as my measure to get the correct IP.
Any suggestions of code?
Also a side note any suggestions on best way to obtain IP range of a subnet based on IP address using PowerShell?
You are working on … !? … so, what have you tried so far? Please show your code. We might be able to sort it out or to extend/improve the code to fit your requirements.
At the moment have no code…just looking around for examples at the moment. I’m using CIM Instance win32_networkAdapter or networkadapterconfiguration to pull information. Looking for something that is compatible for all versions of PS
Cool. So why don’t you come back when you have something to show? There are a lot of great sources for inspiration for Powershell code. For example the PowershellGallery oder StackOverflow. … and mostly we are way better with correcting or improving code than to write it from scratch for someone else.
Ah … and btw … I wouldn’t bother to write my code to be compatible with ALL version of Powershell. There are actually only two versions left - 5.1 and 7.x.
Might need a tweak if IPv6 is enabled as you’ll get that address too.
I’m not aware of any easy way to calculate the range in PowerShell but the list of subnet masks is finite so I guess you could just create a list, do a look up and work out your start and end address.
This command doesn’t help when network card has two IP addresses assigned. I’m trying to find a way when multiple IPs are assigned that get the same IP that matches the gateway so we know its the primary IP/network.
Get Default Gateway into a variable or Split octets' from Gateway
Find best command to get IP Address that matches first two octets' of gateway. Third octect can be different depending on subnet so left that out.
Once have correct IP/Subnet that is based on the gateway I can then work out on IPScanner script
Otherwise is there a way to define which is the "primary" ip address in powershell. Or strip the ipconfig /all information and use the first IP address which is normally the one needed.
Thanks for the suggestions…It’s really hard to determine what will be the primary IP address so I might make my script prompt the user to enter or select which Network ID based on IP Addresses found.
I’ll convert IPs to Network IDs and then use a choice menu for selection. This then can be used to work out IP range to scan (the main reason for my script)
**Update
I might be able to determine primary IP address when there is two or more IP Address on an adapter.
Using Registry which lists the primary IP always first under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
as well was using Get-CIMInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Where-Object {$.DefaultIPGateway -ne $null} | Select-Object -ExpandProperty IPAddress | where {$ -notmatch ‘:’}