Solved

How can I get the IP address of a remote desktop client?

Posted on 2016-08-23
7
99 Views
Last Modified: 2016-08-29
Hey

How can I get the IP address of a remote desktop client? (Powershell - running in users context - non-admin)

Thanx in advance

Mike
0
Comment
Question by:mikeydk
7 Comments
 
LVL 1

Expert Comment

by:Silverwolf
ID: 41767056
You can use the following commands. The latter can only be done in PowerShell.

Command Prompt or Powershell: ping -a IPADDRESS

PowerShell: [System.Net.Dns]::GetHostbyAddress("IPAddress")
0
 
LVL 16

Expert Comment

by:Spike99
ID: 41767097
I think the author is asking how to get the IP of the remote PC from the remote desktop session on the server. Those 2 commands require the IP of the machine in order to run.

PING -A IPADDRESS, for example,  is a reverse lookup.  It will give you the name of the system if you know the IP, but he doesn't know the ip.
0
 
LVL 1

Expert Comment

by:Silverwolf
ID: 41767102
Oops, your right. My bad. Multi-tasking too much. lol

In that case just run the ping command with the hostname (Computer name):
ping hostname.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 41767104
netstat should give you all established connections. The IPs connected on port 3389 should be your remote desktop clients.

HTH,
Dan
0
 
LVL 16

Accepted Solution

by:
Spike99 earned 500 total points
ID: 41767208
Try running this at the command prompt.  It should bring up the client name & IP:

ping %clientname%

Open in new window


I ran that in RDP sessions on both Windows 7 and Server 2008 R2: it pulled up the client name & IP for me on both.
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41768568
This'll get you the client IP of the current user's session. Can't test on IPv6, though, so you might have to play with the copy start index in line 54 if the address format isn't correct if you're using IPv6. Should work on XP and PS 2.0 or later.
If (-not ('WTSApi' -as [Type])) {Add-Type -Language CSharp -TypeDefinition @'
using System;
using System.Runtime.InteropServices;

public class WTSApi
{
	[DllImport("wtsapi32.dll")]
	static extern void WTSFreeMemory(IntPtr pMemory);

	[DllImport("Wtsapi32.dll")]
	static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);

	[StructLayout(LayoutKind.Sequential)]
	private struct WTS_CLIENT_ADDRESS
	{
		public int AddressFamily;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
		public byte[] Address;
	};

	private enum WTS_INFO_CLASS
	{
		WTSClientAddress = 14
	}

	private enum ADDRESSFAMILY
	{
		AF_UNSPEC = 0,
		AF_INET = 2,
		AF_IPX = 6,
		AF_NETBIOS = 17,
		AF_INET6 = 23
	}
	
	public static string GetSessionIP()
	{
		Int32 WTS_CURRENT_SESSION = -1;
		IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
		IntPtr WTSClientAddressPtr = IntPtr.Zero;
		string ClientAddress = "";
		uint bytes = 0;
		WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTS_INFO_CLASS.WTSClientAddress, out WTSClientAddressPtr, out bytes);
		WTS_CLIENT_ADDRESS WTSClientAddress = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(WTSClientAddressPtr, typeof(WTS_CLIENT_ADDRESS));
		WTSFreeMemory(WTSClientAddressPtr);
		switch((ADDRESSFAMILY)WTSClientAddress.AddressFamily)
		{
			case ADDRESSFAMILY.AF_UNSPEC:
				break;
			case ADDRESSFAMILY.AF_INET:
				ClientAddress = WTSClientAddress.Address[2] + "." + WTSClientAddress.Address[3] + "." + WTSClientAddress.Address[4] + "." + WTSClientAddress.Address[5];
				break;
			case ADDRESSFAMILY.AF_INET6:
				byte[] IPv6 = new byte[16];
				Array.Copy(WTSClientAddress.Address, 0, IPv6, 0, 16);	// ATTENTION: Microsoft does not document WHERE the 16 bytes of the IPv6 address are located in the 20 byte array (IPv4 starts at byte 2, not 0)!
				ClientAddress = BitConverter.ToString(IPv6);
				break;
			default:
				ClientAddress = BitConverter.ToString(WTSClientAddress.Address);
				break;
		}
		return ClientAddress;
	}
}
'@
}
[WTSApi]::GetSessionIP()

Open in new window

1
 
LVL 7

Expert Comment

by:Senior IT System Engineer
ID: 41775735
Thanks oBdA,

The solution works :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now