Solved

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

Posted on 2016-08-23
7
277 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 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 17

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 84

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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