<?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>Disconnect-VIServer Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/disconnect-viserver/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/disconnect-viserver/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Mon, 16 Jan 2012 20:12:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.10</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>Disconnect-VIServer Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/disconnect-viserver/</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>Proxy cmdlet revisited: Connect-VIServer and Disconnect-VIServer</title>
		<link>https://www.lucd.info/2012/01/16/proxy-cmdlet-revisted-connect-viserver-and-disconnect-viserver/</link>
					<comments>https://www.lucd.info/2012/01/16/proxy-cmdlet-revisted-connect-viserver-and-disconnect-viserver/#respond</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Mon, 16 Jan 2012 18:12:05 +0000</pubDate>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[Connect-VIServer]]></category>
		<category><![CDATA[Disconnect-VIServer]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=3855</guid>

					<description><![CDATA[In PowerCLI 5.0.1 a handy feature that showed the connected vSphere Servers in [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In <a href="https://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli?view=overview" target="_blank">PowerCLI 5.0.1</a> a handy feature that showed the connected vSphere Servers in the title bar of the PowerCLI window was apparently removed.<br />
In a PowerCLI Community <a href="https://communities.vmware.com/thread/342796?tstart=0" target="_blank">thread</a> some users found this a useful feature that they would like to have back.<br />
I&#8217;m sure the PowerCLI Team will listen to their users and fix this problem in the coming PowerCLI version.</p>
<p>But while we are waiting for a new PowerCLI build that brings back the title bar text, you can fix this for yourself thanks to the proxy cmdlet feature.</p>
<p><span id="more-3855"></span></p>
<p>Proxy cmdlets is (another) handy feature in PowerShell which allows you to modify and extend existing cmdlets. You can add parameters, change the output and do other crazy things. In the <a href="https://blogs.msdn.com/b/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx" target="_blank">Extending and/or Modifing Commands with Proxies</a> post from Jeffrey Snover you can find all the details.</p>
<p>In that same post you can also find the MetaProgramming module which makes it a breeze to customise cmdlets.</p>
<p>You just call the New-ProxyCommand and redirect the output to a .ps1 file, which you can then use to write your customisations for the cmdlet.</p>
<p>In this case I did that for the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI501/html/Connect-VIServer.html" target="_blank">Connect-VIServer</a> and <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI501/html/Disconnect-VIServer.html" target="_blank">Disconnect-VIServer</a> cmdlets as follows</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">New-ProxyCommand Connect-VIServer &gt; .\Connect-VIServer.ps1

New-ProxyCommand Disconnect-VIServer &gt; .\Disconnect-VIServer.ps1</pre><p></p>
<p>The resulting .ps1 files can now be edited to introduce our customisations to these cmdlets.</p>
<h2>My Connect-VIServer</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Connect-VIServer{
  [CmdletBinding(DefaultParameterSetName='Default')]
  param(
  [Parameter(ParameterSetName='Default', Mandatory=$true, Position=0)]
  [ValidateNotNullOrEmpty()]
  [System.String[]]
  ${Server},

  [Parameter(ParameterSetName='Default')]
  [ValidateNotNull()]
  [ValidateRange(0, 65535)]
  [System.Int32]
  ${Port},

  [Parameter(ParameterSetName='Default')]
  [ValidateSet('http','https')]
  [System.String]
  ${Protocol},

  [Parameter(ParameterSetName='Default', ValueFromPipeline=$true)]
  [ValidateNotNullOrEmpty()]
  [System.Management.Automation.PSCredential]
  ${Credential},

  [Parameter(ParameterSetName='Default', ValueFromPipeline=$true)]
  [Alias('Username')]
  [System.String]
  ${User},

  [Parameter(ParameterSetName='Default')]
  [System.String]
  ${Password},

  [Parameter(ParameterSetName='Default')]
  [System.String]
  ${Session},

  [Parameter(ParameterSetName='Default')]
  [switch]
  ${NotDefault},

  [Parameter(ParameterSetName='Default')]
  [switch]
  ${SaveCredentials},

  [Parameter(ParameterSetName='Default')]
  [switch]
  ${AllLinked},

  [Parameter(ParameterSetName='Menu', Mandatory=$true)]
  [switch]
  ${Menu})

  begin
  {
    try {
      $outBuffer = $null
      if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
      {
        $PSBoundParameters['OutBuffer'] = 1
      }
      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Connect-VIServer', [System.Management.Automation.CommandTypes]::Cmdlet)
      $scriptCmd = {&amp; $wrappedCmd @PSBoundParameters }
      $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
      $steppablePipeline.Begin($PSCmdlet)
    } catch {
      Throw
    }
  }

  process
  {
    try {
      $steppablePipeline.Process($_)
    } catch {
      Throw
    }
    switch($defaultVIServers.Count){
      0 {
        $text = &quot;Not Connected&quot;
      }
      1 {
        $text = &quot;Connected to &quot; + $defaultVIServers[0].Name + &quot; as &quot; + $defaultVIServers[0].User
      }
      Default {
        $text = &quot;Connected to &quot; + $defaultVIServers.Count + &quot; servers: &quot;
        $text += [string]::Join(',',($defaultVIServers | %{$_.Name}))
      }
    }
    (Get-Host).UI.RawUI.WindowTitle = $text
  }

  end
  {
    try {
      $steppablePipeline.End()
    } catch {
      Throw
    }
  }
&lt;#

.ForwardHelpTargetName Connect-VIServer
.ForwardHelpCategory Cmdlet

#&gt;
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 1,107</strong>: We make this a function<br />
<strong>Line 78,90</strong>: The code we add to the original cmdlet. This will place the text in the title bar.</p>
<h2>My Disconnect-VIServer</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Disconnect-VIServer{
  [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
  param(
  [Parameter(Position=0, ValueFromPipeline=$true)]
  [ValidateNotNullOrEmpty()]
  [VMware.VimAutomation.ViCore.Types.V1.VIServer[]]
  ${Server},

  [switch]
  ${Force})

  begin
  {
    try {
      $outBuffer = $null
      if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
      {
        $PSBoundParameters['OutBuffer'] = 1
      }
      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Disconnect-VIServer', [System.Management.Automation.CommandTypes]::Cmdlet)
      $scriptCmd = {&amp; $wrappedCmd @PSBoundParameters }
      $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
      $steppablePipeline.Begin($PSCmdlet)
    } catch {
      Throw
    }
  }

  process
  {
    try {
      $steppablePipeline.Process($_)
    } catch {
      Throw
    }
    switch($defaultVIServers.Count){
      0 {
        $text = &quot;Not Connected&quot;
      }
      1 {
        $text = &quot;Connected to &quot; + $defaultVIServers[0].Name + &quot; as &quot; + $defaultVIServers[0].User
      }
      Default {
        $text = &quot;Connected to &quot; + $defaultVIServers.Count + &quot; servers: &quot;
        $text += [string]::Join(',',($defaultVIServers | %{$_.Name}))
      }
    }
    (Get-Host).UI.RawUI.WindowTitle = $text
  }

  end
  {
    try {
      $steppablePipeline.End()
    } catch {
      Throw
    }
  }
&lt;#

.ForwardHelpTargetName Disconnect-VIServer
.ForwardHelpCategory Cmdlet

#&gt;
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 1,65</strong>: We make this a function<br />
<strong>Line 36,48</strong>: The code we add to the original cmdlet. This will place the text in the title bar.</p>
<h2>Usage samples</h2>
<p>First you will need to dot-source the .ps1 files with the customised versions of both cmdlets.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">. ./Connect-VIServer.ps1

. ./Disconnect-VIServer.ps1</pre><p></p>
<p><a href="https://lucd.info/wp-content/uploads/2012/01/dot-source.png"><img decoding="async" class="alignnone  wp-image-3857" title="dot-source" src="https://lucd.info/wp-content/uploads/2012/01/dot-source.png" alt="" width="330" height="94" srcset="https://www.lucd.info/wp-content/uploads/2012/01/dot-source.png 412w, https://www.lucd.info/wp-content/uploads/2012/01/dot-source-300x85.png 300w" sizes="(max-width: 330px) 100vw, 330px" /></a></pre>
<p>We now have a function and a cmdlet for both.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-Command Connect-VIServer

Get-Command Disconnect-VIServer</pre><p></p>
<p><a href="https://lucd.info/wp-content/uploads/2012/01/commands.png"><img fetchpriority="high" decoding="async" class="alignnone  wp-image-3858" title="commands" src="https://lucd.info/wp-content/uploads/2012/01/commands.png" alt="" width="521" height="202" srcset="https://www.lucd.info/wp-content/uploads/2012/01/commands.png 651w, https://www.lucd.info/wp-content/uploads/2012/01/commands-300x116.png 300w" sizes="(max-width: 521px) 100vw, 521px" /></a></p>
<p>But PowerShell will use a function before a cmdlet when both have the same name. Exactly what we need !</p>
<p>If you want to learn more about these precedences, do a</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-Help About_command_precendence</pre><p></p>
<p><a href="https://lucd.info/wp-content/uploads/2012/01/precedence.png"><img decoding="async" class="alignnone  wp-image-3859" title="precedence" src="https://lucd.info/wp-content/uploads/2012/01/precedence.png" alt="" width="517" height="151" srcset="https://www.lucd.info/wp-content/uploads/2012/01/precedence.png 646w, https://www.lucd.info/wp-content/uploads/2012/01/precedence-300x87.png 300w" sizes="(max-width: 517px) 100vw, 517px" /></a></p>
<p>Now we are ready to use our customised cmdlets. When we do a first connect</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$vc = Connect-VIServer -Server MyVC</pre><p></p>
<p>the PowerCLI window will show</p>
<p><a href="https://lucd.info/wp-content/uploads/2012/01/MyVC-connected1.png"><img loading="lazy" decoding="async" class="alignnone  wp-image-3861" title="MyVC-connected" src="https://lucd.info/wp-content/uploads/2012/01/MyVC-connected1.png" alt="" width="406" height="64" srcset="https://www.lucd.info/wp-content/uploads/2012/01/MyVC-connected1.png 507w, https://www.lucd.info/wp-content/uploads/2012/01/MyVC-connected1-300x47.png 300w" sizes="auto, (max-width: 406px) 100vw, 406px" /></a></p>
<p>When we connect an additional vSphere Server</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$esx = Connect-VIServer -Server MyEsx1 -User root -Password MyPswd</pre><p></p>
<p>the PowerCLI window will show.</p>
<p><a href="https://lucd.info/wp-content/uploads/2012/01/MyEsx1-connected.png"><img loading="lazy" decoding="async" class="alignnone  wp-image-3862" title="MyEsx1-connected" src="https://lucd.info/wp-content/uploads/2012/01/MyEsx1-connected.png" alt="" width="498" height="59" srcset="https://www.lucd.info/wp-content/uploads/2012/01/MyEsx1-connected.png 622w, https://www.lucd.info/wp-content/uploads/2012/01/MyEsx1-connected-300x35.png 300w" sizes="auto, (max-width: 498px) 100vw, 498px" /></a></p>
<p>When we disconnect from one of the vSphere Servers, the title bar will be updated as well.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Disconnect-VIServer -Server $vc</pre><p></p>
<p>The title bar will show</p>
<p><a href="https://lucd.info/wp-content/uploads/2012/01/MyVC-disconnected.png"><img loading="lazy" decoding="async" class="alignnone  wp-image-3863" title="MyVC-disconnected" src="https://lucd.info/wp-content/uploads/2012/01/MyVC-disconnected.png" alt="" width="496" height="60" srcset="https://www.lucd.info/wp-content/uploads/2012/01/MyVC-disconnected.png 620w, https://www.lucd.info/wp-content/uploads/2012/01/MyVC-disconnected-300x36.png 300w" sizes="auto, (max-width: 496px) 100vw, 496px" /></a></p>
<p>There are of a couple of open problems</p>
<ul>
<li>The initial PowerCLI window shows &#8220;<strong>Not connected</strong>&#8220;. This could eventually be solved by adapting the PowerCLI initialisation script, but I didn&#8217;t want to change that script</li>
<li>The Disconnect-VIServer cmdlet also accepts a [<strong>string</strong>] for the <strong>Server</strong> argument. The proxy cmdlet doesn&#8217;t accept that, you will have to give it a VIServer object on the Server parameter.</li>
</ul>
<p>Enjoy !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2012/01/16/proxy-cmdlet-revisted-connect-viserver-and-disconnect-viserver/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
