Blog

QuickPost: Logging XenApp server session durations

By James Rankin | 21st October 2015

I’m knee-deep in a revisit of the Internet Explorer 10+ Cookies and History debacle (and I’m already wishing I hadn’t!), so it’s an apt time for a QuickPost just to stop me from going completely and utterly insane. I had a request recently to log the durations of XenApp server sessions during a PoC. Now, this is something that I’d normally do by using a report from EdgeSight/Director or other monitoring tools such as Lakeside SysTrack or ControlUp, but sometimes this can be fiddly or time-consuming to set up, or you may not even actually have the tools required. The requirement was fairly simple – create a comma-delimited log file that simply recorded the userid, date and duration of the session which would then be loaded and manipulated in Excel – so I set off to quickly knock one up. As a requirement, it’s not that strange – I’ve worked in a few places where staff and management wanted to know how much usage PoCs and pilots were actually getting, so it is fairly relevant.

Breaking the task down, what do we need to do?

1. Record the actual date and time of logon, and store this somewhere

2. Record the date and time of logoff

3. Compare the stored logon time to the logoff time and create a Session Duration time

4. Write the user info, the date, and the Session Duration to a file in a network share for perusal

OK. Tools-wise, we’ve gone for PowerShell (great thanks to Duncan Murdoch for helping tidy this up from my rough-and-ready batch/PS combo I originally went with). I used AppSense DesktopNow to do the tooling – however, if you simply want to run the scripts as Group Policy Logon and Logoff Scripts they should work just as well without any alterations. However, if you do it through GPO, make sure the PowerShell execution policy is set correctly to allow your scripts to run.

Firstly, when the user logs on, we need to simply extract the date and time of their logon and store it somewhere. We’ve elected to write it to HKCU in a key called ~Custom. We also need to check that this key exists before writing it.

$date = Get-Date

$regpath = “HKCU:\Software\~Custom”
$name = “LogonTime”
$value = $date

if (!(Test-Path -Path $regpath))
{
    New-Item -Path $regpath -Type String
    New-ItemProperty -Path $regpath -Name $name -Value $value
}
else
{
    Set-ItemProperty -Path $regpath -Name $name -Value $value
}

That’s all we need to do for the logon section.

When the user comes to log out of their XenApp session, we have a bit more work to do.

First of all we need to repeat the gathering of the current date and time once again and store it in a variable

$LogoffTime = (Get-Date)

Then, we need to set up another variable which simply stores the date, as Get-Date gives us both date and time

$Date = (Get-Date -format dd-MM-yyyy“)

Next, we have to retrieve the LogonTime information which we stored in the Registry earlier

$regPath = HKCU:\Software\~Custom
$regvalue = “LogonTime”

$LogonTime = Get-ItemProperty -Path $regPath -Name $regvalue

And next we need to compare $LogonTime with $LogoffTime to generate a $SessionDuration variable, and then cut that down to a single line for perusal. Luckily, PowerShell has the New-TimeSpan cmdlet which can do this for us (isn’t PowerShell great?)

$SessionDuration = New-TimeSpan -Start $LogonTime.LogonTime -End $LogoffTime

$data = “{0:c}” -f $SessionDuration

Finally, we have to take all of the data we’ve gathered into these variables and spit it out using Out-File to the csv file which we’ve got on the network. This is where my PowerShell probably looks clunky and rudimentary (lines will wrap), but hey, it works 🙂

$data1 = $data.Split(“:”); $hours = $($data1[0]); $minutes = $($data1[1]); $seconds = $($data1[2])
$env:USERDOMAIN\$env:USERNAME,$date,$hours hours $minutes minutes $seconds seconds | Out-File \\UKSLDC001\FileStore\Logs\XenAppServerLogs.csv -Append

Needless to say, your users will need Write access to the share and filesystem where the log file resides.

Also, it would be prudent to remove the LogonTime entry from the Registry that we wrote at login, as next time the user logs in it will need to be recreated. Again, use tools like AppSense or a scripted command to achieve this part

Naturally, now it is time to test! Let’s log a user on to our XenApp PoC server published desktop, use a few applications, and then log back out. Note that this should work equally well for published applications and not just desktops or VDI sessions. Let’s check our logs…

…and as you can see, we have a nicely-delimited format to pull out the details for the user, the date, and the logon duration.

Big credit due to Duncan Murdoch for helping me tidy up my dirty scripting (and a big middle finger to Graeme Cowie who said even Rupert Murdoch couldn’t sort out my scripts ;-)). Also Carl Webster for, as usual, pointing me in the right direction. Obviously, I only tested this on English UK date formats, so please test it thoroughly before using it yourself. But hopefully this will help any of you out there wanting some quick and dirty logon duration stats pulled out from your systems during PoC phases.

Back to Blogs

Get in touch

Please call us on 0191 481 3446 or email info@htguk.com and we’ll get back to you.

Chat to our team Messenger