Get vCenter User Sessions and Idle times with PowerCLI

Today I was looking into a small “nice to have” notification system for users that had left their vSphere clients open and logged into vCenter. I found this great bit of script to list currently logged in users over at blog.vmpros.nl and thought I would expand on this in my own way to generate a handy list of logged in users and their current idle time – similar to the way the “Sessions” tab in the vSphere client displays user session information. When I got home this evening I expanded on the original script from vmpros.nl to create the following:

 

$Now = Get-Date
$Report = @()
$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
foreach ($sess in $sessMgr.SessionList){
   $time = $Now - $sess.LastActiveTime
   # Our time calculation returns a TimeSpan object instead of DateTime, therefore formatting needs to be done as follows:
   $SessionIdleTime = '{0:00}:{1:00}:{2:00}' -f $time.Hours, $time.Minutes, $time.Seconds
   $row = New-Object -Type PSObject -Property @{
   		Name = $sess.UserName
		LoginTime = $sess.LoginTime
		IdleTime = $SessionIdleTime
	} ## end New-Object
	$Report += $row
}
$Report

 

[download id=”6″]

 

Here is an example of the output of the script:

 

Using this bit of PowerCLI script, it should be easy for you to create your own notification system based on user session idle time, or some functionality that would disconnect idle users. Let me know if you do improve on the above, or if you have any other suggestions.

 

Get a list of VMs in a cluster and find their HA Restart Priority with PowerCLI

 

This is just a quick post today using one of the most common PowerCLI cmdlets, Get-VM.

 

I needed to find a list of VMs in a specific cluster, and grab their respective HA Restart Priority settings. PowerCLI makes this nice and simple. First of all, connect to your vCenter server, then find the name of your cluster you would like to search. You can use the Get-Cluster cmdlet on its own to list all clusters in a specific vCenter installation.

 

Connect-VIServer yourvcenterservername
Get-Cluster

 

Next, use this one liner to list all the VMs in your specified cluster along with their respective HARestartPriority settings. This will also sort the list in alphabetical order and export it into a CSV file in C:\temp. If you wish to rather just list the items in your shell instead, remove the Export-CSV bit at the end.

 

Get-Cluster "yourclustername" | Get-VM | Select Name,HARestartPriority | Sort Name | Export-CSV C:\temp\vmrestartpriorities.csv

Powershell – Check Free Memory script

 

Here’s a quick script I did using Powershell to check your free memory and report back the amount in MB and GB.

 

$freemem = Get-WmiObject -Class Win32_OperatingSystem

# Display free memory on PC/Server
"---------FREE MEMORY CHECK----------"
""
"System Name     : {0}" -f $freemem.csname
"Free Memory (MB): {0}" -f ([math]::round($freemem.FreePhysicalMemory / 1024, 2))
"Free Memory (GB): {0}" -f ([math]::round(($freemem.FreePhysicalMemory / 1024 / 1024), 2))
""
"------------------------------------"

Download the script here

 

The figure is determined and held in the $freemem variable. After that we simply output two lines to show the amount in MB and GB. We use a simple function to divide the figure by 1024 and round it off, displaying the result with two decimal places. The figure needs to be divided by 1024 as the variable holds the amount in Kilobytes (KB), therefore to determine Megabytes (MB), we divide by 1024. The second figure for GB requires one more division.