A. Mikkelsen VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Thursday, March 18, 2010

The Must Have – Powershell PodCasts

Filed under: PodCasts (PS),PowerShell,Scripting — Tags: , , — A. Mikkelsen @ 13:39 pm

A few weeks ago I came across a podcast on http://get-scripting.blogspot.com about Powershell.

I highly recommend that all people using powershell check it out.

Get the latest and the archived podcasts at
http://get-scripting.blogspot.com/search/label/podcast

Monday, March 15, 2010

New layout

Filed under: Uncategorized — A. Mikkelsen @ 20:58 pm

Today I had to change the theme because the old theme, wasn’t compatible with the latest version of WordPress.org.

I hope you like it, I’ll try to personalize it during the next couple of weeks :-).

Wednesday, March 10, 2010

Powershell – List all vCenter client sessions & disconnect if inactive

Filed under: PowerShell,Scripting,Virtual Center — Tags: , , , — A. Mikkelsen @ 14:25 pm

Today I needed to create a list of all users connected to a vCenter sever through the Client.
And during so I created a few variants ūüôā

This creates a list of all usernames connected to the vCenter server through a vCenter Client.

$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){
   write "$($sess.UserName)"
}

If you want to see a witch clients have been idle for 60 minutes try this.

$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){
    if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){
        write "$($sess.UserName)"
      }
}

If you the want to terminate the sessions that have been inactive  for more than 60 minute

$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
$oldSessions = @()
foreach ($sess in $sessMgr.SessionList){
    if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){
        $oldSessions += $sess.Key
      }
}
$sessMgr.TerminateSession($oldSessions)

You can modify the hell out of this script to suit your needs and be my guest….

You can download the full script here.

The scripts are based on code found  at the PowerCLI community by LucD.
http://communities.vmware.com/message/914858#914858

Powered by WordPress