• View Communities
    • Citrix Developer Network
      The place for unfiltered straight talk on Citrix products. Blogs, code downloads, best practices, APIs, and more can all be found here.
    • Citrix Ready Community Verified
      Does it work with Citrix? Application compatibility questions are a thing of the past with the new Citrix Community Verified site.
    • Blogs
      Learn the latest from the Citrix employees who are building application delivery infrastructure technologies.
    • Blogosphere
      The Citrix Blogosphere is a window into the thousands of conversations taking place about Citrix and Application Delivery.
  •  Sign In
XenApp Developer Network

Count the users on a Citrix Farm (not Published Apps)

Description

This script counts every ICA connection (every logged-on user) of every server in the Citrix XenApp Farm. The script has to be started on a XenApp Server and gets every server in the Farm.

This gives a view of users on every server, but also it counts the users and shows the total logged-on users on the Farm. The difference with the Access Management Console is, it counts users, not started Published Applications or Desktops. Also it shows the load of the server.

When the script is started, on the end it shows the log. But when started with the option /nolog, it will run and log only.

Code Snippet

CountCitrixUsers.vbs
'******************************************************************
' naam:     CountCitrixUsers.vbs
' function: First gets every server in Citrix XenApp Farm, then count
'           the users (not Publisched Apps)
' option:   /nolog - don't autostart the notepad with output
'           Change the strLogDir for folder for output
' author:   Rink Geervliet
' versie:   1.3
' datum:    23-10-2009
'******************************************************************

Option Explicit
On Error Resume Next

Dim objWshArgs: Set objWshArgs = Wscript.Arguments
Dim strNoLogShow: strNoLogShow = objWshArgs.Named.Exists("nolog")
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strScriptName: strScriptName = objFSO.GetBaseName(WScript.ScriptName)
Dim strLogDir: strLogDir = "C:\Management\Scripts\Log\"

Const CREATE = True
Const DO_NOT_CREATE = False
Const FOR_READING = 1
Const FOR_APPENDING = 8        
Const ERROR = 1
Const WARNING = 2
Const HIDE_WINDOW = 0
Const WAIT_ON_RETURN = True
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim objWshShell: Set objWshShell = CreateObject("Wscript.Shell")
Dim objWshExec
Dim strCMDLine: strCMDLine = objWshShell.ExpandEnvironmentStrings("%COMSPEC%")
Dim strQUser: strQUser = " /C QUSER /server:"
Dim strFilter: strFilter = " | FIND /C "
Dim strUserFilter: strUserFilter = """ica-tcp"""
Dim strQFarm: strQFarm = "QFARM /Load"
Dim strXenAppUsers
Dim strFarmUsers: strFarmUsers = 0
Dim strRunServers: strRunServers = 0
Dim strError: strError = objFSO.GetTempName 
Dim strXenAppFarm: strXenAppFarm = objFSO.GetTempName
Dim strXenUsers: strXenUsers = objFSO.GetTempName
Dim strLogName: strLogName = strLogDir & CustomDateTimeFormat() & "-XenAppUsers.log"
Do while objFSO.FileExists(strLogDir & strLogName)
  strLogName = strLogDir & CustomDateTimeFormat() & "-XenAppUsers.log"
Loop
Dim objLogFile: Set objLogFile = objFSO.OpenTextFile(strLogName,FOR_APPENDING,CREATE)

objWshShell.run "%COMSPEC% /c " & strQFarm & " 2>" & strError & " 1>" & strXenAppFarm, HIDE_WINDOW, WAIT_ON_RETURN
 
If objFSO.FileExists(strXenAppFarm) Then
  With objFSO.OpenTextFile(strXenAppFarm,FOR_READING,DO_NOT_CREATE)
    If .AtEndOfStream <> true then
      Dim strXenAppServers: strXenAppServers = .ReadAll()
      .close
      objFSO.DeleteFile strError,true
      objFSO.DeleteFile strXenAppFarm,true 
    End If
  End With
End If

If isEmpty(strXenAppServers) Then
  If strNoLogShow = True Then
    objLogFile.WriteLine "No Results of a XenApp Farm"
  Else 
    WScript.Echo "No Results of a XenApp Farm"
  End If
Else
  Dim arrServerLines: arrServerLines = Split(strXenAppServers,vbcrlf)
  Dim strServerLine
  For each strServerLine in arrServerLines
    If strServerLine <> "" and strServerLine <> vbcrlf Then
      If Left(strServerLine,1) <> "-" and left(strServerline,11) <> "Server Name" Then
        Dim arrServerFields: arrServerFields = split(strServerLine,"             ")
        Dim strXenAppServer: strXenAppServer = arrServerFields(0)
        Dim strLoadBal: strLoadBal = arrServerFields(1)
        Dim strCMDOLine: strCMDOLine = strCMDLine & strQUser & strXenAppServer & strFilter & strUserFilter
        Set objWshExec = objWshShell.Exec(strCMDOLine)

        Do While Not(objWshExec.stdOut.AtEndOfStream)
        	strXenAppUsers = objWshExec.stdOut.ReadLine
        Loop
        Dim strServerLoad
        If strLoadBal = 10000 And strXenAppUsers = 0 Then
          strServerLoad = "is in Maintenace"
        ElseIf strLoadBal = 10000 Then
          strServerLoad = "Load is Full"
          strRunServers = strRunServers + 1
        Else
          strServerLoad = "Load is not Full"
          strRunServers = strRunServers + 1
        End If
        strFarmUsers = strFarmUsers + strXenAppUsers
        objLogFile.WriteLine strXenAppServer & " has " & strXenAppUsers & " Users and the Server " & strServerLoad & " (" & strLoadBal & ")."
      End If
    End If
  Next
  objLogFile.WriteLine "=============================================================="
  Dim strAverage: strAverage = Round (strFarmUsers / strRunServers, 2)
  objLogFile.WriteLine "Citrix XenApp Farm has " & strFarmUsers & " users on " & strRunServers & " active servers (average of " & strAverage & " users on a Citrix server)."
  objLogFile.Close
  If strNoLogShow <> True Then
    objWshShell.run "%SystemRoot%\Notepad.exe " & strLogName
  End If
End If

Function CustomDateTimeFormat()
  CustomDateTimeFormat = year(now) & Right("0" & month(now),2) & Right("0" & day(now),2) & "-" & Right("0" & hour(now),2) & Right("00" & minute(now),2)
End Function

Disclaimer

These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.

Tags

xenapp codeshare xenapp codeshare Delete
xenapp tools xenapp tools Delete
xenapp scripts xenapp scripts Delete
xenapp xenapp Delete
users users Delete
server server Delete
load load Delete
farm farm Delete
Enter tags to add to this page:
Please wait 
Looking for a tag? Just start typing.
Related Links