Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 230663

Terminate Idle VI Client Sessions with PowerCLI Script

$
0
0

Hello PowerCLI gurus

 

I've been looking on the net for a PowerCLI script that would terminate all VI client sessions that have been idle for x period of time.  I've found a post on VMware forums (http://communities.vmware.com/message/914858?z=zI0r8n) but had no luck with it.  I've also found a script created by A.Mikkelsen at http://www.amikkelsen.com/?p=384 but that one is not working either.

 

Following is the actual script by A.Mikkelsen that is based on the code by LucD in the previously mentioned forum.

 

START

##################################################################################
# The script terminates all idle user sessions if idle for longer that xx   #
#                             #
# Created by: Anders Mikkelsen, 2010                                #
##################################################################################
clear
# Add-PSSnapin VMware.VimAutomation.Core

# $server = "vcenter server"
# $user   = "vcenter username"
# $pwd    = "vcenter password"

# Add 1 hour extra the the time, due to timestamp difference between MSSQL and Windows time.
#  5 hours idle time = 360
# 10 hours idle time = 660
$intOlderThan = 60

# Connect-VIServer $server -User $user -Password $pwd
# Connect-VIServer $server

$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($intOlderThan) -lt (Get-Date)){
  $oldSessions += $sess.Key
  #write "$($sess.UserName)"
   }
}

# Terminale session than are idle for longer than approved ($intOlderThan)
$sessMgr.TerminateSession($oldSessions)

Disconnect-VIServer * -Confirm:$false

END

 

The error message I receive is:

 

START

Exception calling "TerminateSession" with "1" argument(s): "A specified parameter was not correct.

"

At D:\Scripts\vc_terminate_idle_sessions.ps1:36 char:26

+ $sessMgr.TerminateSession <<<< ($oldSessions)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

END

 

I should mention that my knowledge of PowerCLI is next to nothing so any help in tweaking this script to get it working with PowerCLI 5.1 Release 2 and VC 5 U2 would be much appreciated.


Viewing all articles
Browse latest Browse all 230663

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>