VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Visual Studio Code (VScode) problems with running PowerCLI commands from script pane.

Sorry, can't seem to find this anywhere else, I hope someone here can help me.

I really want to start using VScode, and I set everything up to start using it for everything PowerShell/PowerCLI.  There is one issue that I can't seem to fix.  I set my default terminal to PowerShell, and I can run commands whether they are PowerShell or PowerCLI.  When I'm in the script window, if I want to run a command as simple as Get-Cluster, it gives me the message below:

"You are not currently connected to any servers. Please connect first using a Connect cmdlet."

I made sure that I connected to vCenter before, and as long as I run the command in the terminal only, it works, but if I go up to where I would write out a script, it's as if that pane is being treated like it's an entirely different session.  I included a screenshot.  Thanks for any help. 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you already check what happens when you switch to Debug (left bar), and then press the Run button in the toolbar?

debug.png


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

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Oops, just noticed what is wrong.
The VSC decides based on the filetype which engine to use to run the code.

You have to save that code first in a .ps1 file


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following two screenshots show what is happening.

Without a .ps1 , the VSC doesn't know how to run that code

vsc1.png

With a .ps1 filetype, VSC knows to use PowerShell to run the code

vsc2.png


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

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

I understand your explanation, but that does not seem to work for me.  By default in my settings, I have this line in my setttings: "files.defaultLanguage": "powershell", so every new item defaults to powershell and vscode even shows the correct icon for the file.  I did save my "untitled" file to "untitled.ps1".  The file name changed, but I still can't run PowerCLI commands from it.  It still produces the same message that I'm not connected to any servers.  I also made sure that I don't have multiple terminals open, and I can run Get-Cluster from the terminal.

I typed Get-Cluster out in the terminal and it shows my clusters, and I included a screenshot.  I then went to my code window, and ran the code, but it's saying that I'm not connected.  I tried it on the saved "Untitiled-3.ps1" and I created a new file with Get-Cluster as my line of code, and when I run either saved or unsaved file, I get the same results that I'm not connected.  Everything works fine when typed directly from the terminal.

I do see the "OUTPUT" shows that there is some tempCodeRunnerFile.ps1 that executes, I'm going to search around and see if there is some edit/change that may help.  I appreciate the help so far.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you check the content of the launch.json file?

You can open that one through the menu and Debug - Open Configurations

launch.png

When you select the Debug in the left bar, and open the debug drop-down list, do you see all the options that you have in the launch.json file?

debug.png

Did you actually save that file somewhere?

Perhaps with a different name than untitled?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And what is shown under the Problems (2) tab?


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

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

The problems are just an alias and when I was changing the PowerCLI configuration.  I've also attached two screenshots.  I also see the dropdown menu to launch with the current file or host process, etc.  I'll try and change those just to see if they make a difference.  I also definitely saved the "Untitled" file, I see the path, and I included it in the screenshot.  I appreciate the help, I was hoping it was just a quick and easy setting that I missed, but I'm going to look around some more. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you already check what happens when you switch to Debug (left bar), and then press the Run button in the toolbar?

debug.png


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

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Well, like most things, it's seems to be user error, and since I'm the user, it must be me.  The cmdlets work from the debug area, whether the file is saved or not, but it does not work when I just run the code.  I wonder if it's the extension.  I'll remove it, play around with it and see if that helps.  Just curious, what extensions do you use?  I included a screenshot.  Thanks again for your help, I doubt that I would have tried the debug bar. 

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Thanks for your help.  I removed the "code runner" extension, and I'm only running either F5 or F8 for debugging and it's working fine.  I started installing extensions and looking at videos on vscode before actually using it, and I thought the issue was within the app, and not an extension.  I'll probably add the extension back and figure out the problem, but for now, it's working just fine.  I appreciate your help this time, and all the times in the past. 

Since I can't really help with you with PowerCLI issues, I'll at least plug your book. Go out and buy:

VMware vSphere PowerCLI Reference: Automating vSphere Administration 2nd Edition

by Luc Dekens (Author),‎ Jonathan Medd (Author),‎ Glenn Sizemore (Author),‎ Brian Graf (Author),‎ Andrew Sullivan (Author),‎ Matt Boren (Author)

VMware vSphere PowerCLI Reference: Automating vSphere Administration: 9781118925119: Computer Scienc...

Thanks again. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks for the plug :smileyblush:

Wondering if it wouldn't be advisable to uninstall/install VSC completely before continuing?

And only install the PowerShell extension. Clean slate!


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

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

You're welcome, it's the least that I could do.  I'm probably going to install vscode on another VM first, and see if that works with the extensions, and then uninstall/reinstall on my other VM.  At least my settings and everything are easy to copy and paste over.  Thanks again. 

Reply
0 Kudos