Powershell Snippet – Retrieve Citrix Endpoint Name from WFAPI

[code language=”powershell”]
$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()
[/code]

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 comment

Your email address will not be published. Required fields are marked *

WordPress Appliance - Powered by TurnKey Linux