VSC and PowerCLI’s VimInventory and VimDatastore providers.
Currently Visual Studio Code is the de facto preferred editor for PowerShell and PowerCLI scripts.
But there is a tiny problem when you try to mount a datastore as a PSDrive in the PowerShell Integrated Console.
Due to the way PowerCLI is written, it is not possible to use the VimInventory and VimDatastore providers, that come with PowerCLI, from the PowerShell Integrated Console.
Note that this is the console that shows the following.
You will see the following error when you try to set up a drive with one of these providers.
This is a known problem and mentioned in PowerShell/PowerShellEditorServices Issue #543 and also in the VMTN thread Microsoft Visual Studio Code (VS Code) can not access vi and vis, vmstore and vmstores. Get-PSDrive and New-PSDrive -PSProvider VimDatastore dont work as expected
A couple of workarounds are available:
- Disable the PSScriptAnalyzer in VSC
- Don’t hover over PowerCLI cmdlets in the editor.
- Load the PowerCLI modules before hovering over a PowerCLI cmdlet in the editor
Of course, the preferred solution is option 3 from that list. And since VSC has it’s own profile, it is quite easy to implement.
Create/modify the VSC PowerShell profile, with
In the profile file, add the following line
Import-Module -Name VMware.PowerCLI 6> $null
The redirection of stream 6 is intended to suppress the Write-Host output from loading the VMware.PowerCLI module. This will only work in PowerShell v5, and higher.
If you find the output produced by loading the VMware.PowerCLI module annoying, there is a PowerCLI Idea that asks to suppress this “feature”. Please upvote the issue if you also would like to see this removed.