Getting a user’s displayname without additional AD query

At a customer we have some applications that rely on specific registry keys to be set, to get this info just in time we’re using Appsense Environment Manager with a Process started trigger fot the specific application. The application needs to display the users displayname. Currently we are using a VBS script for it that queries the Active Directory to get the users displayname. Because of latency problems this script sometimes takes way too long so we were searching other ways to do this, should be possible since the start menu is showing the displayname as well.

And we can!

We get this data through the GetUserNameEx function. Below is the ‘Powershell’ code to return this info. Ofcourse this would need some additional work 🙂

$source = @'
[DllImport("secur32.dll", CharSet = CharSet.Auto)]
public static extern int GetUserNameEx(uint nameFormat, 
System.Text.StringBuilder userName,
ref uint userNameSize);

Add-Type -MemberDefinition $source -Namespace Secur32 -Name name

$nameformat = 3
$size = 64
$str = New-Object System.Text.StringBuilder -ArgumentList $size

[]::GetUserNameEx($nameformat, $str, [ref]$size) | Out-Null