VMware Cloud Community
aakuzmenko
Contributor
Contributor

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

Hi, I had been testing waters with Microsoft Visual Studio Code (VS Code).

Running into some inconsistences.  Cannot run PowerCLI scripts that call PSDrive. It appears VScode cannot access VIM Inventory providers. I am running VS Code as Administrator

Any suggestions on how to work with PSDrive under VS Code? Perhaps I need to adjust VS code settings\permissions to get access to VimInventory? Below is comparison of VS Code vs ISE

Thank you

Example: Get-PSDrive under VScode. There are no vi or vis listed

**************************************************************************

PS C:\> Get-PSDrive

 

Name           Used (GB)     Free (GB) Provider      Root                                                                                                                                                                                            CurrentLocation

----           ---------     --------- --------      ----                                                                                                                                                                                            ---------------

Alias                                  Alias

C                  71.65        166.21 FileSystem    C:\

Cert                                   Certificate   \

D                                      FileSystem    D:\

Env                                    Environment

Function                               Function

HKCU                                   Registry      HKEY_CURRENT_USER

HKLM                                   Registry      HKEY_LOCAL_MACHINE

Variable                               Variable

WSMan                                  WSMan

********************************************************************

Example: Get-PSDrive under ISE. There are vi and vis, vmstore and vmstores!

*************************************************************

PS C:\> Get-PSDrive

 

Name           Used (GB)     Free (GB) Provider      Root                                                                                                                                                                                         CurrentLocation

----           ---------     --------- --------      ----                                                                                                                                                                                         ---------------

Alias                                  Alias                                                                                                                                                                                                                     

C                  71.66        166.21 FileSystem    C:\                                                                                                                                                                                                         

Cert                                   Certificate   \                                                                                                                                                                                                           

D                                      FileSystem    D:\                                                                                                                                                                                                         

Env                                    Environment                                                                                                                                                                                                               

Function                               Function                                                                                                                                                                                                                  

HKCU                                   Registry      HKEY_CURRENT_USER                                                                                                                                                                                           

HKLM                                   Registry      HKEY_LOCAL_MACHINE                                                                                                                                                                                         

Variable                               Variable                                                                                                                                                                                                                 

vi                                     VimInventory  \LastConnectedVCenterServer                                                                                                                                                                                

vis                                    VimInventory  \                                                                                                                                                                                                          

vmstore                                VimDatastore  \LastConnectedVCenterServer                                                                                                                                                                                

vmstores                               VimDatastore  \                                                                                                                                                                                                          

WSMan                                  WSMan          

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

Those drives are activated when you load the Core module.

Try this

Get-PSDrive

Import-Module -Name VMware.VimAutomation.Core

Get-PSDrive


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
aakuzmenko
Contributor
Contributor

Tried, same result - no vi or vis. It appears the clue is in the execution environment. My VS code defaults to PowerShell Integrated Concole as opposed to powershell (dropdown box on the right, middle of the screen ). When I switched terminal to "powershell" and cut and paste command it ran as expected
Reply
0 Kudos
LucD
Leadership
Leadership

Makes no difference for me

psdrive.png


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
aakuzmenko
Contributor
Contributor

I end up re-installing PowerShell extension. It is working now. I am able to see VimInventory providers under "PowerShell Integrated Concole".  Thank you so much for quick reply Luc!

P.S. It contunues working under "powershell" terminal 🙂

Reply
0 Kudos
aakuzmenko
Contributor
Contributor

Follow up to those that might experience similar issue.

The problem is intermittent and it happens only in “Integrated console”. Apparently it is triggered by code analyzer. Whenever code analysis is triggered it pre-loads required modules. This load creates names for the PS drives.

When the actual script is runs it fails to create PS drives as the names are already in use.

Here is link to github with more details "PowerCLI module load fail to create PS drives #543"

https://github.com/PowerShell/PowerShellEditorServices/issues/543

So solution for now is to disable code analyzer or … avoid hovering mouse over code

I will try switching default terminal to pure PowerShell (not integrated) and see if it work

Reply
0 Kudos
LucD
Leadership
Leadership

You can do the initial load of the PowerCLI module (VMware.VimAutomation.Core) in one of your profile files.
That way you should be able to avoid the issue altogether.

As the issue you referred to mentions, this is basically a PowerCLI problem due to it's non-idempotency

The $global:defaultviserver and $global:defaultviservers variables are other examples of this issue.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
aakuzmenko
Contributor
Contributor

also testing another work around - open VS code with blank script pane (making sure there are no powercli commandlets) and first thing run "Import-Module VMware.VimAutomation.Core" in "PowerShell Integrated Concole". Making sure to claim this name space first befor analyser kicks in. So far it is working

The right thing to do is probably configure profile to load the requred modules on start up?

Reply
0 Kudos
aakuzmenko
Contributor
Contributor

Luc u r fast! I just noticed your reply after posting note about profiles
Reply
0 Kudos
LucD
Leadership
Leadership

It could be handy to note that VSC has its own profile file, next to the regular PowerShell profiles.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos