Get-Ryan

[PowerCLI] Disconnect-VISession

Dec 15th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Disconnect-ViSession {
  2. <#
  3. .SYNOPSIS
  4. Disconnects a connected vCenter Session.
  5.  
  6. .DESCRIPTION
  7. Disconnects a open connected vCenter Session.
  8.  
  9. .PARAMETER  SessionList
  10. A session or a list of sessions to disconnect.
  11.  
  12. .EXAMPLE
  13. PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 } | Disconnect-ViSession
  14.  
  15. .EXAMPLE
  16. PS C:\> Get-VISession | Where { $_.Username -eq “User19″ } | Disconnect-ViSession
  17.  
  18. .AUTHOR
  19.  Alan Renouf
  20.  
  21. .SOURCE
  22.  https://blogs.vmware.com/PowerCLI/2011/09/list-and-disconnect-vcenter-sessions.html
  23.  
  24. .OBTAINED
  25.  12/15/2015
  26.  
  27. .EDITED
  28.  Ryan Simeone - 12/15/2015
  29. #>
  30.  
  31. [CmdletBinding()]
  32.  
  33. Param(
  34.       [Parameter(ValueFromPipeline=$true)]
  35.       $SessionList,
  36.  
  37.       [switch]
  38.       $All
  39.       )
  40.  
  41. Process {
  42.    
  43.     #Prevent disconnecting sessions in vSphere Client
  44.     If(!$All){
  45.         $SessionList = $SessionList | Where-Object {$_.UserAgent -notlike "VMWare*"}
  46.     }
  47.  
  48.     #Prevent error trying to disconnect your current session, use Disconnect-VIServer
  49.     $SessionList = $SessionList | Where-Object {$_.Status -ne "Current Session"}
  50.  
  51.     $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
  52.    
  53.     ForEach($Session in $SessionList){
  54.        
  55.         Write “Disconnecting Session for $($Session.Username) which has been active since $($Session.LoginTime)
  56.         $key = $Session.Key
  57.         $SessionMgr.TerminateSession($key)
  58.        
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment