<?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>PSSnapin Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/pssnapin/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/pssnapin/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Fri, 12 Aug 2016 15:45:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.6</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>PSSnapin Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/pssnapin/</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>Universal PowerCLI Loader</title>
		<link>https://www.lucd.info/2016/05/05/universal-powercli-loader/</link>
					<comments>https://www.lucd.info/2016/05/05/universal-powercli-loader/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Thu, 05 May 2016 18:50:47 +0000</pubDate>
				<category><![CDATA[Module]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SnapIn]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Initialization]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[PSSnapin]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=5181</guid>

					<description><![CDATA[PowerCLI is great tool, and the Team behind it surprises us on a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="https://communities.vmware.com/community/vmtn/automationtools/powercli/overview" target="_blank">PowerCLI</a> is great tool, and the <a href="https://www.lucd.info/2014/03/15/powercli-5-5-r2-they-do-listen-to-you/" target="_blank">Team</a> behind it surprises us on a regular basis with a new Release. With the <strong>v6.x</strong> generation we witnessed the introduction of <strong>Modules</strong>. And the Team keeps adding further integration with other VMware products.</p>
<p>With the PowerCLI installation comes a shortcut to a PowerShell sessions, loaded with all the PowerCLI goodness. And this is ideal to make your first steps in the wonderful world of PowerShell and PowerCLI.</p>
<p>But soon you&#8217;ll start using more advanced features of PowerShell. You&#8217;ll be scheduling jobs, running parallel workflows, start using PowerCLI in Desired State Configuration (DSC). At that moment, the simple PowerCLI session doesn&#8217;t cut it anymore, and even the Init scripts that are installed together with PowerCLI, will not give you the exact environment as you want it. Hence my <strong>Universal PowerCLI Loader</strong>!</p>
<p><a href="https://www.lucd.info/2016/05/05/universal-powercli-loader/universal/" rel="attachment wp-att-5182"><img fetchpriority="high" decoding="async" class="alignnone wp-image-5182 size-medium" src="https://lucd.info/wp-content/uploads/2016/05/universal-300x250.jpg" alt="universal" width="300" height="250" srcset="https://www.lucd.info/wp-content/uploads/2016/05/universal-300x250.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/05/universal.jpg 373w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>I have been using a &#8220;<strong>Universal PowerCLI loader</strong>&#8221; function since quite some time now. I thought it was time to prettify it a bit, and share it with the community.</p>
<p><span id="more-5181"></span></p>
<p>To create the <strong>Universal PowerCLI loader</strong>, I first needed to take stock of what all the different <a href="https://communities.vmware.com/community/vmtn/automationtools/powercli/overview" target="_blank">PowerCLI</a> versions require and bring. The result of that inquiry is reflected in the following spreadsheet.</p>
<div class="ead-preview"><div class="ead-document" style="position: relative;padding-top: 90%;"><iframe src="//view.officeapps.live.com/op/embed.aspx?src=https%3A%2F%2Flucd.info%2Fwp-content%2Fuploads%2F2016%2F05%2FPowerCLI-Releases.xlsx" title="Embedded Document" class="ead-iframe" style="width: 100%;height: 100%;border: none;position: absolute;left: 0;top: 0;"></iframe></div><p class="embed_download"><a href="https://lucd.info/wp-content/uploads/2016/05/PowerCLI-Releases.xlsx" download>Download [13.03 KB] </a></p></div>
<p>Some clarifications on the table:</p>
<p><strong>Columns E, F and G</strong>: contain hyperlinks to the respective Release Notes, Download link and Online Helppage for each PowerCLI release. Note that the download will require you to enter a MyVMware account.</p>
<p><strong>Columns H and I</strong>: the minimum and maximum supported PowerShell version</p>
<p><strong>Columns J and K</strong>: the minimum and maximum supported OS (for workstations) on which you can use PowerCLI</p>
<p><strong>Columns L till P</strong>: the required .Net version</p>
<p><strong>Columns Q and R</strong>: the minimum and maximum supported vSphere version. These are just an indication, always use the <a href="https://www.vmware.com/resources/compatibility/sim/interop_matrix.php" target="_blank">VMware Product Interoperability Matrixes</a> to determine which version is required.</p>
<p><strong>Columns U and V</strong>: the number of cmdlets available for the &#8220;regular&#8221; modules and for the &#8220;Cloud&#8221; modules. To get the total number of cmdlets in a specific release, add these two numbers together.</p>
<p>Some interesting observations that can be made from this table:</p>
<ul>
<li>The table starts with <strong>PowerCLI 5.1.0.2</strong>, since I personally think you should never need an older version. If you do, let me know, and I&#8217;ll see if I can expand the table and the function.</li>
<li>Notice that with <strong>PowerCLI 5.8</strong> a separate installation for PowerCLI for Tenants was introduced (row 7)</li>
<li>With the <strong>PowerCLI v6.x</strong> series, modules were introduced</li>
<li>Not all cmdlets are currently migrated from PSSnapins to Modules, hence the mix of PSSnapin and Modules</li>
<li><strong>WMF 5</strong> support is included from the PowerCLI v6.3. Note that this is for the WMF 5 that comes with Windows 10. The WMF 5 that is still in preview, and that will probably be released with Windows 2016, is not yet officially supported</li>
</ul>
<h2>The Function</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Enable-PowerCLI
{
&lt;# .SYNOPSIS Load PowerCLI modules and PSSnapins .DESCRIPTION This function will load all requested PowerCLI modules and PSSnapins. The function will, depending on the installed PowerCLI version, determine what needs to be loaded. .NOTES Author: Luc Dekens .PARAMETER Cloud Switch to indicate if the Cloud related cmdlets shall be loaded .PARAMETER InitScript The PowerCLI PSSnapin have associated initialisation scripts. This switch will indicate if that script needs to be executed or not. .EXAMPLE PS&gt; Enable-PowerCLI
.EXAMPLE
  PS&gt; Enable-PowerCLI -Cloud
#&gt;

  [CmdletBinding()]
  param(
    [Switch]$Cloud,
    [Switch]$InitScript
  )

  $PcliPssnapin = @{
    'VMware.VimAutomation.License' = @(2548067)
    'VMware.DeployAutomation' =@(2548067,3056836,3205540,3737840)
    'VMware.ImageBuilder' = @(2548067,3056836,3205540,3737840)
  }

  $PcliModule = @{
    'VMware.VimAutomation.Core' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.Vds' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.Cloud' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.PCloud' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.Cis.Core' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.Storage' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.HA' = @(2548067,3056836,3205540,3737840)
    'VMware.VimAutomation.vROps' = @(3056836,3205540,3737840)
    'VMware.VumAutomation' = @(3056836,3205540,3737840)
    'VMware.VimAutomation.License' = @(3056836,3205540,3737840)
  }

  # 32- or 64-bit process
  $procArch = (Get-Process -Id $pid).StartInfo.EnvironmentVariables[&quot;PROCESSOR_ARCHITECTURE&quot;]
  if($procArch -eq 'x86'){
    $regPath = 'HKLM:\Software\VMware, Inc.\VMware vSphere PowerCLI'
  }
  else{
    $regPath = 'HKLM:\Software\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
  }
  
  # Check if PowerCLI (regular or Tenant) is installed
  if(!(Test-Path -Path $regPath))
  {
    $regPath = $regPath.Replace('VMware vSphere PowerCLI','VMware vSphere PowerCLI for Tenants')
    if(!(Test-Path -Path $regPath))
    {
        Throw 'Can not find a PowerCLI installation!'        
    }
  }
  
  # Get build
  $buildKey = 'InstalledBuild'
  Try{
    $pcliBuild = Get-ItemProperty -Path $regPath -Name $buildKey |
        Select -ExpandProperty $buildKey -ErrorAction Stop
  }
  Catch{
    Throw &quot;PowerCLI doesn't seem to be installed on this system!&quot;
  }

  # Get installation path
  $installPathKey = 'InstallPath'
  Try{
    $pcliInstallPath = Get-ItemProperty -Path $regPath -Name $installPathKey |
        Select -ExpandProperty $installPathKey -ErrorAction Stop
  }
  Catch{
    Throw &quot;PowerCLI doesn't seem to be installed on this system!&quot;
  }

  # Load modules
  if($pcliBuild -ge 2548067)
  {
    $loadedModule = Get-Module -Name VMware* -ErrorAction SilentlyContinue | %{$_.Name}
    if($loadedModule -and $pcliBuild -ge 3737840)
    {
      $loadedModule = $loadedModule | where{$_ -notmatch 'Common$|SDK$'}
    }
  
    $targetModule = $PcliModule.GetEnumerator() | where{$_.Value -contains $pcliBuild} | %{$_.Key}
    $targetModule = $targetModule | where{$loadedModule -notcontains $_}
    if(!$Cloud)
    {
      $targetModule = $targetModule | where{$_ -notmatch 'Cloud'}
    }
    if($targetModule)
    {
      $targetModule | where{$loadedModule -notcontains $_.Name} | %{
        Import-Module -Name $_ -Verbose:$false
      }
    }
  }
  
  # Load PSSnapin
  $loadedSnap = Get-PSSnapin -Name VMware* -ErrorAction SilentlyContinue | %{$_.Name}
  if($pcliBuild -ge 3737840)
  {
    $loadedSnap = $loadedSnap | where{$_ -notmatch 'Core$'}
  }

  $targetSnap = $PcliPssnapin.GetEnumerator() | where{$_.Value -contains $pcliBuild} | %{$_.Key}
  $targetSnap = $targetSnap | where{$loadedSnap -notcontains $_}
  if(!$Cloud)
  {
    $targetSnap = $targetSnap | where{$_ -notmatch 'Cloud'}
  }
  if($targetSnap)
  {
    $targetSnap | where{$loadedSnap -notcontains $_} | %{
      Add-PSSnapin -Name $_ -Verbose:$false

      # Run initialisation script
      if($InitScript)
      {
        $filePath = &quot;{0}Scripts\Initialize-{1}.ps1&quot; -f $pcliInstallPath,$_.ToString().Replace(&quot;.&quot;, &quot;_&quot;)
        if (Test-Path $filePath) {
          &amp; $filePath
        }
      }
    }
  }
}</pre><p></p>
<h3>Annotations</h3>
<p><strong>Line 27-44</strong>: in these tables the relation between the PowerCLI version and the required modules and PSSnapin are defined</p>
<p><strong>Line 47-53</strong>: depending if the PowerShell session is a 32- or 64-bit process, another registry branch needs to be accessed.</p>
<p><strong>Line 56-63</strong>: check if PowerCLI, or PowerCLI for Tenants, is actually installed on the station before continuing</p>
<p><strong>Line 66-83</strong>: the buildnumber and the installation path are retrieved from the registry</p>
<p><strong>Line 86-106</strong>: loads the requested PowerCLI modules, if we are in a PowerCLI v6.x environment. The logic first determines which modules are available, then substracts the ones that are already loaded and finally loads the missing ones. This means that the function can be called more than once.</p>
<p><strong>Line 94</strong>: in combination with the table, define earlier, this line will check for each known PowerCLI module, if it is present in the current PowerCLI installation.</p>
<p><strong>Line 109-135</strong>: similar logic as before with the modules, to load the required PowerCLI PSSnapins.</p>
<p><strong>Line 127-133</strong>: execute the PSSnapin initialisation scripts if requested. The default is not to run these scripts.</p>
<h2>Sample Usage</h2>
<p>The purpose of the function is quite straightforward, it loads the PowerCLI PSSnapin and modules. The call to the function can be placed in the beginning of your scripts. No more need to handle the loading of PowerCLI in your scripts.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Enable-PowerCLI</pre><p></p>
<p>This will load all PowerCLI PSSnapins and modules, depending on the installed PowerCLI version, minus the Cloud related modules and PSSnapins.</p>
<p>If you need the Cloud functionality, you just need to do</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Enable-PowerCLI -Cloud</pre><p></p>
<p>The <strong>Enable-PowerCLI</strong> function is available on my Github <a href="https://github.com/lucdekens/Scripts.git" target="_blank">Scripts</a> repository.</p>
<p>Note that the <strong>Enable-PowerCLI</strong> function can <strong>not</strong> be added to a module. The <strong>Add-PSSnapin</strong> cmdlets executed inside a module, are not propagated to the caller!<br />
This is a know, unresolved issue.</p>
<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2016/05/05/universal-powercli-loader/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
			</item>
	</channel>
</rss>
