<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PeetersOnline &#187; session</title>
	<atom:link href="http://www.peetersonline.nl/index.php/tag/session/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peetersonline.nl</link>
	<description>Using Powershell for (virtually) everything!</description>
	<lastBuildDate>Fri, 19 Aug 2011 09:22:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Finding and Stopping VI Sessions with Powershell</title>
		<link>http://www.peetersonline.nl/index.php/vmware/finding-and-stopping-vi-sessions-with-powershell/</link>
		<comments>http://www.peetersonline.nl/index.php/vmware/finding-and-stopping-vi-sessions-with-powershell/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 11:23:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://www.peetersonline.nl/?p=104</guid>
		<description><![CDATA[If you have been trying out the VMware VI Toolkit for Windows Powershell, you have probably noticed. You have to disconnect (disconnect-viserver) each session you set up to the VI Server (connect-viserver), or it will linger for eternity (or until the next reboot at least). That&#8217;s why I always include a disconnect-viserver command at the <a href='http://www.peetersonline.nl/index.php/vmware/finding-and-stopping-vi-sessions-with-powershell/'>[...]</a>
Related posts:<ol>
<li><a href='http://www.peetersonline.nl/index.php/vmware/calculate-vsphere-5-licenses-with-powershell/' rel='bookmark' title='Calculate vSphere 5 Licenses with Powershell (UPDATED)'>Calculate vSphere 5 Licenses with Powershell (UPDATED)</a></li>
<li><a href='http://www.peetersonline.nl/index.php/vmware/remove-vmware-memory-limits-with-powershell/' rel='bookmark' title='Remove vmware memory limits with Powershell'>Remove vmware memory limits with Powershell</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>If you have been trying out the <a href="http://www.vmware.com/" target='_blank'>VMware</a> <a href="http://www.vmware.com/sdk/vitk_win/index.html" target='_blank'>VI Toolkit</a> for Windows <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target='_blank'>Powershell</a>, you have probably noticed. You have to disconnect (disconnect-viserver) each session you set up to the VI Server (connect-viserver), or it will linger for eternity (or until the next reboot at least). That&#8217;s why I always include a disconnect-viserver command at the end of my <a href="http://www.vmware.com/sdk/vitk_win/index.html" target='_blank'>VI Toolkit</a> scripts. But what happens in real life: when debugging a script, it hardly ever reaches the end of the script. It will stop at an error or be cancelled by me to fix some bug I notice. I regularly check the Sessions tab in my VI Client and terminate all my idle sessions to keep VI clean and fast. So why not automate that too? Here&#8217;s how:</p>
<blockquote><p>function Get-Session<br />
{<br />
	$Global:SI = Get-View ServiceInstance<br />
	$Global:SM = Get-View $SI.Content.SessionManager<br />
	Return $SM.SessionList<br />
}</p>
<p>function Stop-Session<br />
{<br />
	Process<br />
		{<br />
		ForEach ($Session in $_)<br />
			{<br />
			If ($Session.Key -ne $SM.CurrentSession.Key)<br />
				{<br />
					$Key = $session.Key<br />
					$SM.TerminateSession($Key)<br />
					Write-Host &#8220;Session $Key terminated.&#8221;<br />
					$Key = $null<br />
				}<br />
			Else<br />
				{<br />
				Write-Warning &#8220;Cannot terminate current session.&#8221;<br />
				}<br />
			}<br />
		}<br />
}</p>
</blockquote>
<p>The function Get-Session lists the currently active sessions. You can then filter them as you do any collection of objects in <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target='_blank'>Powershell</a> and pipe them to Stop-Session to have them terminated. Note that VI Client sessions appear to create two actual sessions and that the API (and therefore my function) will not allow you to terminate the current session.<br />
Example:</p>
<blockquote><p>Get-Session | Where { ((Get-Date) &#8211; $_.LastActiveTime).TotalHours -ge 8 } | Stop-Session</p>
</blockquote>
<p>Enjoy!</p>
<p>Related posts:<ol>
<li><a href='http://www.peetersonline.nl/index.php/vmware/calculate-vsphere-5-licenses-with-powershell/' rel='bookmark' title='Calculate vSphere 5 Licenses with Powershell (UPDATED)'>Calculate vSphere 5 Licenses with Powershell (UPDATED)</a></li>
<li><a href='http://www.peetersonline.nl/index.php/vmware/remove-vmware-memory-limits-with-powershell/' rel='bookmark' title='Remove vmware memory limits with Powershell'>Remove vmware memory limits with Powershell</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.peetersonline.nl/index.php/vmware/finding-and-stopping-vi-sessions-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>To Disconnect(-VIServer) or not to Disconnect</title>
		<link>http://www.peetersonline.nl/index.php/vmware/to-disconnect-viserver-or-not-to-disconnect/</link>
		<comments>http://www.peetersonline.nl/index.php/vmware/to-disconnect-viserver-or-not-to-disconnect/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:04:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[Disconnect]]></category>
		<category><![CDATA[Disconnect-VIServer]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://www.peetersonline.nl/index.php/vmware/to-disconnect-viserver-or-not-to-disconnect/</guid>
		<description><![CDATA[With the release of VI Toolkit v1.0, the bèta-cmdlet Get-VIServer has been renamed to Connect-VIServer, which more accurately describes the action. The opposite action is now also available in the form of Disconnect-VIServer. Every script you start with Connect-VIServer should end with Disconnect-VIServer if you&#8217;d like to keep the amount of open sessions to your <a href='http://www.peetersonline.nl/index.php/vmware/to-disconnect-viserver-or-not-to-disconnect/'>[...]</a>
Related posts:<ol>
<li><a href='http://www.peetersonline.nl/index.php/vmware/remove-vmware-memory-limits-with-powershell/' rel='bookmark' title='Remove vmware memory limits with Powershell'>Remove vmware memory limits with Powershell</a></li>
<li><a href='http://www.peetersonline.nl/index.php/vmware/calculate-vsphere-5-licenses-with-powershell/' rel='bookmark' title='Calculate vSphere 5 Licenses with Powershell (UPDATED)'>Calculate vSphere 5 Licenses with Powershell (UPDATED)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>With the release of <a href="http://www.vmware.com/sdk/vitk_win/index.html" target='_blank'>VI Toolkit</a> v1.0, the bèta-cmdlet Get-VIServer has been renamed to Connect-VIServer, which more accurately describes the action. The opposite action is now also available in the form of Disconnect-VIServer. <strong>Every script you start with Connect-VIServer should end with Disconnect-VIServer</strong> if you&#8217;d like to keep the amount of open sessions to your Virtual Center Server to a minimum. And you do if you like having any sort of performance from your VC Server.</p>
<p>Here&#8217;s three ways you can make sure you don&#8217;t end up with unnecessary sessions:</p>
<p><span id="more-50"></span></p>
<p><strong><u>1. Find scripts that lack the Disconnect-VIServer command:</u></strong></p>
<blockquote><p>$Path = &#8220;D:\Scripts&#8221;<br />
$ConnectMatches = Get-ChildItem $Path -Recurse |<br />
   Where {$_.PSIsContainer -eq $False} |<br />
   Select-String &#8220;Connect-VIServer&#8221; |<br />
   Where {$_.Line -notmatch &#8220;Disconnect&#8221;}<br />
$DisconnectMatches = Get-ChildItem $Path -Recurse |<br />
   Where {$_.PSIsContainer -eq $False} |<br />
   Select-String &#8220;Disconnect-VIServer&#8221;<br />
Compare-Object -ReferenceObject $ConnectMatches`<br />
  -DifferenceObject $DisconnectMatches -Property Path</p></blockquote>
<p><strong><u>2. Disconnect old sessions through the VI Client GUI</u></strong></p>
<p>On the Administration page of the VI Client, click on the Sessions tab. Sort by Status, Select the sessions that have been idle for a long time and click Terminate.</p>
<p><strong><u>3. Use a script to disconnect old sessions</u></strong></p>
<p>That&#8217;s more like it! Now who would want to go clicking and sorting about in the VI Client? ;) Thank goodness we have the <a href="http://www.vmware.com/sdk/vitk_win/index.html" target='_blank'>VI Toolkit</a>. Here&#8217;s the code:</p>
<blockquote><p>$VCServerName = &#8220;YourVCServerName&#8221;<br />
$HoursOld = 24        # Modify value at your pleasure</p>
<p>$VC = Connect-VIServer $VCServerName<br />
$ServiceInstance = Get-View ServiceInstance<br />
$SessionManager = Get-View $ServiceInstance.Content.SessionManager<br />
$SessionManager.SessionList |<br />
   Where {$_.LastActiveTime -lt (Get-Date).AddHours(-$HoursOld)} |<br />
   % {$SessionManager.TerminateSession($_.Key)}<br />
Disconnect-VIServer -Confirm:$False<br />
Write-Host &#8220;The VI Toolkit ROCKS!&#8221;</p></blockquote>
<p>Enjoy!</p>
<p>Related posts:<ol>
<li><a href='http://www.peetersonline.nl/index.php/vmware/remove-vmware-memory-limits-with-powershell/' rel='bookmark' title='Remove vmware memory limits with Powershell'>Remove vmware memory limits with Powershell</a></li>
<li><a href='http://www.peetersonline.nl/index.php/vmware/calculate-vsphere-5-licenses-with-powershell/' rel='bookmark' title='Calculate vSphere 5 Licenses with Powershell (UPDATED)'>Calculate vSphere 5 Licenses with Powershell (UPDATED)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.peetersonline.nl/index.php/vmware/to-disconnect-viserver-or-not-to-disconnect/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

