• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1411
  • Last Modified:

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

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
mikeydk
Asked:
mikeydk
1 Solution
 
SilverwolfCommented:
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
 
Spike99On-Site IT TechnicianCommented:
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
 
SilverwolfCommented:
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
Dan CraciunIT ConsultantCommented:
netstat should give you all established connections. The IPs connected on port 3389 should be your remote desktop clients.

HTH,
Dan
0
 
Spike99On-Site IT TechnicianCommented:
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
 
oBdACommented:
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
 
Senior IT System EngineerIT ProfessionalCommented:
Thanks oBdA,

The solution works :-)
0

Featured Post

Problems using Powershell and Active Directory?

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now