Powershell Snippet – Retrieve Citrix Endpoint Name from WFAPI


$code = @'
using System;
using System.Runtime.InteropServices;
namespace WFAPI
{

public enum WF_INFO_CLASS
{
WFVersion, // OSVERSIONINFO
WFInitialProgram,
WFWorkingDirectory,
WFOEMId,
WFSessionId,
WFUserName,
WFWinStationName,
WFDomainName,
WFConnectState,
WFClientBuildNumber,
WFClientName,
WFClientDirectory,
WFClientProductId,
WFClientHardwareId,
WFClientAddress,
WFClientDisplay,
WFClientCache,
WFClientDrives,
WFICABufferLength,
WFLicenseEnabler,
RESERVED2,
WFApplicationName,
WFVersionEx,
WFClientInfo,
WFUserInfo,
WFAppInfo,
WFClientLatency,
WFSessionTime,
WFLicensingModel
}

public class Program
{
[DllImport("wfapi.dll", CharSet=CharSet.Unicode,SetLastError=true)]
public static extern bool WFQuerySessionInformation(System.IntPtr hServer, int sessionId, WF_INFO_CLASS WFInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);

[DllImport("wfapi.dll", ExactSpelling = true, SetLastError = false)]
public static extern void WFFreeMemory(IntPtr memory);

public const int WF_CURRENT_SESSION = -1;

public static string GetClientName()
{
System.IntPtr buffer = IntPtr.Zero;
uint bytesReturned;
try
{
bool sessionInfo = WFQuerySessionInformation(System.IntPtr.Zero, WF_CURRENT_SESSION, WF_INFO_CLASS.WFClientName, out buffer, out bytesReturned);
return Marshal.PtrToStringUni(buffer);
}
catch
{
return string.Empty;
}
finally
{
WFFreeMemory(buffer);
buffer = IntPtr.Zero;
}
}

}
}

'@

Add-Type -TypeDefinition $code -Language CSharp

[WFAPI.Program]::GetClientName()

Note: To make the code simple/small no error checking is included; Assumption: this code is called inside an active ICA session.

Tested with XenDesktop 7.6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s