The error is caused by the fact that you are trying to kill your own session (if it was started longer than the time interval ago).
Apparently in vSphere 5.* they included a fail-safe mechanism to avoid that.
You can catch this by adding a simple test.
## max number of idle minutes for sessions to keep
$intOlderThan=60
$serviceInstance=Get-View'ServiceInstance'
## get the session manager object
$sessMgr=Get-View$serviceInstance.Content.sessionManager
## array to hold info about stale sessions
$oldSessions=@()
foreach ($sessin$sessMgr.SessionList){
if (($sess.LastActiveTime).addminutes($intOlderThan) -lt (Get-Date) -and
$sess.Key-ne$sessMgr.CurrentSession.Key){
$oldSessions+=$sess.Key
} ## end if
} ## end foreach
## if there are any old sessions, terminate them; else, just write message to the Warning stream
if (($oldSessions|Measure-Object).Count-gt0) {
## Terminate sessions than are idle for longer than approved ($intOlderThan)
$sessMgr.TerminateSession($oldSessions)
} ## end if
else {Write-Warning"No sessions that have been idle for more than '$intOlderThan' minutes; no action taken"}