?
Solved

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

Posted on 2016-08-23
7
Medium Priority
?
777 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 17

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 35

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 17

Accepted Solution

by:
Spike99 earned 2000 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 85

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 8

Expert Comment

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

The solution works :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 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