How to find which IP address is active for communication? - powershell

I am using Windows XP Sp3. We are having both wired network and wireless network.
Both will have different IP address range .
Is there any way to find which IP address/Gateway is used to communicate?
i.e. We are having permission to wired network IP range to rdp to our lab machines. But with wireless connection we can't.
Often people forget that and asking me.
If the wireless network is active channel , then it should either try to connect through wired network or it should intimate them. How to achieve this using powershell?

You can get the same information route print shows like this:
Get-WmiObject -Class Win32_IP4RouteTable | select Destination, Mask, NextHop, Metric1
If your network address is 10.0.0.0 you can get the NextHop IP like this:
Get-WmiObject -Class Win32_IP4RouteTable | ? {$_.Destination -eq "10.0.0.0"} | select NextHop

As I understand you should manually add routes to lab machines for wired interface. And you could detemine current route settings with command:
route print
running from windows console (cmd )
Guess this links could be also helpfull to you:
Configure default gateway
Usage route command
Adding static IP route

Related

Powershell: Determine the current Physical network adapter in use allowing internet connections

I am trying to determine the physical network adapter in use through powershell.
I have some of the pieces for a function I am creating but have issues to determine the actual physical adapter in use:
With Get-NetAdapter -Physical | Select-Object Name, InterfaceDescription, ifIndex, Status I can get the physical network adapters, and if they are up:
Here I get also the interface index (relevant later):
Name
InterfaceDescription
IfIndex
Status
Ethernet
Intel(R) Ethernet Connection (4) I219-V
23
Up
Wi-Fi
Intel(R) Dual Band Wireless-AC 8265
8
Up
With Get-NetRoute -InterfaceIndex <interface index> | Where-Object {$_.AddressFamily -eq 'IPV4'} I can get the routes for all interfaces (2 in this case)
The problem is that both interfaces have a 0.0.0.0/0 route for their respective gateway, so any of them can route me to the "regular" internet (let's say, google.com).
So how do I determine the actual interface in use?. I think that the interface metric in both cases can help me:
ifIndex
DestinationPrefix
NextHop
RouteMetric
ifMetric
8
0.0.0.0/0
192.168.3.1
256
40
23
0.0.0.0/0
192.168.1.1
256
25
So the Ethernet interface (IfIndex = 23) has an IfMetric of 25. As RouteMetric is the same for both, Ethernet Interface will take precedence, in theory.
This shall apply to connections with or without VPN clients (indeed for these tests I am connected through a VPN client) but:
a. I am not 100% sure that this may work every time.
b. What if the Route Metric is different for the interfaces?. What takes precedence: the ifMetric, the RouteMetric or a combination of both?.
Will be glad to read your comments or experiences about this.
Thanks!
It is a combination of the metrics from my experience, however the way I usually get a definitive answer is to run the Test-NetConnection command to a URL or IP and the output of that command will list the interface it used. I use it to ensure traffic goes over our VPN adapter but in your case if you did a Test-NetConnection -ComputerName www.google.com it will return the interface alias of the interface used which you can use to find the ifindex and map it back to the adapter if you have multiple adapters

Powershell script - Change IP using the InterfaceDescription

I am trying to create a powershell script that changes a single port on a computer.
I have 10 machines, each have 4 ports with one port out the four having a specific Interface Description "Intel(R) 82579LM Gigabit Network Connection". This is constant throughout all the machines. The other 3 ports are just “Intel(R) 82574L Gigabit Network Connection” ports.
I only care for the 82579LM port. I want a script that will set a static IP to this one interface that I can run on each machine after a reinstall. I already have a script that is able to use the hostname to set the IP but I cannot figure a way to change the IP using the Interface Description from get-netadapter to select the adapter.
I can't get my head around this, any input would be amazing!
Quick/easy method would be to hold the object from your Get-NetAdapter -InterfaceDescription results in a variable
PS> $INTEL82579LM=Get-NetAdapter -InterfaceDescription "Intel(R) 82579LM Gigabit Network Connection"
You can access individual attributes of the object as $variable.attribute -- so the interface ID is $INTEL82579LM.ifIndex
PS> echo $INTEL82579LM.ifIndex
19
The object held in the variable is used to insert the interface index number on subsequent commands:
PS>Set-NetIPAddress -InterfaceIndex $INTEL82579LM.ifIndex -IPAddress 10.1.2.3 -PrefixLength 24
Set-NetIPAddress is for updating existing IP address config. New-NetIPAddress creates a new one.

Routing and VPN

I have created a powershell script to create a VPN, connect to add, add the routes for it.
The only issue I am having is that my gateway for that VPN is not known upfront, so users need to add it with a parameter.
This isn't so clean, so I would like to retrieve the gateway from the VPN connection in PowerShell. Is there any way to this?
I am connecting to my VPN like this "rasphone.exe -d $name".
How can I obtain the gateway for this VPN so I can route certain IP's through the VPN?
I have found a working approach:
First I need to get the IP configuration for the VPN connection
$ipConfiguration = Get-NetIPConfiguration | where { $_.InterfaceAlias -eq $name } | Select-Object -First 1
Than I can add routes for this VPN like this:
ROUTE.EXE ADD $ip MASK $mask $ipConfiguration.IPv4Address.IPAddress if $ipConfiguration.interFaceIndex;
The IP address is not the actual gateway, but in the docs I have read that the closed gateway is taken. This works perfectly!

Is there a way to change TCP/IP setting of IP v4 via Powershell/batch?

In my home network , I have to set IP address and DNS server. In Office network that settings to be removed .
Daily i am doing this activity manually. Though powershell automate so many things ,I am not finding any cmdlet for this in Windows 7.
I am doing the procedure manually . Is there any way to solve this using powershell? Even if batch file is there for it , I can use it
Here is a script that I use for that.
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
if($wmi.count -eq 1)
{
$wmi.EnableStatic("10.0.0.2", "255.255.255.0")
$wmi.SetGateways("10.0.0.1", 1)
$wmi.SetDNSServerSearchOrder(#("10.0.0.3","10.0.0.4"))
}
An an even better option for most situation (especially home networks) is to use DHCP. If you have a wireless router, It should have that option.
Isn't PowerShell a bit of overkill?
You can use simple NETSH command from command line.
To check config:
netsh interface ip show config
To turn on DHCP:
netsh interface ip set address name="Local Area Connection" source=dhcp
To setup static address:
netsh interface ip set address name="Local Area Connection" source=static addr=192.168.0.100 mask=255.255.255.0 gateway=192.168.0.1

is there a way I can get the IP Address when I connect to a VPN server

I am currently using Powershell to get me the list of all the IP Address of my machine. When I am not using my vpn connection ( when I am at work) I get the IP Address of my machine correct. but When I am at home and I am using my VPN connection, I get another IP Address which is being provided by the client. I would like to get the second IP Address provided to me by the VPN client.
I am currently using this query
(gwmi -query "Select IPAddress From Win32_NetworkAdapterConfiguration = True").IpAddress
However like I have said before this query does not give me the IP address provided by the VPN client .
Please suggest..
Thanks and Regards
Nav
try this:
[System.Net.Dns]::GetHostEntry($a).addresslist
Return al IP4 and IP6.
Cisco VPN not always show to client the IP of vpn connection.