<?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>SiW Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/siw/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/siw/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Fri, 20 Sep 2013 10:52:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>SiW Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/siw/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>SiW &#8211; Robust Sessions and PowerCLI</title>
		<link>https://www.lucd.info/2013/08/27/siw-robust-sessions-and-powercli/</link>
					<comments>https://www.lucd.info/2013/08/27/siw-robust-sessions-and-powercli/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 27 Aug 2013 14:08:32 +0000</pubDate>
				<category><![CDATA[2013]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[remote session]]></category>
		<category><![CDATA[robust]]></category>
		<category><![CDATA[SiW]]></category>
		<category><![CDATA[VMworld]]></category>
		<category><![CDATA[VSVC4944]]></category>
		<category><![CDATA[Session]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=4534</guid>

					<description><![CDATA[One of the new features that came with PowerShell v3 was the introduction [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>One of the new features that came with <strong>PowerShell v3</strong> was the introduction of &#8220;<strong>robust</strong>&#8221; or &#8220;<strong>persistent</strong>&#8221; sessions. This is, imho, a huge improvement on the concept of <strong>remote sessions</strong> who were introduced in <strong>PowerShell v2</strong>.<br />
In short, before robust sessions, you had to keep the the client, that created the session, <strong>open</strong> to keep the remote session <strong>alive</strong>. With robust sessions, the client side can be closed, and you can reconnect to the robust session, even from another client.<br />
This new remote session concept is something that can be used to solve a number of known <a href="https://communities.vmware.com/community/vmtn/automationtools/powercli" target="_blank">PowerCLI</a> &#8220;issues&#8221;.</p>
<p><a href="https://lucd.info/wp-content/uploads/2013/08/siw-icon.png">                        <img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-4544" alt="siw-icon" src="https://lucd.info/wp-content/uploads/2013/08/siw-icon.png" width="226" height="224" srcset="https://www.lucd.info/wp-content/uploads/2013/08/siw-icon.png 226w, https://www.lucd.info/wp-content/uploads/2013/08/siw-icon-150x150.png 150w" sizes="(max-width: 226px) 100vw, 226px" /></a></p>
<p style="font-size: 9px;">Siw stands for Server in Waiting. We selected that term since we liked the sound of it, no double entendre should be suspected <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><span id="more-4534"></span>Some of these issues, that most of you probably know:</p>
<ul>
<li><a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/html/Connect-VIServer.html" target="_blank">Connect-VIServer</a> can be slow</li>
<li>The 1st cmdlet in a session is slow</li>
<li>Keep the PowerCLI version aligned on all the stations were it is used</li>
<li>Have powerfull stations to run scripts against &#8220;big&#8221; vSphere environments</li>
</ul>
<p>The idea is that you would have a &#8220;workhorse&#8221; station, with enough resources to cope with the bigger vSphere environments. On this workhorse station you set up a number of robust sessions, and when you need to run a PowerCLI script on a client, you would connect from the client to such a robust session, and execute your script in that robust session.</p>
<p><a href="https://lucd.info/wp-content/uploads/2013/08/siw-schematic.png">         <img decoding="async" class="alignnone size-medium wp-image-4543" alt="siw-schematic" src="https://lucd.info/wp-content/uploads/2013/08/siw-schematic-300x123.png" width="413" height="180" /></a></p>
<p>To make the creation and usage of these robust sessions in combination with PowerCLI easier, I created a number of functions.</p>
<h2>The Functions</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function New-SiwSession {

&lt;#  
.SYNOPSIS  Creates a number of robust PowerCLI sessions   
.DESCRIPTION The function will create a number of
  robust remote sessions on a specific server. The
  remote sessions will be initialised to run PowerCLI
  scripts
.NOTES  Author:  Luc Dekens  
.PARAMETER ComputerName
  The hostname of the server where the robust PowerCLI
  sessions will be created
.PARAMETER Credential
  A PSCredential object that contains the credentials for
  the robust session.
.PARAMETER SiwType
  The name prefix for the robust PowerCLI sessions
.PARAMETER NumberOfSessions
  The number of robust PowerCLI sessions that shall be
  created. The maximum number is 99.
.PARAMETER vServer
  The name of the vSphere server to which the robust PowerCLI
  session shall connect
 .PARAMETER vCredential
  The PSCredential object that will be used to connect to the
  $vServer server. If $null, the session credentials will be
  used

.EXAMPLE
  PS&gt; New-SiwSession -ComputerName $tgtServer -Credential $cred `
  &gt;&gt; -SiwType &quot;PowerCLI&quot; -NumberOfSessions 3 -vServer &quot;vCenter&quot;
#&gt;

  param(
    [String]$ComputerName,
    [System.Management.Automation.PSCredential]$Credential,
    [String]$SiwType,
    [ValidateRange(1,99)] 
    [Int]$NumberOfSessions,
	[String]$vServer,
    [System.Management.Automation.PSCredential]$vCredential
  )

  if(!$vCredential){$vCredential = $Credential}
  $pclStartUp = {
    param($vServer,$Credential)

    Add-PSSnapin VMware.VimAutomation.Core
    Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null
    Connect-VIServer -Server $vServer -Credential $Credential | Out-Null
    Get-Datacenter | Out-Null
  }

  $newSiwOption = @{
    IdleTimeout = -1
    NoMachineProfile = $true
    SkipCACheck = $true
    SkipCNCheck = $true
    SkipRevocationCheck = $true
  }
  $SiwOptions = New-PSSessionOption @newSiwOption 

  $newSiwSession = @{
    ComputerName = $ComputerName
    Credential = $Credential
    EnableNetworkAccess = $true
    Name = $null
    SessionOption = $SiwOptions
  }

  1..$NumberOfSessions | %{
    $newSiwSession.Name = $SiwType + (&quot;-{0:d2}&quot; -f $_)
    $SiwSession = New-PSSession @newSiwSession
    $pclInit = @{
      Session = $SiwSession
      ScriptBlock = $pclStartUp
      ArgumentList = $vServer,$Credential
    }
    Invoke-Command @pclInit
    Disconnect-PSSession -Session $SiwSession -Confirm:$false | Out-Null
  }
}

function Get-SiwSession {

&lt;#  
.SYNOPSIS  Returns a robust PowerCLI session
.DESCRIPTION The function will return an available robust
  PowerCLI session. If no robust sessions are available,
  function  will return $null
.NOTES  Author:  Luc Dekens  
.PARAMETER ComputerName
  The hostname of the server where the robust PowerCLI
  sessions will be created
.PARAMETER Credential
  A PSCredential object that contains the credentials for
  the robust session.
.PARAMETER SiwType
  The name prefix for the robust PowerCLI sessions
.EXAMPLE
  PS&gt; Get-SiwSession -ComputerName $tgtServer -Credential $cred `
  &gt;&gt; -SiwType &quot;PowerCLI&quot;
#&gt;

  param(
    [String]$ComputerName,
    [System.Management.Automation.PSCredential]$Credential,
    [String]$SiwType 
  )

  $getSiwSession = @{
    ComputerName = $ComputerName
    Credential = $Credential
  }

  Get-PSSession @getSiwSession |
  Where {$_.Name -match &quot;^$($SiwType)&quot; -and $_.Availability -eq &quot;None&quot;} |
  Get-Random
}

function Abort-SiwSession {

&lt;#  
.SYNOPSIS  Aborts robust PowerCLI sessions   
.DESCRIPTION The function abort all robust PowerCLI
  sessions on a server.
.NOTES  Author:  Luc Dekens  
.PARAMETER Session
  The handle of the session that needs to be aborted.
.EXAMPLE
  PS&gt; Abort-SiwSession -Session $pcliSession
#&gt;

  param( 
    [CmdletBinding()]
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [System.Management.Automation.Runspaces.PSSession]$Session
  )

  $session | %{
    $cimInstance = @{
      ComputerName = $_.ComputerName
            ClassName = &quot;Win32_Process&quot;
      Filter = &quot;Name Like 'wsmprovhost.exe'&quot;
    }
    Get-CimInstance @cimInstance |
    Invoke-CimMethod -MethodName Terminate -Confirm:$false
  }
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 38</strong>: The function allows a request to create up to99 robust PowerCLI sessions. But keep in mind that PowerShell itself has a throttle limit of 32 sessions. In other words PowerShell handle the instructions in the first 32 sessions and place the other remote sessions in wait.</p>
<p><strong>Line 45-52</strong>: These are the lines that set up the robust session as a PowerCLI session.Line 49: The robust session can&#8217;t handle PowerCLI warning messages correctly, so we suppress those.</p>
<p><strong>Line 51</strong>: This random PowerCLI cmdlet avoids the delay you normally see on the 1st PowerCLI cmdlet that is executed in a new session.</p>
<p><strong>Line 54-60</strong>: The session options.</p>
<p><strong>Line 63-69</strong>: The parameters used to create the remote session.</p>
<p><strong>Line 71-81</strong>: The loop and code block that create the requested number of robust PowerCLI sessions</p>
<p><strong>Line 46,77,79</strong>: Notice how the values for the PowerCLI code block are passed to the remote session.</p>
<p><strong>Line 80</strong>: The remote session is immediately disconnected.</p>
<p><strong>Line 107</strong>: Since you can only connect to a robust session with the same credentials as the ones which created the session, the function requires these as a parameter as well.</p>
<p><strong>Line 118</strong>: To avoid over-using the 1st robust session that is returned, the function will return a &#8220;random&#8221; available session.</p>
<p><strong>Line 146</strong>: Since this function is/was primarily used to &#8220;kill&#8221; unresponsive functions, it uses the Win32_Process instance that corresponds with a robust session on that target remote server.</p>
<h2>Sample Usage</h2>
<p>Since the hard work for setting up and using robust PowerCLI sessions is done in the functions, the use of these robust PowerCLI sessions is quite simple.</p>
<p>First you create a number of sessions.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$tgtServer = &quot;SRV1&quot;
$vCenter = &quot;VCENTER&quot;

$pswdSecure = ConvertTo-SecureString -String $pswd -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$pswdSecure

# Create 3 Siw sessions
New-SiwSession -ComputerName $tgtServer -Credential $cred -SiwType &quot;PowerCLI&quot; -NumberOfSessions 3 -vServer $vCenter</pre><p></p>
<p>Once the sessions are created, you connect to one of the available ones to run your PowerCLI code.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag"># Use one of the sessions to run PowerCLI code on SRV1
$session = Get-SiwSession -ComputerName $tgtServer -Credential $cred -SiwType &quot;PowerCLI&quot;

Connect-PSSession -Session $session | Out-Null
Invoke-Command -Session $session -ScriptBlock {Get-VMHost}
Disconnect-PSSession -Session $session | Out-Null

# Drop the sessions
Get-PSSession -ComputerName $tgtServer -Credential $cred | Remove-PSSession</pre><p></p>
<p>Should something go wrong and the robust PowerCLI session is stuck, you can try the abort function to kill the session.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag"># Abort the sessions
Get-PSSession -ComputerName SRV1 -Credential $cred | Abort-SiwSession</pre><p></p>
<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2013/08/27/siw-robust-sessions-and-powercli/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
	</channel>
</rss>
