I was reading tweets today and someone asked for a way to retrieve how often desktops are used – how many times users log into virtual desktops. I am not sure the reason behind the request but this is possible via the VMware View PowerCLI command Get-EventReport.
Get-EventReport -viewName user_events | Where {$_.eventtype -eq "AGENT_CONNECTED"} | Group-Object machineid | Format-Table *
The problem with Get-EventReport is that the output is the machineid property, not allowing administrator to identify the virtual desktops.
I created a script that leverages the work Clint Kitson did while creating the VMware View Pool Membership Management using PowerCLI. The key to make this work is to query the local ADAM database to uncover the relationship between machineid and the real name of the desktop (ipHostNumber).
Download the scripts here, and keep them in the same folder in one of the View connection servers. To execute use:
.\get_vm_login_count.ps1 -username "username" -password "password" -connectionbroker ipaddress
The result will be a list of desktops (hostname) and the total number of times each desktop was used. This is determined by the eventtype AGENT_CONNECTED. You may also use the parameter –eventtype to choose from the possible list of VMware View events:
- AGENT_CONNECTED
- AGENT_DISCONNECTED
- AGENT_ENDED
- AGENT_PENDING
- AGENT_PENDING_EXPIRED
- AGENT_RECONFIGURED
- AGENT_RECONNECTED
- AGENT_RESUME
- AGENT_SHUTDOWN
- AGENT_STARTUP
- AGENT_SUSPEND
- refer to the VMware View 5.0 Integration Guide for more information
The script will automatically produce results for this year, but the parameter –startdate can also be used to provide a specific date or a date range.
1: #####
2: ## 09/30/2011
3: # Andre Leibovici - @andreleibovici - myvirtualcloud.net
4: #
5: # EXAMPLE
6: # .\get_vm_login_count -username "admin" -password "password" -connectionbroker 192.168.2.2
7: ######
8:
9: Param ($username="", $password="", $connectionbroker="127.0.0.1", $startdate=(Get-Date -Day 01 -Month 01 -Hour 0 -Minute 0 -Second 0), $eventtype="AGENT_CONNECTED")
10:
11: if((!$username -or !$password)) {
12: write "Missing Parameters";exit
13: }
14:
15: $cn = $null
16: $cn = @{}
17: $cns = .\custom_vmware_view_ps.ps1 -username $username -password $password -connectionbroker $connectionbroker -action view-get-objects -ou servers | %{
18: $cn.add($_.cn,$_.ipHostNumber)
19: }
20: $event = Get-EventReport -viewName user_events -startDate $startdate | Where {$_.eventtype -eq $eventtype}
21: $event | Group-Object machineid | Select-Object *,@{n="hostname";e={$cn.($_.name)}} | Where-Object {$_.hostname} | Format-Table *
This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net. Visit myvirtualcloud.net for more articles about Virtualization, VDI and End User Computing.






4 comments
Skip to comment form ↓
Beew
02/08/2012 at 12:32 am (UTC -7) Link to this comment
This is a something we’ve been looking for. Hopefully it can help us charge users/departments based on the number of times they’ve been using certain VM’s, but the we would have to know the usernames as well. Is it possible to retrieve the usernames as well with this script?
Andre Leibovici
02/08/2012 at 8:39 am (UTC -7) Link to this comment
@Beew
You can use the cmdlets below to retrieve the amount of times each user has logged in.
Get-EventReport -viewName user_events | Group-Object userdisplayname | Format-Table *
or
Get-EventReport -viewName user_events | Group-Object userdisplayname, machineid | Format-Table *
Regards,
Andre
burak ç?nar
04/29/2013 at 2:00 am (UTC -7) Link to this comment
i got an error when i try to run ps.
i’m using view 5.1
The term ‘Get-EventReport’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is co
rrect and try again.
At C:\Users\user\Desktop\get_vm_login_count.ps1:20 char:25
+ $event = Get-EventReport <<<< -viewName user_events -startDate $startdate | Where {$_.eventtype -eq $eventtype}
+ CategoryInfo : ObjectNotFound: (Get-EventReport:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Andre Leibovici
05/03/2013 at 10:58 am (UTC -7) Link to this comment
burak ç?nar, It seems to me you have not loaded the View PoserShell Module. You should start a PowerShell shell session using the shortcut View places in your server.
-Andre