<?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/category/powershell/visual-studio-code/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/category/powershell/visual-studio-code/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Mon, 09 Dec 2019 09:09:35 +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/category/powershell/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>Cloud-init &#8211; Part 2 &#8211; Advanced Ubuntu</title>
		<link>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/</link>
					<comments>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sat, 07 Dec 2019 07:43:24 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Deploy]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[powershellv6]]></category>
		<category><![CDATA[powershellv7]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Visual Studio Code]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[cloud-init]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[repository]]></category>
		<category><![CDATA[v6]]></category>
		<category><![CDATA[v7]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=6693</guid>

					<description><![CDATA[In Cloud-init – Part 1 – The Basics, we laid the groundwork for [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In <a href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank" rel="noopener noreferrer">Cloud-init – Part 1 – The Basics</a>, we laid the groundwork for using <strong>cloud-init</strong> in a <strong>vSphere</strong> environment. In this post we will go into more <strong>advanced Ubuntu</strong> setups. This includes deploying PowerShell, v6 and v7, using repositories and if needed, a GUI with Visual Studio Code.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="679" height="296" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2.png" alt="" class="wp-image-6708" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2.png 679w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2-300x131.png 300w" sizes="(max-width: 679px) 100vw, 679px" /></figure>



<span id="more-6693"></span>



<p>For the installation of software on the instance we use the <strong>user-data</strong> YAML file. In the following sections, we discuss some of the more obvious candidates you want to have on your station. Remember that the basic idea of the series is to have a &#8220;<em>cattle</em>&#8221; station to test/run (and optionally develop) your scripts. </p>



<h2 class="wp-block-heading">Scripting Packages</h2>



<h3 class="wp-block-heading">PowerShell</h3>



<p>Let&#8217;s start by installing <strong>PowerShell</strong> on the instance. This comes down to adding a couple of instructions to the YAML file we used in Part 1.</p>



<pre class="lang:yaml decode:true ">#cloud-config
hostname: ubuntubionicps
fqdn: ubuntubionicps.local.lab
write_files:
- path: /etc/netplan/50-cloud-init.yaml
  content: |
    network:
     version: 2
     ethernets:
      ens192:
       addresses: [192.168.10.82/24]
       gateway4: 192.168.10.1
       dhcp6: false
       nameservers:
         addresses:
           - 192.168.10.2
           - 192.168.10.3
         search:
           - local.lab
       dhcp4: false
       optional: true
- path: /etc/sysctl.d/60-disable-ipv6.conf
  owner: root
  content: |
    net.ipv6.conf.all.disable_ipv6=1
    net.ipv6.conf.default.disable_ipv6=1
runcmd:
- netplan --debug apply
- sysctl -w net.ipv6.conf.all.disable_ipv6=1
- sysctl -w net.ipv6.conf.default.disable_ipv6=1
- apt-get -y update
- add-apt-repository universe
# PowerShell
- wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh
- chmod 755 install-powershell.sh
# PowerShell v6
- ./install-powershell.sh
# PowerShell v7
- ./install-powershell.sh -preview
- apt-get -y clean
- apt-get -y autoremove --purge
timezone: Europe/Brussels
system_info:
  default_user:
    name: default-user
    lock_passwd: false
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
disable_root: false
ssh_pwauth: yes
users:
  - default
  - name: luc
    gecos: LucD
    lock_passwd: false
    groups: sudo, users, admin
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
chpasswd:
  list: |
    default-user:$6$aPp//e2ueP$ETEXcAhAyQuJ4qNCbqxmmSYGZbg2wFwpP/YITvoXdgxwZBnf32drePKi2OIn5fLqtH5pHO03yRdPXK3ToLG6b0
    luc:$6$kW1WwJ2K$M6415du1BZd.qt92SvR6X.RuyDhEZmgR4hz4NcKH9XHn2850Vc6zHpubXM6uUeqMUaJQ740ogROB74gfBEhn9.
    root:$6$Js9CVr06$br9qf0VxuBsdY7Vtg/0pk9jLlycYBDLVsvbKwLDleCK7dSDheOxWaFOWdjkiqSPRrWG./N8V5RgCVwugZGnTc1
  expire: false
package_upgrade: true
package_reboot_if_required: true
power_state:
  delay: now
  mode: reboot
  message: Rebooting the OS
  condition: if [ -e /var/run/reboot-required ]; then exit 0; else exit 1; fi</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 33-39</strong>: The instructions to install PowerShell are added to the <strong>runcmd</strong> section.</p>



<p><strong>Line 33</strong>: You can use comments in your YAML file. I highly recommend doing that. It will help others, and probably yourself later on.</p>



<p><strong>Line 34-35</strong>: The method uses the <a rel="noreferrer noopener" aria-label="install.sh (opens in a new tab)" href="https://github.com/PowerShell/PowerShell/blob/master/tools/install-powershell.sh" target="_blank">install-powershell.sh</a> bash script to install the latest PowerShell v6 and the latest PowerShell v7 Preview. I personally prefer this method vs the apt-get method, since it is globally usable on multiple Linux distributions. The <strong>install-powershell.sh</strong> script determines on which distro it is running, and launches the appropriate command(s).</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img decoding="async" width="550" height="288" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps.png" alt="" class="wp-image-6744" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps.png 550w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps-300x157.png 300w" sizes="(max-width: 550px) 100vw, 550px" /></figure>



<h3 class="wp-block-heading">Git &amp; repo cloning</h3>



<p>For your scripts, you, of course, use a repository. That means you will need to configure <a rel="noreferrer noopener" aria-label="git (opens in a new tab)" href="https://www.linux.com/tutorials/introduction-using-git/" target="_blank">git</a>, and a way to clone repositories to your instance.</p>



<p>The following extract of the YAML file only shows the lines in the <strong>runcmd</strong> section.</p>



<pre class="lang:yaml decode:true  ">runcmd:
- netplan --debug apply
- sysctl -w net.ipv6.conf.all.disable_ipv6=1
- sysctl -w net.ipv6.conf.default.disable_ipv6=1
- apt-get -y update
- add-apt-repository universe
# PowerShell
- wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh
- chmod 755 install-powershell.sh
# PowerShell v6
- ./install-powershell.sh
# PowerShell v7
- ./install-powershell.sh -preview
# Git
- git config --global user.name "LucD"
- git config --global user.email "lucd@lucd.info"
- mkdir /home/luc/vCheck-vSphere
- chown luc:luc /home/luc/vCheck-vSphere
- git clone https://github.com/alanrenouf/vCheck-vSphere /home/luc/vCheck-vSphere
- apt-get -y clean
- apt-get -y autoremove --purge</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 14-16</strong>: Since git is by default installed in the Ubuntu 18.04 LTS Cloud Image OVA file, we only need to configure some settings. In this we update some configuration settings, primarily to be able to correct sign off commits.</p>



<p><strong>Line 17-19</strong>: As part of the deployment process through cloud-init, you can clone specific repositories to the instance. In these lines I used <a rel="noreferrer noopener" aria-label="Alan Renouf (opens in a new tab)" href="https://twitter.com/alanrenouf" target="_blank">Alan Renouf</a>&#8216;s <a href="https://github.com/alanrenouf/vCheck-vSphere" target="_blank" rel="noreferrer noopener" aria-label="vCheck-vSphere (opens in a new tab)">vCheck-vSphere</a> repository as an example.</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img decoding="async" width="517" height="293" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck.png" alt="" class="wp-image-6747" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck.png 517w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck-300x170.png 300w" sizes="(max-width: 517px) 100vw, 517px" /></figure>



<h2 class="wp-block-heading">Going GUI</h2>



<p>There might be occasions when you need to provide a temporary station with a GUI. That can also be done with cloud-init&#8217;s user-data YAML file.</p>



<h3 class="wp-block-heading">The Desktop</h3>



<p>The choice of which desktop and which desktop manager is completely up to you. Just be aware that some of these might require more resources to be assigned to the instance.</p>



<p>The following sample is intended for an Ubuntu 18.04 LTS instance. Again this extract only shows the <strong>runcmd</strong> section of the YAML file.</p>



<pre class="lang:yaml decode:true   "># Install GUI
- apt-get install -y tasksel
- tasksel install lubuntu-core
# ==&gt; know issue
- apt-get --purge remove -y light-locker
- service lightdm start
# Install RDP
- apt-get install -y apt-transport-https
- apt-get install -y xrdp
- systemctl enable xrdp</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 1-10</strong>: In this example, I use <a rel="noreferrer noopener" aria-label="lubuntu (opens in a new tab)" href="https://lubuntu.net/" target="_blank">lubuntu</a> as the desktop. Primarily because it is fast and resource-friendly.</p>



<p><strong>Line </strong>2: We use <a rel="noreferrer noopener" aria-label="tasksel (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/xenial/man8/tasksel.8.html" target="_blank">tasksel</a> to install the desktop, so we have to make sure that it is available.</p>



<p><strong>Line 5</strong>: There is a known issue with the <a rel="noreferrer noopener" aria-label="light-locker (opens in a new tab)" href="https://launchpad.net/ubuntu/+source/light-locker" target="_blank">light-locker</a> package. Until a final fix is available, the easiest solution is to just uninstall it. If you absolutely need a screen-saver, install one of the alternatives. Like for example <a rel="noreferrer noopener" aria-label="xscreensaver (opens in a new tab)" href="https://packages.ubuntu.com/search?keywords=xscreensaver" target="_blank">xscreensaver</a>.</p>



<p><strong>Line 6</strong>: Next we start the display manager <a rel="noreferrer noopener" aria-label="lightdm (opens in a new tab)" href="https://wiki.ubuntu.com/LightDM" target="_blank">lightdm</a>.</p>



<p><strong>Line 7-</strong>10: We need a way to connect to the station we are deploying. In this example, I opted for RDP as my protocol.</p>



<p><strong>Line 8</strong>: The <a rel="noreferrer noopener" aria-label="apt-transport-https (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/bionic/man1/apt-transport-https.1.html" target="_blank">apt-transport-https</a> package is a pre-requisite.</p>



<p><strong>Line 10</strong>: Configure the <a rel="noreferrer noopener" aria-label="xrdp (opens in a new tab)" href="https://help.ubuntu.com/community/xrdp" target="_blank">xrdp</a> service to automatically start at the boot.</p>



<h3 class="wp-block-heading">Visual Studio Code</h3>



<p>Now that we have a GUI, we can use several GUI based applications. For working with PowerShell, the <a rel="noreferrer noopener" aria-label="Visual Studio Code (opens in a new tab)" href="https://code.visualstudio.com/" target="_blank">Visual Studio Code</a> editor with the <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank" rel="noreferrer noopener" aria-label="PowerShell extension (opens in a new tab)">PowerShell extension</a> is an obvious choice.</p>



<pre class="lang:yaml decode:true "># Install Visual Studio Code
- add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
- apt-get update
- apt-get install -y code
- mkdir /home/luc/.vscode
- chown luc:luc /home/luc/.vscode
- code --install-extension ms-vscode.powershell --user-data-dir /home/luc/.vscode/ --extensions-dir /home/luc/.vscode/extensions/
- code --install-extension eamodio.gitlens --user-data-dir /home/luc/.vscode --extensions-dir /home/luc/.vscode/extensions/</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 2-4</strong>: The standard <a href="https://code.visualstudio.com/docs/setup/linux" target="_blank" rel="noopener noreferrer">installation procedure</a> for Code on a Linux platform.</p>
<p><strong>Line 5-8</strong>: This installs the extensions for <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank" rel="noopener noreferrer">PowerShell</a> and <a href="https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens" target="_blank" rel="noopener noreferrer">GitLens</a>. Remember that the cloud-init stages run under the root account, if you need to install packages for a user, make sure to change the owner.</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="561" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1024x561.png" alt="" class="wp-image-6773" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1024x561.png 1024w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-300x164.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-768x421.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1536x842.png 1536w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-720x395.png 720w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI.png 1633w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">Conclusion</h2>



<p>We have now set the stage to actually start using these instances. In one of the next posts in this cloud-init series, we will show how to run scripts on these stations.</p>



<p>If you have suggestions or questions about specific additions on these instances, feel free to use the comments.</p>



<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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>
<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>
<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>
<p><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>
<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>
<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>
<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>
