Sponsored Links

Login






Lost Password?
No account yet? Register
CitrixThings!
Citrix Session and License Usage Print E-mail
Written by Andrew Wood   
Wednesday, 14 October 2009

This VBS script queries the farm's license server for current license usage - and then provides a session count for each of the active servers in the farm.

This allows you to readily validate the current license for the entire farm use from a single command. 

The output is in a CSV format - allowing you to import it into the spreadsheet of your choice as required.

 

Click here to download the script

---------

<job>
<runtime>
<description>
Citrix License Useage Script

</description>

        <named name="license" helpstring="Show Ctx License Count" type="boolean" required="false"  />
        <named name="session" helpstring="Show Session Information" type="boolean" required="false"  />
        <named name="licsrv" helpstring="Override default license server" type="string" required="false"  />
</runtime>
<reference object="MetaFrameCOM.MetaFrameFarm"/>
<script language="VBScript">

' =================================================================
' =================================================================
  cScript =      "ctxusage.wsf"
' Description:     .
'
' Author:          This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
'
' This script is free to be copied, modified or reused.
' Please retain acknowledgement of the authors above, increment
' the version below including the author making the change and a
' brief description of the change.

  nVersion = 0.2
 
' Revision History
' 0.1   Initial Revision
' 0.2    Dynamically retreive the farm's license server (15/10/09)
' =================================================================
' =================================================================
On Error Resume Next



' =================================================================
' Initial Declarations
' =================================================================

'Event Type constants
Const EVENT_TYPE_ERROR = 1 
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

Dim nICAActiveCount, nICADisconnectedCount, nRDPActiveCount, nRDPDisconnectedCount
Dim nUseStd, nUseAdv, nUseEnt, nUsePlt


'Parameters
Dim objNamedArgs, objFarm
Set objArgs = WScript.Arguments
set objNamedArgs = objArgs.Named

nUseStd = 0
nUseAdv = 0
nUseEnt = 0
nUsePlt = 0

Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(MetaFrameWinFarmObject)

strMyCitrixLicenseServer = objFarm.WinFarmObject.LicenseServerName

If Len(strMyCitrixLicenseServer) = 0 Then
    wscript.echo "Unable to determine license server name"
    wscript.quit
End If


'
' =================================================================
' Retrieve parameters
' =================================================================

If objArgs.Count < 1 Or objArgs.Count > 3 Then
    objArgs.ShowUsage()
    WScript.Quit
Else
    If objNamedArgs.Exists("license") Then
        lCheckLicense = objNamedArgs("license")
    Else
        lCheckLicense = False
    End If

    If objNamedArgs.Exists("session") Then
        lCheckSession = objNamedArgs("session")
    Else
        lCheckSession = False
    End If
    
    If objNamedArgs.Exists("licsrv") Then
        if Len(objNamedArgs("licsrv")) = 0 Then
            wscript.echo "============================================"
            wscript.echo "ERROR: No override license server specified."
            wscript.echo "============================================"
            objArgs.ShowUsage()
            wscript.quit
        Else
            wscript.echo "OVERRIDE default license server <" & strMyCitrixLicenseServer & "> with <" & objNamedArgs("licsrv") & ">"
            strMyCitrixLicenseServer = objNamedArgs("licsrv")
        End If
    End If
    
    ' - determine session count for each type of server... then can display licenses
    Call chkCtxLicense_CheckSession(lCheckSession)
    
    If lCheckLicense Then Call chkCtxLicense_CheckLicense
    
End if

' =================================================================
' Main Body
' =================================================================


'++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' ctxUsage_GetLicenseInfo
'
' Query the farm License Server to obtain current license usage info,
'
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Sub chkCtxLicense_CheckLicense
    On Error Resume Next
    
    'Connect to Citrix WMI
    Dim objWMIService
    Dim colItems
    
    Set objWMIService = GetObject("winmgmts:\\" & strMyCitrixLicenseServer & "\root\CitrixLicensing")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Citrix_GT_License_Pool",,48)
    
    'Count up licenses
    Dim intCount
    Dim intInUseCount
    Dim arrLicense(5)
    Const TOTAL = 0
    Const STANDARD = 1
    Const ADVANCED = 2
    Const ENTERPRISE = 3
    Const PLATINUM = 4
    n=0
    for each i in arrLicense
        arrLicense(n) = 0
        n=n+1
    next
    For Each objItem in colItems
        Select Case objItem.PLD    
            Case "MPS_STD_CCU"    
                arrLicense(STANDARD) = arrLicense(STANDARD) + objItem.Count
                arrLicense(TOTAL) = arrLicense(TOTAL) + objItem.Count
                intInUseCount = intInUseCount + objItem.InUseCount
            Case "MPS_ADV_CCU"    
                arrLicense(ADVANCED) = arrLicense(ADVANCED) + objItem.Count
                arrLicense(TOTAL) = arrLicense(TOTAL) + objItem.Count
                intInUseCount = intInUseCount + objItem.InUseCount
            Case "MPS_ENT_CCU"    
                arrLicense(ENTERPRISE) = arrLicense(ENTERPRISE) + objItem.Count
                arrLicense(TOTAL) = arrLicense(TOTAL) + objItem.Count
                intInUseCount = intInUseCount + objItem.InUseCount
            Case "MPS_PLT_CCU"    
                arrLicense(PLATINUM) = arrLicense(PLATINUM) + objItem.Count            
                arrLicense(TOTAL) = arrLicense(TOTAL) + objItem.Count
                intInUseCount = intInUseCount + objItem.InUseCount
        End Select

        intInUseCount = intInUseCount + objItem.InUseCount
    Next
    
    'Start Message
    Dim strMessage
    WScript.Echo "Std Max,Std Use,Adv Max,Adv Use,Ent Max,Ent Use,Plt Max,Plt Use,Total Avail,Total InUse,% Use"
    nUseTotal = nUseStd+nUseAdv+nUseEnt+nUsePlt
    WScript.Echo arrLicense(STANDARD) & "," & nUseStd & "," & _
                 arrLicense(ADVANCED) & "," & nUseAdv & "," & _
                 arrLicense(ENTERPRISE) & "," & nUseEnt  & "," & _
                 arrLicense(PLATINUM) & "," & nUsePlt  & "," &_
                 arrLicense(TOTAL) & "," & (nUseTotal) & "," & _
                 FormatPercent(intInUseCount / arrLicense(TOTAL),0,0)
                
End Sub

Sub chkCtxLicense_CheckSession (lDisplayInfo)
       On Error Resume Next
       Dim aServer, aSession

       Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
       
       objFarm.Initialize(MetaFrameWinFarmObject)
       wscript.echo "ServerName,TotalSessionCount,Load,CtxEdition,ICA-ActiveUsers,ICA-DiscUsers,RDP-ActiveUsers,RDP-DiscUsers"    
       For Each aZone In objFarm.Zones
            For Each aServer in aZone.OnlineServers
          
                Set objServer = CreateObject("MetaFrameCOM.MetaFrameServer")
                objServer.Initialize MetaFrameWinSrvObject, uCase(aServer.ServerName)
                Set winserver = objServer.WinServerObject5
                call chkCtxLicense_UniqueSessionCount (aServer.ServerName,winserver.MPSEdition)              
                
                If lDisplayInfo Then
                    WScript.echo  objServer.ServerName & "," _
                              & objServer.SessionCount & "," _
                              & winserver.ServerLoad & "," _
                              & winserver.MPSEdition & "," _
                              & nICAActiveCount & "," _
                              & nICADisconnectedCount & "," _
                              & nRDPActiveCount & "," _
                              & nRDPDisconnectedCount
                End If
            Next
       Next

End Sub

Sub chkCtxLicense_UniqueSessionCount (cServerName, cEdition)

    nICAActiveCount         = 0
    nICADisconnectedCount     = 0
    nRDPActiveCount         = 0
    nRDPDisconnectedCount     = 0    
    
    If cServerName = ""  Then
        Exit Sub
    End if

    Set objShell = CreateObject("Wscript.Shell")
    Set objFS = CreateObject("Scripting.FileSystemObject")

    cTmpFile = objFS.GetSpecialFolder(2).ShortPath & "\" & objFS.GetTempName
    
    'Run command and redirect stdout and stderr into temp file
    objShell.Run "%ComSpec% /c %SystemRoot%\System32\QUSER.EXE /SERVER:" & cServerName & " >" & cTmpFile & " 2>&1", 0, True

    On Error Resume Next
    'Open the temp file
    Set objTempFile = objFS.OpenTextFile(cTmpFile)
    ' Read first line
    cLine = objTempFile.ReadLine
    If Err.Number = 0 Then
        On Error Goto 0
    
        If Left(Trim(cLine), 8) = "USERNAME" Then
    
            Do While lEndOfFile <> True
                cLine = objTempFile.ReadLine
            
                cUserName = Trim(Mid(cLine,1,23))
                cSessionName = UCase((Trim(Mid(cLine,24,3))))
                cSessionID = Trim(Mid(cLine,43,2))
                cState = UCase(Trim(Mid(cLine,47,8)))
                        
                If cSessionID <> "0" Then ' - commented out - allows match up of sessions if include /admin console

                    Select Case cEdition
                        Case "STD"
                            nUseStd = nUseStd + 1
                        Case "ADV"
                            nUseAdv = nUseAdv + 1
                        Case "ENT"
                            nUseEnt = nUseEnt + 1
                        Case "PLT"
                            nUsePlt = nUsePlt + 1
                    End Select
                End If
                
                    If cSessionName = "ICA" Then
                        If cState = "ACTIVE" Then nICAActiveCount=nICAActiveCount+1
                        If cState = "DISCONNECTED" Then nICADisconnectedCount=nICADisconnectedCount+1
                    Else
                        If cSessionName = "RDP" Then
                            If cState = "ACTIVE" Then nRDPActiveCount=nRDPActiveCount+1
                            If cState = "DISCONNECTED" Then nRDPDisconnectedCount=nRDPDisconnectedCount+1
                        End If
                    End If
                

                lEndOfFile = objTempFile.AtEndOfStream
            Loop
        End If
    End If

    objTempFile.Close
    'Delete it
    objFS.DeleteFile cTmpFile, True

End Sub

</script>
</job>

 
© 2010 Citrix Things!
Citrix Things! is based on software released under the GNU/GPL License.