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
Those drives are activated when you load the Core module.
Try this
Import-Module -Name VMware.VimAutomation.Core
Get-PSDrive
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 🙂
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
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
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?
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