Jump to content
Welcome to our new Citrix community!
  • 1

Monitor API: How to get maximum users/used licenses per day?


tylital520

Question

Hi,

I am trying to find a way to query Monitor API to find out what is the maximum amount of Apps and Virtual Desktops users we have in one day. I also want to have this information to check what is the maximum number of licenses used per day.

 

Does anyone know how this data could be fetched from Monitor API?

I've tried the following, but running the script takes forever, and I am not even sure if what I am getting is the correct data. There must be a better way than this:

 

$API_URL = "http://domain.com/Citrix/Monitor/Odata/v4/Data"

$USERNAME = ""
$PASSWORD = ConvertTo-SecureString "" -AsPlainText `
                                      -Force

$CREDENTIALS = new-object -typename System.Management.Automation.PSCredential `
                          -argumentlist $USERNAME, $PASSWORD


$SessionSummary = @{}
$SessionCount = @{}

# Get session activity summaries by desktop group
$SessionSummaries = Invoke-RestMethod –URI ”$API_URL/SessionActivitySummaries” `
                                      -Method Get `
                                      -Credential $CREDENTIALS | Select -ExpandProperty value

# Group returned desktop group session activity summaries by summarydate property 
$DailyStats = $SessionSummaries | Where {$_.summarydate.split("T")[0] -eq (Get-Date (Get-Date).AddDays(-1) -Format yyyy-MM-dd)} `
                                | Group-Object summarydate

# Process each group
ForEach($group in $DailyStats){

    $TotalSessions = @()
    
    # Process each desktop group session activity summary in the group
    ForEach($TimeGroup in $group.group){
        
        # Get desktop group
        $DesktopGroup = (Invoke-RestMethod –URI ”$API_URL/DesktopGroups”  `
                                           -Method Get `
                                           -Credential $CREDENTIALS).value | Where {$_.id -eq $TimeGroup.DesktopGroupId}

        # Get all machines belonging to that desktop group
        $Machines = (Invoke-RestMethod –URI ”$API_URL/Machines”  `
                                       -Method Get `
                                       -Credential $CREDENTIALS).value | Where {$_.desktopgroupid -eq $DesktopGroup.Id}

        # Get sessions that belong to those machines 
        ForEach($machine in $Machines){
        
            $Sessions = (Invoke-RestMethod –URI ”$API_URL/Sessions”  `
                                           -Method Get `
                                           -Credential $CREDENTIALS).value  | Where {$_.machineid -eq $Machine.Id}

            # Add session to TotalSessions if session was active while summarydate was taken
            ForEach($session in $Sessions){
            
                if((Get-Date $TimeGroup.summarydate) -ge (Get-Date $session.startDate)){

                    if(($session.EndDate -eq $null) -or ((Get-Date $TimeGroup.summarydate) -le (Get-Date $session.endDate))){

                        $TotalSessions += $session
                        
                    }

                }
            }
            
        }
                                                           
    }

    # Add found sessions per timestamp (group) to SessionSummary
    $SessionSummary.add($Group.name, $TotalSessions)
      
}  

# Go through found session per timestamp, find unique users per session
# and add results to SessionCount-hashtable
ForEach($Entry in $TimeTable.GetEnumerator()){

    $Time = Get-Date $Entry.key -Format "yyyy-MM-dd hh:mm:ss"
    $Users = $Entry.value.userid | Select -Unique | Measure | Select -ExpandProperty count
    $SessionCount.Add($Time, $Users)

}
 
$SessionCount

 

Link to comment

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...