<?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>Visual Studio Code Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/visual-studio-code/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/visual-studio-code/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Tue, 17 May 2016 20:24:08 +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>Visual Studio Code Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/visual-studio-code/</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>Visual Studio Code and PowerCLI</title>
		<link>https://www.lucd.info/2016/04/23/visual-studio-code-powercli/</link>
					<comments>https://www.lucd.info/2016/04/23/visual-studio-code-powercli/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sat, 23 Apr 2016 21:45:12 +0000</pubDate>
				<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[Visual Studio Code]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=5149</guid>

					<description><![CDATA[In a blog post on November 16th 2015 Microsoft announced PowerShell support in [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In a <a href="https://blogs.msdn.microsoft.com/powershell/2015/11/16/announcing-powershell-language-support-for-visual-studio-code-and-more/" target="_blank">blog post</a> on <strong>November 16th 2015</strong> Microsoft announced <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank">PowerShell support</a> in <a href="https://code.visualstudio.com/" target="_blank">Visual Studio Code</a>. And as it befits the &#8220;new&#8221; Microsoft, this PowerShell extension for Visual Studio Code (VSC) was placed on <a href="https://github.com/PowerShell/vscode-powershell" target="_blank">GitHub</a>.</p>
<p>VSC offers features that are currently not present in the <a href="https://technet.microsoft.com/en-us/library/dd315244.aspx" target="_blank">PowerShell ISE</a>, one of the more important ones for now being <strong>Git support</strong>. And another, not negligible feature, VSC is a <strong>free product</strong>. On the <a href="https://vmwarecode.slack.com/messages/powercli/" target="_blank">VMware{code} <strong>powercli</strong></a> Slack channel, which you should check out, the new editor was discussed briefly.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/pcli-and-vsc-2/" rel="attachment wp-att-5154"><img decoding="async" class="alignnone size-full wp-image-5154" src="https://lucd.info/wp-content/uploads/2016/04/PCLI-and-VSC.png" alt="PCLI-and-VSC" width="345" height="127" srcset="https://www.lucd.info/wp-content/uploads/2016/04/PCLI-and-VSC.png 345w, https://www.lucd.info/wp-content/uploads/2016/04/PCLI-and-VSC-300x110.png 300w" sizes="(max-width: 345px) 100vw, 345px" /></a></p>
<p>With the <a href="https://github.com/PowerShell/vscode-powershell/blob/master/CHANGELOG.md#060" target="_blank">0.6.0 release</a> of <strong>PowerShell for VS@Code</strong>, a couple of important new features were introduced. For the PowerCLI users, the addition of a VSC specific &#8216;<strong>profile</strong>&#8216;, makes editing your PowerCLI scripts in VSC a lot easier. Want to try it out, read on!</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/vsc-060/" rel="attachment wp-att-5200"><img fetchpriority="high" decoding="async" class="alignnone wp-image-5200 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/vsc-060-300x200.jpg" alt="vsc-060" width="300" height="200" srcset="https://www.lucd.info/wp-content/uploads/2016/04/vsc-060-300x200.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/vsc-060.jpg 612w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><span id="more-5149"></span></p>
<h2>Setting up VSC for PowerCLI</h2>
<p><a href="https://communities.vmware.com/community/vmtn/automationtools/powercli" target="_blank">PowerCLI</a>, at the time of writing, still consists of a number of <strong>PSSnapins</strong> and <strong>Modules</strong>. In my <a href="https://www.lucd.info/2016/05/05/universal-powercli-loader/" target="_blank">Universal PowerCLI Loader</a> post, I introduced a generic function, named <strong>Enable-PowerCLI</strong>, that will, for each <strong>official</strong> PowerCLI release, make sure that all PSSnapins and Modules (from v6.* onwards) are loaded. We will use this function to set up PowerCLI in our VSC.</p>
<p>The VSC profile is stored in a file called &#8220;%UserProfile%\Documents\WindowsPowerShell\<strong>Microsoft.VSCode_profile.ps1</strong>&#8220;.</p>
<p>In that file I store my <strong>Enable-PowerCLI</strong> function, and at the end of the file I call the function.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/profile1/" rel="attachment wp-att-5201"><img decoding="async" class="alignnone wp-image-5201 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/profile1-300x178.jpg" alt="profile1" width="300" height="178" srcset="https://www.lucd.info/wp-content/uploads/2016/04/profile1-300x178.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/profile1.jpg 593w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Since the <strong>PowerShell for VS@Code</strong> 0.6.1 release, the execution of the profile.ps1 is set to <strong>true</strong> by <strong>default</strong>. You can skip the following paragraph.</p>
<p>In the 0.6.0 release, the execution of the profile was not set by default. You could enable this via the &lt;File&gt;&lt;Preferences&gt;&lt;User Settings&gt; menu entry. In the editor the default settings and your personal <strong>settings.json</strong> file are opened. In your <strong>settings.json</strong> file, enter the line <strong>&#8220;powershell.enableProfileLoading&#8221;: true</strong>, this will make sure the profile.ps1 is executed the next time you open VSC.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/profile2/" rel="attachment wp-att-5202"><img loading="lazy" decoding="async" class="alignnone wp-image-5202 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/profile2-300x54.jpg" alt="profile2" width="300" height="54" srcset="https://www.lucd.info/wp-content/uploads/2016/04/profile2-300x54.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/profile2-768x138.jpg 768w, https://www.lucd.info/wp-content/uploads/2016/04/profile2-1024x184.jpg 1024w, https://www.lucd.info/wp-content/uploads/2016/04/profile2.jpg 1300w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p>Now restart your VSC, and lo and behold, all PowerCLI cmdlets are known and available under <strong>Intellisense</strong>.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/vsc-pcli1-2/" rel="attachment wp-att-5205"><img loading="lazy" decoding="async" class="alignnone wp-image-5205 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/vsc-pcli1-1-300x64.jpg" alt="vsc-pcli1" width="300" height="64" srcset="https://www.lucd.info/wp-content/uploads/2016/04/vsc-pcli1-1-300x64.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/vsc-pcli1-1.jpg 614w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/vsc-pcli21/" rel="attachment wp-att-5203"><img loading="lazy" decoding="async" class="alignnone wp-image-5203 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/vsc-pcli21-300x68.jpg" alt="vsc-pcli21" width="300" height="68" srcset="https://www.lucd.info/wp-content/uploads/2016/04/vsc-pcli21-300x68.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/vsc-pcli21.jpg 672w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<h2>Snippets</h2>
<p>In VSC there is a <strong>snippet</strong> feature, which allows you to define a code block that can be called by using a <strong>shortname</strong> in the editor. This is a handy way to have your PowerCLI code templates available at all times, no need to learn by hearth or &#8220;feeling lucky&#8221; in the search engine of your choice.</p>
<p>A snippet is packaged in a JSON file. The snippets file for PowerShell is &#8220;%appdata%\Code\User\snippets\powershell.json&#8221;. In that file you have one or more snippets of code.</p>
<p>The following is a short example of template code for the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI60R3/html/Get-Stat.html" target="_blank">Get-Stat</a> cmdlet for VMs.</p>
<p>In the snippets folder in the powershell.json file we add the definition for the PCLIStatVM snippet.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/snippet1/" rel="attachment wp-att-5215"><img loading="lazy" decoding="async" class="alignnone wp-image-5215 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/snippet1-300x126.jpg" alt="snippet1" width="300" height="126" srcset="https://www.lucd.info/wp-content/uploads/2016/04/snippet1-300x126.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/snippet1-768x323.jpg 768w, https://www.lucd.info/wp-content/uploads/2016/04/snippet1.jpg 798w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p>We can now call the snippet by just entering the name.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/snippet2/" rel="attachment wp-att-5216"><img loading="lazy" decoding="async" class="alignnone wp-image-5216 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/snippet2-300x51.jpg" alt="snippet2" width="300" height="51" srcset="https://www.lucd.info/wp-content/uploads/2016/04/snippet2-300x51.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/snippet2.jpg 600w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p>When we hit &lt;Enter&gt;, while the entry is selected, the code will be inserted in our current document.</p>
<p><a href="https://www.lucd.info/2016/04/23/visual-studio-code-powercli/snippet3/" rel="attachment wp-att-5217"><img loading="lazy" decoding="async" class="alignnone wp-image-5217 size-medium" src="https://lucd.info/wp-content/uploads/2016/04/snippet3-300x93.jpg" alt="snippet3" width="300" height="93" srcset="https://www.lucd.info/wp-content/uploads/2016/04/snippet3-300x93.jpg 300w, https://www.lucd.info/wp-content/uploads/2016/04/snippet3.jpg 691w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p>The <a href="https://code.visualstudio.com/docs/customization/userdefinedsnippets" target="_blank">syntax of a VSC snippet</a> is documented in the online VSC documentation.</p>
<h2>PowerShell Editor Services</h2>
<p>The <strong>PowerShell Editor Services</strong> offer a common extensibility model which allows one to write extension code in PowerShell. In the <a href="https://github.com/PowerShell/vscode-powershell/blob/master/CHANGELOG.md#060" target="_blank">0.6.0 release</a> this is now made available through the <strong>$psEditor</strong> object and the <strong>Register-EditorCommand</strong> cmdlet. More information can be found in the, also newly introduced, <a href="https://powershell.github.io/PowerShellEditorServices/" target="_blank">PowerShell Editor Services</a> documentation.</p>
<p>As an example of the use of the <a href="https://powershell.github.io/PowerShellEditorServices/guide/extensions.html#registering-editor-commands" target="_blank">Register-EditorCommand</a> I created the following example. It adds a command to the VSC that allows me to open a browser with the pages I normally use as reference and help when I work with PowerCLI. Add the following code to your profile.ps1 file.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$pclihelp = {
  $browser = 'chrome.exe'
  $pclisites = 'https://communities.vmware.com/community/vmtn/automationtools/powercli/content?filterID=contentstatus[published]~objecttype~objecttype[thread]',
  'https://www.vmware.com/support/developer/PowerCLI/PowerCLI63R1/html/index.html',
  'https://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/right-pane.html',
  'https://blogs.vmware.com/PowerCLI',
  'https://lucd.info'
  
  Start-Process $browser $pclisites
}

Register-EditorCommand `
-Name &quot;PowerCLI.HelpSites&quot; `
-DisplayName &quot;PowerCLI Help Sites&quot; `
-ScriptBlock $pclihelp</pre><p></p>
<p>In the VSC you can now use this new command as the following short video will show.</p>
<div style="width: 770px;" class="wp-video"><video class="wp-video-shortcode" id="video-5149-1" width="770" height="485" preload="metadata" controls="controls"><source type="video/mp4" src="https://lucd.info/wp-content/uploads/2016/04/vsc-powershell.mp4?_=1" /><a href="https://lucd.info/wp-content/uploads/2016/04/vsc-powershell.mp4">https://lucd.info/wp-content/uploads/2016/04/vsc-powershell.mp4</a></video></div>
<p>&nbsp;</p>
<p>A second example will actually allow you to act on selected text in the editor. With the EditorContext object you can actually what the user has selected, and take action accordingly. In this trivial example it takes the PowerCLI cmdlet that was selected, and open the online Help page for that cmdlet. Note that there is no checking implemented whatsoever, if you select something else than a PowerCLI cmdlet, the browser will go to an invalid page.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$pclicmdhelp = {
  param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context)

  $cmdlet = $context.CurrentFile.GetText($context.SelectedRange)
  $browser = 'chrome.exe'
  $cmdhelp = &quot;https://www.vmware.com/support/developer/PowerCLI/PowerCLI63R1/html/$($cmdlet).html&quot;
  
  Start-Process $browser $cmdhelp
}

Register-EditorCommand `
-Name &quot;PowerCLI.HelpCmdlet&quot; `
-DisplayName &quot;PowerCLI Cmdlet Help&quot; `
-ScriptBlock $pclicmdhelp</pre><p></p>
<p>And a short video that shows how this works.</p>
<ul>
<li>Select the PowerCLI cmdlet</li>
<li>Press &lt;F1&gt;</li>
<li>Type &#8216;ps ad&#8217; and the fuzzy search will bring you to the additional commands menu</li>
<li>Select the PowerCLI Cmdlet Help</li>
</ul>
<div style="width: 770px;" class="wp-video"><video class="wp-video-shortcode" id="video-5149-2" width="770" height="384" preload="metadata" controls="controls"><source type="video/mp4" src="https://lucd.info/wp-content/uploads/2016/04/pseditor-ex2.mp4?_=2" /><a href="https://lucd.info/wp-content/uploads/2016/04/pseditor-ex2.mp4">https://lucd.info/wp-content/uploads/2016/04/pseditor-ex2.mp4</a></video></div>
<p>&nbsp;</p>
<p>In this third example, I show how you can interrogate the AST and also how you can send feedback to the user. This command counts the different cmdlets and variables that are present in the file. And displays the result in the Output area.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$pscountcmdlet = {
  param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context)

  $cmdArr = @()
  $varArr = @()
  foreach($token in $context.CurrentFile.Tokens){
    switch($token.GetType().Name){
      'StringLiteralToken'{
        if($token.TokenFlags -eq 'CommandName'){
          $cmdArr += $token.Value
        }
      }
      'VariableToken'{
        $varArr += $token.Name
      }
    }
  }

  $cmdArr = $cmdArr | Sort-Object -Unique
  $varArr = $varArr | Sort-Object -Unique

  Write-Output &quot;You used $($cmdArr.Count) different cmdlets&quot;
  Write-Output &quot;`t$($cmdArr -join '|')&quot;
  Write-Output &quot;You used $($varArr.Count) different variables&quot;
  Write-Output &quot;`t$($varArr -join '|')&quot;
}

Register-EditorCommand `
-Name &quot;PowerShell.CountCmdletVar&quot; `
-DisplayName &quot;Count Cmdlets/Variables&quot; `
-ScriptBlock $pscountcmdlet</pre><p></p>
<p>And a short video to demonstrate.</p>
<div style="width: 770px;" class="wp-video"><video class="wp-video-shortcode" id="video-5149-3" width="770" height="586" preload="metadata" controls="controls"><source type="video/mp4" src="https://lucd.info/wp-content/uploads/2016/04/pseditor-ex3.mp4?_=3" /><a href="https://lucd.info/wp-content/uploads/2016/04/pseditor-ex3.mp4">https://lucd.info/wp-content/uploads/2016/04/pseditor-ex3.mp4</a></video></div>
<p>&nbsp;</p>
<p>At the end of this post, you can download a sample VSC profile.ps1, it contains the PowerCLI Universal Loader function and the <strong>Register-EditorCommand</strong> example.</p>
<p>I&#8217;m currently still exploring all the possibilities of the the <strong>VS@Code</strong> and<strong> <strong>PowerShell for VS@Code</strong></strong>. There is a whole lot more to discover, and I intend to keep this post updated with new discoveries and additional features that will come in future releases.</p>
<p>If you want to play with VSC and the PowerShell feature, there are a couple of excellent blog posts from <a href="https://twitter.com/r_keith_hill" target="_blank">Keith Hill</a>, that will help you get started.</p>
<p><a href="https://rkeithhill.wordpress.com/2015/12/26/getting-started-with-visual-studio-code-for-use-with-powershell/">Getting Started with Visual Studio Code for Use with PowerShell</a><br />
<a href="https://rkeithhill.wordpress.com/2015/12/27/debugging-powershell-script-with-visual-studio-code/" target="_blank">Debugging PowerShell Script with Visual Studio Code</a></p>
<p>And there is of course the extensive, online <a href="https://code.visualstudio.com/Docs" target="_blank">VSC documentation</a>.</p>
<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2016/04/23/visual-studio-code-powercli/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		<enclosure url="https://lucd.info/wp-content/uploads/2016/04/vsc-powershell.mp4" length="1115132" type="video/mp4" />
<enclosure url="https://lucd.info/wp-content/uploads/2016/04/pseditor-ex2.mp4" length="204225" type="video/mp4" />
<enclosure url="https://lucd.info/wp-content/uploads/2016/04/pseditor-ex3.mp4" length="292746" type="video/mp4" />

			</item>
	</channel>
</rss>
