Solved

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

Posted on 2016-08-23
7
344 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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
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 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

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

Suggested Solutions

Title # Comments Views Activity
Adding a countdown to HTA 12 98
export dirsync errors detail to a CSV file. 2 33
Can powershell display multiple job outputs in one window. 3 19
Powershell Code 4 31
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

733 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