AlbertWT
Virtuoso
Virtuoso

Who is looking at my VM using the VSphere Console at the same time ?

Jump to solution

People,

I'd like to know if it is possible to know which VI Client / vSphere console user is currently viewing the Vmware console at the same time for a particular VM that I'm working on ?
Because at the moment, someone else is accessing my VM console and may have forgot to close the VI Client and now I'm stuck.

This is the error I got when I try to connect to the console.  MKS Internal Error due to "RemoteDisplay.maxConnections with a value of 1" is in effect.

Thanks,
/* Any kind of comment or input would be greatly appreciated */
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert

Hello, AlbertWT-

Well, it looks like there is another way.  You could get the number of current MKS (Mouse Keyboard Screen) connections for the given VM, and use that value for your Select statement.  That should correspond with the users with current MKS connections to that VM.

So, putting that together with what Hosted suggested and you modified, it would be like:
$strVMName = "myVM"
## get VM; will use an ExtensionData value from this later
$vm = Get-VM -Name $strVMName
## get the events that are for console connects, sort, and then select the first X items, based on the number of current MKS connections
Get-VIEvent $vm | ? {$_.FullFormattedMessage -like "Remote console Connected*"} | Sort CreatedTime -Descending | Select UserName, CreatedTime -First $vm.ExtensionData.Summary.Runtime.NumMksConnections | ft -AutoSize

And, a variation on the last line, this one that checks the VIEvent's type (vs. doing a string compare):

## same, but check the event type instead of a string compare on the FullFormattedMessage (just to not rely on string compares)
Get-VIEvent $vm | ? {$_ -is [VMware.Vim.VmRemoteConsoleConnectedEvent]} | Sort CreatedTime -Descending | Select UserName, CreatedTime -First $vm.ExtensionData.Summary.Runtime.NumMksConnections | ft -AutoSize

Both should work, but the second version removes the dependency on the string comparision.  So, if the FullFormattedMessage changes in future vSphere implementations, things should still work (though, this would still be vulnerable to breaking if VMware changes the type name for the event).  Somewhat of a preference thing.

How does that do for you?

Note:  by sorting on CreatedTime, this process assumes that the current console connections were made sequentially.  That is, if someone has been connected to the VM's console since yesterday, and you connect / disconnect / re-connect, _you_ will have the two most recent Connect events.

View solution in original post

0 Kudos
6 Replies
Hosted201110141
Enthusiast
Enthusiast

This may be what you are looking for.

get-vm -Name MyVM | Get-VIEvent | where { $_.fullFormattedMessage -like "Remote console Connected*" } | select userName

AlbertWT
Virtuoso
Virtuoso

Many thanks for your response Hosted,

I have modified the script to be like the following for simplicity purpose:

get-vm -Name "DB01-VM" | Get-VIEvent | where { $_.fullFormattedMessage -like "Remote console Connected*" } | Sort CreatedTime -Descending | select  userName, CreatedTime -First 1 | ft -AutoSize

However the test scenario is failed since it can only returns the last person who tried to access into the VM and that was me (Myself) so it is not always correct in determining who is currently viewing the VM right now.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

You should drop the -First 1 parameter from the Select-Object to see all console connections over time.

The way you do it, you only see the most recent connection.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AlbertWT
Virtuoso
Virtuoso

Luc,

Yes I was trying to get the correct way of identifying who is currently using the Vsphere console session into a particular VM but it seems that it is not possible.

So I must conclude that the script above at least the only way to know who is currently connecting by manually parsing the result by eyes.

Any other input and suggestion would be greatly appreciated.

Thanks,

AWT

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
mattboren
Expert
Expert

Hello, AlbertWT-

Well, it looks like there is another way.  You could get the number of current MKS (Mouse Keyboard Screen) connections for the given VM, and use that value for your Select statement.  That should correspond with the users with current MKS connections to that VM.

So, putting that together with what Hosted suggested and you modified, it would be like:
$strVMName = "myVM"
## get VM; will use an ExtensionData value from this later
$vm = Get-VM -Name $strVMName
## get the events that are for console connects, sort, and then select the first X items, based on the number of current MKS connections
Get-VIEvent $vm | ? {$_.FullFormattedMessage -like "Remote console Connected*"} | Sort CreatedTime -Descending | Select UserName, CreatedTime -First $vm.ExtensionData.Summary.Runtime.NumMksConnections | ft -AutoSize

And, a variation on the last line, this one that checks the VIEvent's type (vs. doing a string compare):

## same, but check the event type instead of a string compare on the FullFormattedMessage (just to not rely on string compares)
Get-VIEvent $vm | ? {$_ -is [VMware.Vim.VmRemoteConsoleConnectedEvent]} | Sort CreatedTime -Descending | Select UserName, CreatedTime -First $vm.ExtensionData.Summary.Runtime.NumMksConnections | ft -AutoSize

Both should work, but the second version removes the dependency on the string comparision.  So, if the FullFormattedMessage changes in future vSphere implementations, things should still work (though, this would still be vulnerable to breaking if VMware changes the type name for the event).  Somewhat of a preference thing.

How does that do for you?

Note:  by sorting on CreatedTime, this process assumes that the current console connections were made sequentially.  That is, if someone has been connected to the VM's console since yesterday, and you connect / disconnect / re-connect, _you_ will have the two most recent Connect events.

0 Kudos
AlbertWT
Virtuoso
Virtuoso

Hi Matt,

Thanks for your reply, so at least with your script I can minimize the search scope of the people who is using the console.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos