Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Disconnect-ViSession {
- <#
- .SYNOPSIS
- Disconnects a connected vCenter Session.
- .DESCRIPTION
- Disconnects a open connected vCenter Session.
- .PARAMETER SessionList
- A session or a list of sessions to disconnect.
- .EXAMPLE
- PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 } | Disconnect-ViSession
- .EXAMPLE
- PS C:\> Get-VISession | Where { $_.Username -eq “User19″ } | Disconnect-ViSession
- .AUTHOR
- Alan Renouf
- .SOURCE
- https://blogs.vmware.com/PowerCLI/2011/09/list-and-disconnect-vcenter-sessions.html
- .OBTAINED
- 12/15/2015
- .EDITED
- Ryan Simeone - 12/15/2015
- #>
- [CmdletBinding()]
- Param(
- [Parameter(ValueFromPipeline=$true)]
- $SessionList,
- [switch]
- $All
- )
- Process {
- #Prevent disconnecting sessions in vSphere Client
- If(!$All){
- $SessionList = $SessionList | Where-Object {$_.UserAgent -notlike "VMWare*"}
- }
- #Prevent error trying to disconnect your current session, use Disconnect-VIServer
- $SessionList = $SessionList | Where-Object {$_.Status -ne "Current Session"}
- $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
- ForEach($Session in $SessionList){
- Write “Disconnecting Session for $($Session.Username) which has been active since $($Session.LoginTime)”
- $key = $Session.Key
- $SessionMgr.TerminateSession($key)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment