Setting Up VSCode for PowerShell in 3 easy step

Recently I have switched from PowerShell ISE to VSCode for script development. I was very comfortable with PowerShell ISE except it’s “intellisense” feature which used to hang my system a lot. Switching to VSCode certainly improved my development experience and gave me a better tool for debugging and testing my Powershell scripts.

VSCode is an open-source source code editor which supports multiple programming languages with the help of VSCode extensions.

Installing and setting up VSCode for PowerShell

  1. Install VSCode
    VSCode is available for Windows, Linux and Mac devices. You can install VSCode from https://code.visualstudio.com/Download
  2.  Install PowerShell extension for VSCode
    • Go to Extension
    • Search for ‘Powershell’
    • Select and install ‘Powershell’ extension
    • Reload
  3. Change your default language mode to PowerShell, Once your PowerShell extension is installed you will see that intellisense in VSCode does not provide suggestions like it does for PowerShell ISE, That is because you have not set up your working environment to PowerShell. To change your working environment to PowerShell do follow below steps-
    • In VSCode, hit ctrl + shift + p 
    • Type “Change Language Mode”
    • and, Select Powershell

With the above steps intellisense will work in the current script file but it will not work in a new script file,

  • To make PowerShell as your default language mode do follow below steps.
    • Got to Settings
    • Search for ‘defaultlan’ and you will find below key 
    •  Set this key to ‘Powershell’ and hit ctrl+s,

That’s all you need to do in order to set up VSCode for PowerShell.

Thanks,

ESXCLI+PowerCLI: Perfect fusion for ESXi

powershell-cim_1

Hello Everyone,

In this post I will discuss about managing ESXi hosts using ESXCLI and PowerCLI. Yes, You got it right. ESXCLI is one of the widely used command line interface for ESXi whereas PowerCLI is a PowerShell based snap-in for VMware.

ESXCLI is intended to provide a single set of commands to perform host based administrative tasks. ESXCLI has set of namespace for various components like network, storage, device etc. Refer below pic to see available namespaces.

How to run ESXCLI?

Well working with ESXCLI is very simple. Take a putty session to host and type esxcliOnce you run this command then it will show you all the available namespace under ESXCLI. Further more you can type child namespace and it will show you available namespace and commands under that.

Ex. 1

~ # esxcli network nic list

The above command will list NIC details (MAC, Adapter type, Link status etc.)

ESXCLI
ESXCLI Namespace

When you have very small environment or performing specific tasks on 1-2 esxi then taking a putty session and running ESXCLI command is not a big pain. However there are scenarios where you want to perform management tasks on large number of ESXi hosts then above approach may not be a feasible one. Such scenarios could be-

  1. Getting certain reports across all the hosts (Firmware, driver version or specific configuration detail)
  2. Setting up standard configuration across multiple hosts or entire environment.

etc.

So in such type of scenario you would like to automate the task. For that obvious choice would be PowerCLI.

Tip: PowerCLI is a PowerShell snap-in for VMware. There is no specific training required for PowerCLI. 

How to execute ESXCLI Commands using PowerCLI?

PowerCLI has a cmdlet called Get-ESXCLI, This cmdlet can be executed against remote ESXi hosts. This can be done by passing a value (hostname) to -VMhost parameter. Refer below command-

PowerCLI C:\> Get-Esxcli -VMhost TestEsxi.poc

The above command will give you available ESXCLI namespace on TestEsxi.poc host.

For a moment let us consider Ex 1. ~ # esxcli network nic list

Same result can be achieved via executing below command in PowerCLI.

PowerCLI C:> (Get-Esxcli -VMhost "TestEsxi.poc").network.nic.list()

How to automate same task for multiple hosts?

Below is the PS script which will automate same task for multiple host. I don’t think that below script needs any explanation, It is very simple and straight forward script. First, You Get content from a .txt file. This file contains ESXi Host names. In later part you are just taking hostname one by one and dynamically passing it to cmdlet.

Please refer below screenshot for same, Though it does not harm anything to your environment but still i would highly recommend you to run this in POC first. Also please note that you have to connect vCenter first.

PowerCLI C:> Connect-VIServer -Server vCenterName

 

ESXCLItest.PNG
ESXCLI Test

Conclusion:

ESXCLI is a great tool for ESXi management and capability to integrate with PowerShell is just awesome. Next time if you have to collect few reports from multiple ESXi hosts then try to automate those reports using PowerCLI and ESXCLI.

FAQs:

  1. How to Learn PowerShell?
  2. How to install PowerCLI within PowerShell Console?
    • Check out this blog for your reference

Hope you liked this post, Please comment and share your feedback in comment section.

Thanks,