1

I'm currently running the following script I've made for practice as I'm attempting to improve my powershell skills, I wanted to start with some basics in case I missed things as I slowly pieced together my knowledge and was calculating circles and other shapes/forms to work with functions a bit.

function calculate-circle{ 
$pi = 3.14
[float]$radius = read-host "What is the radius?"

$surface = $radius*$radius*$pi
write-host "The surface area of a circle with radius $radius is $surface"

}

Within the Powershell ISE, this executes without a flaw and I'm able to input a radius and come up with calculations.

In VS Code I get the following, after highlighting the entire script and running it using the hotkey for "Run Selected Text In Active Terminal".

PS C:\> function calculate-circle{
Missing closing '}' in statement block or type definition.
At line:0 char:0
PS C:\>     $mypi = 3.14
PS C:\>     [float]$radius = read-host "What is the radius?"
What is the radius?:
PS C:\>     $surface = $radius*$radius*$mypi
PS C:\>     write-host "The surface area of a circle with radius $radius is $surface"
The surface area of a circle with radius 0 is 0
PS C:\> }
At line:1 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.

What I can't figure out is, is this an issue with my code or is this some quirk of Powershell within VS Code? I was really enjoying some of the VS Code functionality so I'm a bit miffed. Thanks a ton!

0

1 Answer 1

1

Using the Terminal: Run Selected Text In Active Terminal command from the command palette, also accessible as Run Selected Text from the Terminal menu, submits and executes the selection's lines individually, which doesn't work with multi-line snippets such as function definitions.

Instead, use the PowerShell: Run Selection command that comes with the PowerShell extension, which is bound to hotkey F8 by default, just like in the ISE; you can also access it from a selection's context menu using the Run Selection command.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.